Agent with Memory

Store user memories and session summaries for a Mistral agent in Postgres.

Code

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.mistral.mistral import MistralChat
from agno.tools.websearch import WebSearchTools

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
# Setup the database
db = PostgresDb(db_url=db_url)

agent = Agent(
    model=MistralChat(id="mistral-large-latest"),
    user_id="john",
    session_id="john-conversation",
    tools=[WebSearchTools()],
    # Pass the database to the Agent
    db=db,
    # Enable user memories
    update_memory_on_run=True,
    # Enable session summaries
    enable_session_summaries=True,
)

# -*- Share personal information
agent.print_response("My name is john billings", stream=True)

# -*- Share personal information
agent.print_response("I live in nyc", stream=True)

# -*- Share personal information
agent.print_response("I'm going to a concert tomorrow", stream=True)

# -*- Make tool call
agent.print_response("What is the weather in nyc?", stream=True)

# Ask about the conversation
agent.print_response(
    "What have we been talking about, do you know my name?", stream=True
)

The memory and summary managers use the configured Mistral model. This demo uses explicit user and session IDs; supply your application's actual identities when serving multiple users.

Usage

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Set your API key

export MISTRAL_API_KEY=xxx

Run PgVector

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql \
  -v pgvolume:/var/lib/postgresql \
  -p 5532:5432 \
  --name pgvector \
  agnohq/pgvector:18

Install dependencies

uv pip install -U mistralai ddgs agno sqlalchemy "psycopg[binary]" pgvector

Run Agent

Save the code above as memory.py, then run:

python memory.py