Skip to content

Commit

Permalink
Python: postgres memory store (microsoft#1354)
Browse files Browse the repository at this point in the history
### Motivation and Context
<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  Brings parity to python implementation
  2. What problem does it solve?
With the pgvector extension, Postgres is a viable option for the storing
of embeddings for memory in Semantic Kernel.
The C# implementation is already capable of this, but the feature has
yet to be added to python.
  3. What scenario does it contribute to?
  Additional options for memory storage
  4. If it fixes an open issue, please link to the issue here.
-->


### Description
Implemention of the MemoryStore to use a Postgresql (with pgvector
extension) to resolve microsoft#1270

Taking a different approach from the .net implementation:
Collections as postgres schemas as opposed to collection being another
column in the table, willing to change to match but would love to have a
discussion comparing the options.




### Contribution Checklist
<!-- Before submitting this PR, please make sure: -->
- [x] The code builds clean without any errors or warnings
- [x] The PR follows SK Contribution Guidelines
(https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [x] The code follows the .NET coding conventions
(https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions)
verified with `dotnet format`
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄

---------

Co-authored-by: Dmytro Struk <[email protected]>
Co-authored-by: Shawn Callegari <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
Co-authored-by: Abby Harrison <[email protected]>
  • Loading branch information
5 people authored Jul 11, 2023
1 parent 36f4e7c commit 88d67de
Show file tree
Hide file tree
Showing 12 changed files with 1,420 additions and 408 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
OpenAI__ApiKey: ${{ secrets.OPENAI__APIKEY }}
Pinecone__ApiKey: ${{ secrets.PINECONE__APIKEY }}
Pinecone__Environment: ${{ secrets.PINECONE__ENVIRONMENT }}
Postgres__Connectionstr: ${{secrets.POSTGRES__CONNECTIONSTR}}
run: |
cd python
poetry run pytest ./tests/integration
6 changes: 3 additions & 3 deletions FEATURE_MATRIX.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
| Math Skill |||| |
| Text Skill ||| 🔄 | |
| Time Skill |||| |
| Wait Skill || || |
| Wait Skill || || |

## Planning

Expand All @@ -63,10 +63,10 @@
| Weaviate |||| Currently supported on Python 3.9+, 3.8 coming soon |
| Chroma |||| |
| Qdrant |||| |
| Pinecone || || |
| Pinecone || || |
| Milvus |||| Coming soon |
| Sqlite |||| Vector optimization requires [sqlite-vss](https://github.com/asg017/sqlite-vss) |
| Postgres || || Vector optimization requires [pgvector](https://github.com/pgvector/pgvector) |
| Postgres || || Vector optimization requires [pgvector](https://github.com/pgvector/pgvector) |
| CosmosDB |||| CosmosDB is not optimized for vector storage |
| Redis |||| Vector optimization requires [RediSearch](https://redis.io/docs/stack/search) |

Expand Down
1 change: 1 addition & 0 deletions python/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ AZURE_OPENAI_ENDPOINT=""
AZURE_OPENAI_API_KEY=""
PINECONE_API_KEY=""
PINECONE_ENVIRONMENT=""
POSTGRES_CONNECTION_STRING=""
982 changes: 595 additions & 387 deletions python/poetry.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ weaviate-client = "^3.18.0"
[tool.poetry.group.pinecone.dependencies]
pinecone-client = "^2.2.2"

[tool.poetry.group.postgres.dependencies]
psycopg-pool = "^3.1.7"
psycopg = "^3.1.9"

[tool.isort]
profile = "black"

Expand Down
2 changes: 2 additions & 0 deletions python/semantic_kernel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
azure_openai_settings_from_dot_env,
openai_settings_from_dot_env,
pinecone_settings_from_dot_env,
postgres_settings_from_dot_env,
)

__all__ = [
"Kernel",
"NullLogger",
"openai_settings_from_dot_env",
"azure_openai_settings_from_dot_env",
"postgres_settings_from_dot_env",
"pinecone_settings_from_dot_env",
"PromptTemplateConfig",
"PromptTemplate",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@


class PineconeMemoryStore(MemoryStoreBase):
"""A memory store that uses Pinecone as the backend."""

_logger: Logger
_pinecone_api_key: str
_pinecone_environment: str
Expand All @@ -42,8 +44,8 @@ def __init__(
Arguments:
pinecone_api_key {str} -- The Pinecone API key.
pinecone_environment {str} -- The Pinecone environment.
logger {Optional[Logger]} -- The logger to use. (default: {None})
default_dimensionality {int} -- The default dimensionality to use for new collections.
logger {Optional[Logger]} -- The logger to use. (default: {None})
"""
if default_dimensionality > MAX_DIMENSIONALITY:
raise ValueError(
Expand Down
7 changes: 7 additions & 0 deletions python/semantic_kernel/connectors/memory/postgres/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) Microsoft. All rights reserved.

from semantic_kernel.connectors.memory.postgres.postgres_memory_store import (
PostgresMemoryStore,
)

__all__ = ["PostgresMemoryStore"]
Loading

0 comments on commit 88d67de

Please sign in to comment.