repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | [Supabase](https://supabase.com/) 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. |
| #2 | |
| #3 | Create a [Supabase](https://supabase.com/dashboard/projects) account and project, then get your connection string from Project Settings > Database. See the [docs](https://supabase.github.io/vecs/hosting/) for details. |
| #4 | |
| #5 | ### Usage |
| #6 | |
| #7 | <CodeGroup> |
| #8 | ```python Python |
| #9 | import os |
| #10 | from mem0 import Memory |
| #11 | |
| #12 | os.environ["OPENAI_API_KEY"] = "sk-xx" |
| #13 | |
| #14 | config = { |
| #15 | "vector_store": { |
| #16 | "provider": "supabase", |
| #17 | "config": { |
| #18 | "connection_string": "postgresql://user:password@host:port/database", |
| #19 | "collection_name": "memories", |
| #20 | "index_method": "hnsw", # Optional: defaults to "auto" |
| #21 | "index_measure": "cosine_distance" # Optional: defaults to "cosine_distance" |
| #22 | } |
| #23 | } |
| #24 | } |
| #25 | |
| #26 | m = Memory.from_config(config) |
| #27 | messages = [ |
| #28 | {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, |
| #29 | {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, |
| #30 | {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, |
| #31 | {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} |
| #32 | ] |
| #33 | m.add(messages, user_id="alice", metadata={"category": "movies"}) |
| #34 | ``` |
| #35 | |
| #36 | ```typescript Typescript |
| #37 | import { Memory } from "mem0ai/oss"; |
| #38 | |
| #39 | const config = { |
| #40 | vectorStore: { |
| #41 | provider: "supabase", |
| #42 | config: { |
| #43 | collectionName: "memories", |
| #44 | embeddingModelDims: 1536, |
| #45 | supabaseUrl: process.env.SUPABASE_URL || "", |
| #46 | supabaseKey: process.env.SUPABASE_KEY || "", |
| #47 | tableName: "memories", |
| #48 | }, |
| #49 | }, |
| #50 | } |
| #51 | |
| #52 | const memory = new Memory(config); |
| #53 | |
| #54 | const messages = [ |
| #55 | {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"}, |
| #56 | {"role": "assistant", "content": "How about thriller movies? They can be quite engaging."}, |
| #57 | {"role": "user", "content": "I'm not a big fan of thriller movies but I love sci-fi movies."}, |
| #58 | {"role": "assistant", "content": "Got it! I'll avoid thriller recommendations and suggest sci-fi movies in the future."} |
| #59 | ] |
| #60 | |
| #61 | await memory.add(messages, { userId: "alice", metadata: { category: "movies" } }); |
| #62 | ``` |
| #63 | </CodeGroup> |
| #64 | |
| #65 | ### SQL Migrations for TypeScript Implementation |
| #66 | |
| #67 | The following SQL migrations are required to enable the vector extension and create the memories table: |
| #68 | |
| #69 | ```sql |
| #70 | -- Enable the vector extension |
| #71 | create extension if not exists vector; |
| #72 | |
| #73 | -- Create the memories table |
| #74 | create table if not exists memories ( |
| #75 | id text primary key, |
| #76 | embedding vector(1536), |
| #77 | metadata jsonb, |
| #78 | created_at timestamp with time zone default timezone('utc', now()), |
| #79 | updated_at timestamp with time zone default timezone('utc', now()) |
| #80 | ); |
| #81 | |
| #82 | -- Create the vector similarity search function |
| #83 | create or replace function match_vectors( |
| #84 | query_embedding vector(1536), |
| #85 | match_count int, |
| #86 | filter jsonb default '{}'::jsonb |
| #87 | ) |
| #88 | returns table ( |
| #89 | id text, |
| #90 | similarity float, |
| #91 | metadata jsonb |
| #92 | ) |
| #93 | language plpgsql |
| #94 | as $$ |
| #95 | begin |
| #96 | return query |
| #97 | select |
| #98 | t.id::text, |
| #99 | 1 - (t.embedding <=> query_embedding) as similarity, |
| #100 | t.metadata |
| #101 | from memories t |
| #102 | where case |
| #103 | when filter::text = '{}'::text then true |
| #104 | else t.metadata @> filter |
| #105 | end |
| #106 | order by t.embedding <=> query_embedding |
| #107 | limit match_count; |
| #108 | end; |
| #109 | $$; |
| #110 | ``` |
| #111 | |
| #112 | Go to [Supabase](https://supabase.com/dashboard/projects) and run the above SQL migrations in the SQL Editor. |
| #113 | |
| #114 | ### Config |
| #115 | |
| #116 | Here are the parameters available for configuring Supabase: |
| #117 | |
| #118 | <Tabs> |
| #119 | <Tab title="Python"> |
| #120 | | Parameter | Description | Default Value | |
| #121 | | --- | --- | --- | |
| #122 | | `connection_string` | PostgreSQL connection string (required) | None | |
| #123 | | `collection_name` | Name for the vector collection | `mem0` | |
| #124 | | `embedding_model_dims` | Dimensions of the embedding model | `1536` | |
| #125 | | `index_method` | Vector index method to use | `auto` | |
| #126 | | `index_measure` | Distance measure for similarity search | `cosine_distance` | |
| #127 | </Tab> |
| #128 | <Tab title="TypeScript"> |
| #129 | | Parameter | Description | Default Value | |
| #130 | | --- | --- | --- | |
| #131 | | `collectionName` | Name for the vector collection | `mem0` | |
| #132 | | `embeddingModelDims` | Dimensions of the embedding model | `1536` | |
| #133 | | `supabaseUrl` | Supabase URL | None | |
| #134 | | `supabaseKey` | Supabase key | None | |
| #135 | | `tableName` | Name for the vector table | `memories` | |
| #136 | </Tab> |
| #137 | </Tabs> |
| #138 | |
| #139 | ### Index Methods |
| #140 | |
| #141 | The following index methods are supported: |
| #142 | |
| #143 | - `auto`: Automatically selects the best available index method |
| #144 | - `hnsw`: Hierarchical Navigable Small World graph index (faster search, more memory usage) |
| #145 | - `ivfflat`: Inverted File Flat index (good balance of speed and memory) |
| #146 | |
| #147 | ### Distance Measures |
| #148 | |
| #149 | Available distance measures for similarity search: |
| #150 | |
| #151 | - `cosine_distance`: Cosine similarity (recommended for most embedding models) |
| #152 | - `l2_distance`: Euclidean distance |
| #153 | - `l1_distance`: Manhattan distance |
| #154 | - `max_inner_product`: Maximum inner product similarity |
| #155 | |
| #156 | ### Best Practices |
| #157 | |
| #158 | 1. **Index Method Selection**: |
| #159 | - Use `hnsw` for fastest search performance when memory is not a constraint |
| #160 | - Use `ivfflat` for a good balance of search speed and memory usage |
| #161 | - Use `auto` if unsure, it will select the best method based on your data |
| #162 | |
| #163 | 2. **Distance Measure Selection**: |
| #164 | - Use `cosine_distance` for most embedding models (OpenAI, Hugging Face, etc.) |
| #165 | - Use `max_inner_product` if your vectors are normalized |
| #166 | - Use `l2_distance` or `l1_distance` if working with raw feature vectors |
| #167 | |
| #168 | 3. **Connection String**: |
| #169 | - Always use environment variables for sensitive information in the connection string |
| #170 | - Format: `postgresql://user:password@host:port/database` |
| #171 |