Memory
Code
1from kern.agent import Agent2from kern.db.postgres import PostgresDb3from kern.models.ollama.chat import Ollama45# Setup the database6db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"7db = PostgresDb(db_url=db_url)89agent = Agent(10 model=Ollama(id="qwen2.5:latest"),11 # Pass the database to the Agent12 db=db,13 # Enable user memories14 update_memory_on_run=True,15 # Enable session summaries16 enable_session_summaries=True,17 # Show debug logs so, you can see the memory being created18)1920# -*- Share personal information21agent.print_response("My name is john billings", stream=True)2223# -*- Share personal information24agent.print_response("I live in nyc", stream=True)2526# -*- Share personal information27agent.print_response("I'm going to a concert tomorrow", stream=True)2829# Ask about the conversation30agent.print_response(31 "What have we been talking about, do you know my name?", stream=True32)Usage
Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall Ollama
Follow the Ollama installation guide and run:
1ollama pull qwen2.5:latestInstall dependencies
1uv pip install -U ollama kern-ai sqlalchemy psycopg pgvectorRun Agent
1python cookbook/11_models/ollama/memory.py