Logo

Quickstart Guide

# First, install the python package
# $ pip install turbopuffer[fast]

import turbopuffer as tpuf
# API tokens are given to you in your invite
tpuf.api_key = "your-token"

ns = tpuf.Namespace('namespace-name')

# Upsert vectors and attributes
ns.upsert(
    ids=[1, 2],
    vectors=[[0.1, 0.2], [0.3, 0.4]],
    attributes={ "name": ["foo", "foo"], "public": [1, 0] },
    distance_metric='cosine_distance',
)

# Query nearest neighbors
print(ns.query(
  vector=[0.15, 0.22],
  top_k=10,
  distance_metric="cosine_distance",
  filters=["And", [["name", "Eq", "foo"], ["public", "Eq", 1]]],
  include_attributes=["name"],
  include_vectors=False
))
# [VectorRow(id=1, vector=None, attributes={'name': 'foo'}, dist=0.009067952632904053)]

# Vectors can be updated by passing new data for an existing ID
ns.upsert(
  ids=[1, 2, 3],
  vectors=[[1.1, 1.2], [1.3, 1.4], [1.5, 1.6]],
  attributes={ "name": ["foo", "foo", "foo"], "public": [1, 1, 1] },
  distance_metric='cosine_distance',
)

# Vectors are deleted by ID
ns.delete([1, 3])
© 2024 turbopuffer Inc.
Privacy PolicyTerms of service