turbopuffer Python Client
The official Python client for accessing the turbopuffer API.
Usage
- Install the turbopuffer package and set your API key.
$ pip install turbopuffer
Or if you're able to run C binaries for JSON encoding, use:
$ pip install turbopuffer[fast]
- Start using the API
import turbopuffer as tpuf
tpuf.api_key = 'your-token'
tpuf.api_base_url = "https://gcp-us-east4.turbopuffer.com"
ns = tpuf.Namespace('hello_world')
if ns.exists():
print(f'Namespace {ns.name} exists with {ns.dimensions()} dimensions and approximately {ns.approx_count()} vectors.')
ns.upsert(
ids=[1, 2],
vectors=[[0.1, 0.2], [0.3, 0.4]],
attributes={'name': ['foo', 'foos']},
distance_metric='cosine_distance',
)
ns.upsert(
{
'id': id,
'vector': [id/10, id/10],
'attributes': {'name': 'food', 'num': 8}
} for id in range(3, 10),
distance_metric='cosine_distance',
)
vectors = ns.query(
vector=[0.15, 0.22],
distance_metric='cosine_distance',
top_k=10,
filters=['And', [
['name', 'Glob', 'foo*'],
['name', 'NotEq', 'food'],
]],
include_attributes=['name'],
include_vectors=True
)
print(vectors)
namespaces = tpuf.namespaces()
print('Total namespaces:', len(namespaces))
for namespace in namespaces:
print('Namespace', namespace.name, 'contains approximately', namespace.approx_count(),
'vectors with', namespace.dimensions(), 'dimensions.')
ns.delete([1, 2])
Endpoint Documentation
For more details on request parameters and query options, check the docs at https://turbopuffer.com/docs
Development
poetry run pytest
- Bump version in
turbopuffer/version.py
and pyproject.toml
git tag vX.Y.Z && git push --tags
poetry build
poetry publish