Memory

Code

1from kern.agent import Agent
2from kern.db.postgres import PostgresDb
3from kern.models.ollama.chat import Ollama
4
5# Setup the database
6db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
7db = PostgresDb(db_url=db_url)
8
9agent = Agent(
10 model=Ollama(id="qwen2.5:latest"),
11 # Pass the database to the Agent
12 db=db,
13 # Enable user memories
14 update_memory_on_run=True,
15 # Enable session summaries
16 enable_session_summaries=True,
17 # Show debug logs so, you can see the memory being created
18)
19
20# -*- Share personal information
21agent.print_response("My name is john billings", stream=True)
22
23# -*- Share personal information
24agent.print_response("I live in nyc", stream=True)
25
26# -*- Share personal information
27agent.print_response("I'm going to a concert tomorrow", stream=True)
28
29# Ask about the conversation
30agent.print_response(
31 "What have we been talking about, do you know my name?", stream=True
32)

Usage

Set up your virtual environment

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

Install Ollama

Follow the Ollama installation guide and run:

1ollama pull qwen2.5:latest

Install dependencies

1uv pip install -U ollama kern-ai sqlalchemy psycopg pgvector

Run Agent

1python cookbook/11_models/ollama/memory.py