Agent with Memory

Code

1from kern.agent import Agent
2from kern.db.postgres import PostgresDb
3from kern.models.vllm import VLLM
4from kern.utils.pprint import pprint
5
6# Change this if your Postgres container is running elsewhere
7DB_URL = "postgresql+psycopg://ai:ai@localhost:5532/ai"
8
9agent = Agent(
10 model=VLLM(id="microsoft/Phi-3-mini-128k-instruct"),
11 db=PostgresDb(db_url=DB_URL),
12 update_memory_on_run=True,
13 enable_session_summaries=True,
14)
15
16# -*- Share personal information
17agent.print_response("My name is john billings", stream=True)
18# -*- Print memories and summary
19if agent.db:
20 pprint(agent.get_user_memories(user_id="test_user"))
21 pprint(
22 agent.get_session(session_id="test_session").summary # type: ignore
23 )
24
25# -*- Share personal information
26agent.print_response("I live in nyc", stream=True)
27# -*- Print memories and summary
28if agent.db:
29 pprint(agent.get_user_memories(user_id="test_user"))
30 pprint(
31 agent.get_session(session_id="test_session").summary # type: ignore
32 )
33
34# -*- Share personal information
35agent.print_response("I'm going to a concert tomorrow", stream=True)
36# -*- Print memories and summary
37if agent.db:
38 pprint(agent.get_user_memories(user_id="test_user"))
39 pprint(
40 agent.get_session(session_id="test_session").summary # type: ignore
41 )
42
43# Ask about the conversation
44agent.print_response(
45 "What have we been talking about, do you know my name?", stream=True
46)
Note

Ensure Postgres database is running.

Usage

Set up your virtual environment

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

Start Postgres database

1./cookbook/scripts/run_pgvector.sh

Install Libraries

1uv pip install -U kern-ai openai vllm sqlalchemy psycopg pgvector

Start vLLM server

1vllm serve microsoft/Phi-3-mini-128k-instruct \
2 --dtype float32 \
3 --enable-auto-tool-choice \
4 --tool-call-parser pythonic

Run Agent

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