Agent with Memory

Code

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

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 your API key

1export MISTRAL_API_KEY=xxx

Install dependencies

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

Run Agent

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