Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: SK memory connector to postgres #7000

Open
sophialagerkranspandey opened this issue Jun 28, 2024 Discussed in #6988 · 1 comment
Open

Python: SK memory connector to postgres #7000

sophialagerkranspandey opened this issue Jun 28, 2024 Discussed in #6988 · 1 comment
Labels
bug Something isn't working python Pull requests for the Python Semantic Kernel

Comments

@sophialagerkranspandey
Copy link
Contributor

Discussed in #6988

Originally posted by jamesmkfoo23 June 27, 2024
Hi, is postgres supported with Python?

Azure docs for Azure PostgreSQL Flexible python shows the usage psycopg2
https://learn.microsoft.com/en-us/azure/postgresql/flexible-server/connect-python?tabs=cmd%2Cpasswordless#create-a-table-and-insert-data . I believe therefore this should be the correct library psycopg2.

Trying to use the connector but i'm getting this error when i try to import the postgres memory store

from semantic_kernel.connectors.memory.postgres import PostgresMemoryStore

semantic_kernel\connectors\memory\postgres\__init__.py:3
  from semantic_kernel.connectors.memory.postgres.postgres_memory_store import (PostgresMemoryStore)

semantic_kernel\connectors\memory\postgres\postgres_memory_store.py:9
  from psycopg import Cursor
  
ModuleNotFoundError: No module named 'psycopg'

Is there any samples for SK pg memory connector?

@sophialagerkranspandey sophialagerkranspandey added the python Pull requests for the Python Semantic Kernel label Jun 28, 2024
@github-actions github-actions bot changed the title SK memory connector to postgres Python: SK memory connector to postgres Jun 28, 2024
@MaheshBudarapu
Copy link

Install Dependencies:
Make sure you have the required dependencies installed:
pip install psycopg
pip install semantic-kernel

from semantic_kernel import Kernel
from semantic_kernel.connectors.memory.postgres import PostgresMemoryStore
import psycopg2

Database connection parameters

db_params = {
'dbname': 'your_database_name',
'user': 'your_username',
'password': 'your_password',
'host': 'your_host',
'port': 'your_port'
}

Create a connection to the database

connection = psycopg2.connect(**db_params)

Initialize the PostgresMemoryStore

memory_store = PostgresMemoryStore(connection)

Initialize the Kernel with the memory store

kernel = Kernel(memory_store=memory_store)

Example: Using the memory store

Store some data

memory_store.store('some_key', 'some_value')

Retrieve stored data

value = memory_store.retrieve('some_key')
print(f'Retrieved value: {value}')

Close the connection when done

connection.close()

Explanation:
Install Dependencies: Ensure you have the psycopg and semantic-kernel packages installed.
Database Connection Parameters: Update the db_params dictionary with your PostgreSQL database credentials.
Create Connection: Establish a connection to the PostgreSQL database using psycopg2.connect.
Initialize PostgresMemoryStore: Create an instance of PostgresMemoryStore using the database connection.
Initialize Kernel: Pass the memory store to the Kernel initializer.
Example Usage: Demonstrates storing and retrieving data from the memory store.
Close Connection: Ensure the database connection is closed after operations are completed.

@markwallace-microsoft markwallace-microsoft added bug Something isn't working and removed triage labels Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Pull requests for the Python Semantic Kernel
Projects
Status: Backlog: Planned
Development

No branches or pull requests

3 participants