Back to Mem0

Turbopuffer

docs/components/vectordbs/dbs/turbopuffer.mdx

2.0.123.8 KB
Original Source

Turbopuffer is a serverless vector database optimized for low-latency search at scale. It offers cost-effective vector storage with native metadata filtering.

Usage

<CodeGroup> ```python Python import os from mem0 import Memory

os.environ["OPENAI_API_KEY"] = "sk-xx" os.environ["TURBOPUFFER_API_KEY"] = "tpuf_xxxxxxxxxxxx"

config = { "vector_store": { "provider": "turbopuffer", "config": { "collection_name": "movie_preferences", "embedding_model_dims": 1536, "region": "gcp-us-central1", } } }

m = Memory.from_config(config)

messages = [ {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, {"role": "user", "content": "I'm not a big fan of thrillers but I love sci-fi."}, {"role": "assistant", "content": "Got it! I'll suggest sci-fi movies instead."} ]

m.add(messages, user_id="alice", metadata={"category": "movies"})

Search memories

results = m.search(query="sci-fi recommendations", filters={"user_id": "alice"})


```typescript TypeScript
import { Memory } from "mem0ai/oss";

// Set TURBOPUFFER_API_KEY in your environment, or pass it as config.apiKey below.
const config = {
  vectorStore: {
    provider: "turbopuffer",
    config: {
      collectionName: "movie_preferences",
      region: "gcp-us-central1",
    },
  },
};

const memory = new Memory(config);

const messages = [
  { role: "user", content: "I'm planning to watch a movie tonight. Any recommendations?" },
  { role: "assistant", content: "How about thriller movies? They can be quite engaging." },
  { role: "user", content: "I'm not a big fan of thrillers but I love sci-fi." },
  { role: "assistant", content: "Got it! I'll suggest sci-fi movies instead." },
];

await memory.add(messages, { userId: "alice", metadata: { category: "movies" } });

// Search memories
const results = await memory.search("sci-fi recommendations", { userId: "alice" });
</CodeGroup>

Config

Here are the parameters available for configuring Turbopuffer:

ParameterDescriptionDefault Value
collection_nameName of the namespace/collectionmem0
embedding_model_dimsDimensions of the embedding model (must match your chosen embedding model)1536
api_keyTurbopuffer API keyEnvironment variable: TURBOPUFFER_API_KEY
regionTurbopuffer regiongcp-us-central1
distance_metricDistance metric for vector similarity (cosine_distance or euclidean_squared)cosine_distance
batch_sizeBatch size for bulk operations100
extra_paramsAdditional parameters for the Turbopuffer clientNone
<Note> **TypeScript (Node.js) config keys** are camelCase: `collectionName`, `apiKey`, `region`, `distanceMetric`, and `batchSize`. The TypeScript SDK infers the vector dimension from your embedder, so `embeddingModelDims` is not required. </Note>

Regions

RegionLocation
gcp-us-central1Iowa, USA (Default)
aws-us-west-2Oregon, USA

Config Example

<CodeGroup> ```python Python config = { "vector_store": { "provider": "turbopuffer", "config": { "collection_name": "my_memories", "embedding_model_dims": 1536, "api_key": "tpuf_xxxxxxxxxxxx", "region": "aws-us-west-2", "distance_metric": "cosine_distance", "batch_size": 200, } } } ```
typescript
const config = {
  vectorStore: {
    provider: "turbopuffer",
    config: {
      collectionName: "my_memories",
      apiKey: "tpuf_xxxxxxxxxxxx",
      region: "aws-us-west-2",
      distanceMetric: "cosine_distance",
      batchSize: 200,
    },
  },
};
</CodeGroup>