Back to Mem0

Supabase

docs/components/vectordbs/dbs/supabase.mdx

2.0.157.1 KB
Original Source

Supabase is an open-source Firebase alternative that provides a PostgreSQL database with pgvector extension for vector similarity search. It offers a powerful and scalable solution for storing and querying vector embeddings.

Create a Supabase account and project, then get your connection string from Project Settings > Database. See the docs for details.

Usage

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

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

config = { "vector_store": { "provider": "supabase", "config": { "connection_string": "postgresql://user:password@host:port/database", "collection_name": "memories", "index_method": "hnsw", # Optional: defaults to "auto" "index_measure": "cosine_distance" # Optional: defaults to "cosine_distance" } } }

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 thriller movies but I love sci-fi movies."}, {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} ] m.add(messages, user_id="alice", metadata={"category": "movies"})


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

const config = {
    vectorStore: {
      provider: "supabase",
      config: {
        collectionName: "memories",
        embeddingModelDims: 1536,
        supabaseUrl: process.env.SUPABASE_URL || "",
        supabaseKey: process.env.SUPABASE_KEY || "",
        tableName: "memories",
      },
    },
}

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 thriller movies but I love sci-fi movies."},
    {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."}
]

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

SQL Migrations for TypeScript Implementation

The following SQL migrations are required to enable the vector extension and create the memories table:

sql
-- Enable the vector extension
create extension if not exists vector;

-- Create the memories table
create table if not exists memories (
  id text primary key,
  embedding vector(1536),
  metadata jsonb,
  created_at timestamp with time zone default timezone('utc', now()),
  updated_at timestamp with time zone default timezone('utc', now())
);

-- Create the vector similarity search function
create or replace function match_vectors(
  query_embedding vector(1536),
  match_count int,
  filter jsonb default '{}'::jsonb
)
returns table (
  id text,
  similarity float,
  metadata jsonb
)
language plpgsql
as $$
begin
  return query
  select
    t.id::text,
    1 - (t.embedding <=> query_embedding) as similarity,
    t.metadata
  from memories t
  where case
    when filter::text = '{}'::text then true
    else t.metadata @> filter
  end
  order by t.embedding <=> query_embedding
  limit match_count;
end;
$$;

Go to Supabase and run the above SQL migrations in the SQL Editor.

Row Level Security

Tables created through the Supabase dashboard have Row Level Security (RLS) enabled by default with no policies attached. With RLS on and no policies, the TypeScript SDK's queries return zero rows with an HTTP 200 (no error is raised), which looks like an empty memory store rather than a permissions problem. If you use the SQL migrations above (via the SQL Editor), RLS is left in its default off state and this does not apply.

If your table has RLS enabled, add policies for the key your app uses (the example below grants full access to the service_role key; scope it down for anon/authenticated keys as needed):

sql
alter table memories enable row level security;

create policy "Allow service role full access to memories"
on memories
for all
to service_role
using (true)
with check (true);

PostgREST Row Limits

Supabase's PostgREST layer caps the number of rows returned by a single request at db-max-rows (1000 by default), for both .select() queries and RPC function calls like match_vectors. Requesting a topK above this limit for search() or list() will not raise an error, results are capped at db-max-rows instead. The TypeScript list() method paginates internally to work around this, but search() cannot since match_vectors has no offset parameter; it logs a warning when it detects a truncated result. Raise db-max-rows in your Supabase project settings if you need more than 1000 results per search.

Config

Here are the parameters available for configuring Supabase:

<Tabs> <Tab title="Python"> | Parameter | Description | Default Value | | --- | --- | --- | | `connection_string` | PostgreSQL connection string (required) | None | | `collection_name` | Name for the vector collection | `mem0` | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | | `index_method` | Vector index method to use | `auto` | | `index_measure` | Distance measure for similarity search | `cosine_distance` | </Tab> <Tab title="TypeScript"> | Parameter | Description | Default Value | | --- | --- | --- | | `collectionName` | Name for the vector collection | `mem0` | | `embeddingModelDims` | Dimensions of the embedding model | `1536` | | `supabaseUrl` | Supabase URL | None | | `supabaseKey` | Supabase key | None | | `tableName` | Name for the vector table | `memories` | </Tab> </Tabs>

Index Methods

The following index methods are supported:

  • auto: Automatically selects the best available index method
  • hnsw: Hierarchical Navigable Small World graph index (faster search, more memory usage)
  • ivfflat: Inverted File Flat index (good balance of speed and memory)

Distance Measures

Available distance measures for similarity search:

  • cosine_distance: Cosine similarity (recommended for most embedding models)
  • l2_distance: Euclidean distance
  • l1_distance: Manhattan distance
  • max_inner_product: Maximum inner product similarity

Best Practices

  1. Index Method Selection:

    • Use hnsw for fastest search performance when memory is not a constraint
    • Use ivfflat for a good balance of search speed and memory usage
    • Use auto if unsure, it will select the best method based on your data
  2. Distance Measure Selection:

    • Use cosine_distance for most embedding models (OpenAI, Hugging Face, etc.)
    • Use max_inner_product if your vectors are normalized
    • Use l2_distance or l1_distance if working with raw feature vectors
  3. Connection String:

    • Always use environment variables for sensitive information in the connection string
    • Format: postgresql://user:password@host:port/database