pgvector is open-source vector similarity search for Postgres. After connecting with postgres run CREATE EXTENSION IF NOT EXISTS vector; to create the vector extension.

Usage

import os
from mem0 import Memory

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

config = {
    "vector_store": {
        "provider": "pgvector",
        "config": {
            "user": "test",
            "password": "123",
            "host": "127.0.0.1",
            "port": "5432",
        }
    }
}

m = Memory.from_config(config)
messages = [
    {"role": "user", "content": "I'm planning to watch a movie tonight. Any recommendations?"},
    {"role": "assistant", "content": "How about a 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"})

Config

Here’s the parameters available for configuring pgvector:
ParameterDescriptionDefault Value
dbnameThe name of the databasepostgres
collection_nameThe name of the collectionmem0
embedding_model_dimsDimensions of the embedding model1536
userUser name to connect to the databaseNone
passwordPassword to connect to the databaseNone
hostThe host where the Postgres server is runningNone
portThe port where the Postgres server is runningNone
diskannWhether to use diskann for vector similarity search (requires pgvectorscale)True
hnswWhether to use hnsw for vector similarity searchFalse
sslmodeSSL mode for PostgreSQL connection (e.g., ‘require’, ‘prefer’, ‘disable’)None
connection_stringPostgreSQL connection string (overrides individual connection parameters)None
connection_poolpsycopg2 connection pool object (overrides connection string and individual parameters)None
Note: The connection parameters have the following priority:
  1. connection_pool (highest priority)
  2. connection_string
  3. Individual connection parameters (user, password, host, port, sslmode)