Skip to content

feat(memory): add self-hosted PostgreSQL/pgvector memory backend (PgvectorMemory) - #8307

Open
Nantha kumar (Nanduu24) wants to merge 1 commit into
microsoft:mainfrom
Nanduu24:feat/pgvector-memory
Open

Nantha kumar (Nanduu24) wants to merge 1 commit into
microsoft:mainfrom
Nanduu24:feat/pgvector-memory

Conversation

@Nanduu24

Copy link
Copy Markdown

Why are these changes needed?

autogen-ext/memory ships chromadb, mem0, redis, and canvas, but none offers semantic vector memory on a general-purpose SQL database the user already controls. Teams running on PostgreSQL that need agent memory on-premise, in air-gapped environments, or under data-residency constraints have no built-in option.

This PR adds PgvectorMemory (autogen_ext.memory.pgvector), a Memory component backed by PostgreSQL with the pgvector extension:

  • add embeds a MemoryContent (TEXT / MARKDOWN / JSON) and stores it, with its vector, in a table the user owns (extension, table, and HNSW index created on first use).
  • query embeds the query and returns the nearest memories by cosine distance via a pgvector HNSW index, with an optional distance_threshold.
  • update_context injects the relevant memories as a SystemMessage, matching the other backends.
  • clear / close behave as expected.
  • Embeds via a local SentenceTransformers model by default (configurable model_name), with an injectable embedding_function override.
  • Ships as the optional extra autogen-ext[pgvector].

It follows the existing redis / chromadb component pattern (a serializable Component config), and the connection pool and embedder are both injectable, which keeps it unit-testable without a live database.

Testing

  • Unit tests in tests/memory/test_pgvector_memory.py run against an in-process fake pool and a deterministic embedder (no live DB, driver calls, or network), covering add/query ranking, the distance threshold, JSON round-trips, update_context, clear, the connection-pool lifecycle, and component config serialization — 9 passing.
  • Verified end-to-end against a live PostgreSQL 17 + pgvector 0.8.6 instance (real psycopg driver): semantic ranking, JSON round-trip, update_context, cross-instance persistence, the vector column + HNSW index, and clear().
  • ruff format, ruff check, and pyright are clean on the changed files.

Related issue number

Feature suggestion: #8306

Checks

  • I've included any doc changes needed. The new component is documented via class/config docstrings with a usage example (consistent with the other memory backends); happy to add an API-reference entry if preferred.
  • I've added tests corresponding to the changes introduced in this PR.
  • I've made sure all auto checks have passed.

Adds PgvectorMemory under autogen_ext.memory.pgvector, a Memory component
backed by PostgreSQL with the pgvector extension. Content is embedded and
stored, with its vector, in a table the user owns; query returns the nearest
memories by cosine distance using a pgvector HNSW index, and update_context
injects them as a system message.

The existing memory backends cover Chroma, mem0, and Redis, but none offers
vector memory on a general-purpose SQL database the user already controls.
PgvectorMemory fills that gap for on-premise, air-gapped, and data-residency
constrained deployments. It follows the redis/chromadb component pattern: a
serializable config plus an injectable connection pool and embedder, which keeps
it unit-testable without a live database. Embeddings use a local
SentenceTransformers model by default (configurable), with an injectable
override. Ships as the optional extra autogen-ext[pgvector].

Includes unit tests that run against a fake pool and a deterministic embedder
(no live database or network), covering add/query ranking, the distance
threshold, JSON round-trips, update_context, clear, the connection-pool
lifecycle, and component config serialization.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant