Skip to content

Instantly share code, notes, and snippets.

Using the API

Getting started

Accessing the API

The API is made available via our web Console. You can use the Workbench to try out the API in the browser and then generate API keys in Account Settings. Use workspaces to segment your API keys and control spend by use case.

Authentication

All requests to the Anthropic API must include an x-api-key header with your API key. If you are using the Client SDKs, you will set the API when constructing a client, and then the SDK will send the header on your behalf with every request. If integrating directly with the API, you’ll need to send this header yourself.

@8enmann
8enmann / .zshrc
Created January 19, 2021 00:26
dotfiles
# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
@8enmann
8enmann / test_process.py
Created June 24, 2019 20:46
Partly broken example trying to test Process as a single thread.
# mock_process.py
"""Make a process single threaded for testing."""
import multiprocessing
def f(x):
return x
def target():
"""Run a process by specifying a target function."""
@8enmann
8enmann / barrier_example.py
Created June 24, 2019 18:10
Simple Barrier demo
# barrier_example.py
"""Example usage of barrier."""
import multiprocessing as mp
import itertools
def worker(barrier:mp.Barrier, q:mp.Queue=None):
print(mp.current_process().name, 'waiting')
i = barrier.wait()
if i == 0:
@8enmann
8enmann / bpe.py
Created June 24, 2019 03:29
Approximate BPE implementation.
"""Implements an approximate BPE encoding over bytes with some tricks for efficiency.
https://arxiv.org/pdf/1508.07909.pdf section 3.2.
Basic algorithm from the paper:
Initialize the vocab with the character vocabulary
Each word is a sequence of characters plus an enod of word symbol '·'
Count all symbol pairs
Replace each occurence of the most frequent pair ('a', 'b') with 'ab'.
Each merge represents a character n-gram
@8enmann
8enmann / binary_search.py
Created June 23, 2019 19:17
Practice implementation of binary search.
"""Practice implementation of binary search."""
import pytest
from typing import Sequence
def binary_search(sorted_arr: Sequence[int], target:int) -> int:
"""Find the index of target in sorted_arr.
Returns:
Index of the target or -1 if not found. Picks the first match randomly if there are multiple.
@8enmann
8enmann / ngram.py
Created June 22, 2019 21:03
Calculate ngrams as fast as possible
"""Calculate N-gram counts as fast as possible for a large encoded file."""
import numpy as np
from collections import deque
import multiprocessing as mp
from typing import Any, Iterable, Generator
import time
from collections import Counter
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
from functools import partial
@8enmann
8enmann / counter.py
Created June 22, 2019 20:18
Experimenting with pytest, cosine similarity, ngram counting
import requests
import re
from typing import List, Sequence
from collections import Counter
import numpy as np
TEST_URL = 'http://titan.dcs.bbk.ac.uk/~kikpef01/testpage.html'
URL_RE = re.compile(r'href="(http.+?)"')
TOKENIZER_RE: re.Pattern = re.compile(r'\w+')
@8enmann
8enmann / count.py
Last active June 22, 2019 20:14
Benchmark reading files with threading and multiprocessing
"""Read files using threading and multiprocessing.
Execute on https://coderpad.io/sandbox
"""
from concurrent.futures import ThreadPoolExecutor, as_completed
from multiprocessing.dummy import Pool
import multiprocessing
from collections import Counter
import glob
@8enmann
8enmann / test_format_time.py
Created June 21, 2019 02:05
Format time in Python using pytest
"""
TDD solution to:
Input: a bunch of times, minutes and seconds, formatted as a single string like: "12:32 34:01 15:23 9:27 55:22 25:56"
Output: the sum of the times, hours, minutes, and seconds, formatted as a single string like: "2:32:41"
https://github.com/cjdev/interview-preparation
https://coderpad.io/sandbox
"""
import pytest