The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.
Python agentic-memoryagentic-memory-systemagentsaiai-agentschatgptgenaillmlong-term-memorymemorymemory-managementpythonragstate-management
HNSW support for pgvector (#2139)
Dev Khant7397279872b0
diff --git a/docs/components/vectordbs/dbs/pgvector.mdx b/docs/components/vectordbs/dbs/pgvector.mdxindex 57c4bdeef..d505de009 100644--- a/docs/components/vectordbs/dbs/pgvector.mdx+++ b/docs/components/vectordbs/dbs/pgvector.mdx@@ -37,4 +37,5 @@ Here's the parameters available for configuring pgvector:| `password` | Password to connect to the database | `None` || `host` | The host where the Postgres server is running | `None` || `port` | The port where the Postgres server is running | `None` |-| `diskann` | Whether to use diskann for vector similarity search (requires pgvectorscale) | `True` |\ No newline at end of file+| `diskann` | Whether to use diskann for vector similarity search (requires pgvectorscale) | `True` |+| `hnsw` | Whether to use hnsw for vector similarity search | `False` |\ No newline at end of filediff --git a/mem0/configs/vector_stores/pgvector.py b/mem0/configs/vector_stores/pgvector.pyindex b81ed9859..c9047d5f4 100644--- a/mem0/configs/vector_stores/pgvector.py+++ b/mem0/configs/vector_stores/pgvector.py@@ -12,6 +12,7 @@ class PGVectorConfig(BaseModel):host: Optional[str] = Field(None, description="Database host. Default is localhost")port: Optional[int] = Field(None, description="Database port. Default is 1536")diskann: Optional[bool] = Field(True, description="Use diskann for approximate nearest neighbors search")+ hnsw: Optional[bool] = Field(False, description="Use hnsw for faster search")@model_validator(mode="before")def check_auth_and_connection(cls, values):diff --git a/mem0/vector_stores/pgvector.py b/mem0/vector_stores/pgvector.pyindex c8893e377..989b60df5 100644--- a/mem0/vector_stores/pgvector.py+++ b/mem0/vector_stores/pgvector.py@@ -32,6 +32,7 @@ class PGVector(VectorStoreBase):host,port,diskann,+ hnsw,):"""Initialize the PGVector database.@@ -45,9 +46,11 @@ class PGVector(VectorStoreBase):host (str, optional): Database hostport (int, optional): Database portdiskann (bool, optional): Use DiskANN for faster search+ hnsw (bool, optional): Use HNSW for faster search"""self.collection_name = collection_nameself.use_diskann = diskann+ self.use_hnsw = hnswself.conn = psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)self.cur = self.conn.cursor()@@ -59,11 +62,10 @@ class PGVector(VectorStoreBase):def create_col(self, embedding_model_dims):"""Create a new collection (table in PostgreSQL).- Will also initialize DiskANN index if the extension is installed.+ Will also initialize vector search index if specified.Args:- name (str): Name of the collection.- embedding_model_dims (int, optional): Dimension of the embedding vector.+ embedding_model_dims (int): Dimension of the embedding vector."""self.cur.execute(f"""@@ -82,11 +84,19 @@ class PGVector(VectorStoreBase):# Create DiskANN index if extension is installed for faster searchself.cur.execute(f"""- CREATE INDEX IF NOT EXISTS {self.collection_name}_vector_idx+ CREATE INDEX IF NOT EXISTS {self.collection_name}_diskann_idxON {self.collection_name}USING diskann (vector);""")+ elif self.use_hnsw:+ self.cur.execute(+ f"""+ CREATE INDEX IF NOT EXISTS {self.collection_name}_hnsw_idx+ ON {self.collection_name}+ USING hnsw (vector vector_cosine_ops)+ """+ )self.conn.commit()