Memory with PostgreSQL

Code

1from kern.agent import Agent
2from kern.db.postgres import PostgresDb
3
4# Setup Postgres
5db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
6db = PostgresDb(db_url=db_url)
7
8agent = Agent(
9 db=db,
10 update_memory_on_run=True,
11)
12
13agent.print_response("My name is John Doe and I like to play basketball on the weekends.")
14agent.print_response("What's do I do in weekends?")

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Set environment variables

1export OPENAI_API_KEY=xxx

Install dependencies

1uv pip install -U kern-ai openai sqlalchemy 'psycopg[binary]'

Run Example

1python mem-postgres-memory.py
1python mem-postgres-memory.py