Open sophialagerkranspandey opened 1 week ago
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
db_params = { 'dbname': 'your_database_name', 'user': 'your_username', 'password': 'your_password', 'host': 'your_host', 'port': 'your_port' }
connection = psycopg2.connect(**db_params)
memory_store = PostgresMemoryStore(connection)
kernel = Kernel(memory_store=memory_store)
memory_store.store('some_key', 'some_value')
value = memory_store.retrieve('some_key') print(f'Retrieved value: {value}')
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.
Discussed in https://github.com/microsoft/semantic-kernel/discussions/6988