Memory

Persist user memories and session summaries in Postgres for an Ollama qwen2.5 agent across a multi-turn conversation.

Code

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.ollama.chat import Ollama

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

agent = Agent(
    model=Ollama(host="http://localhost:11434", api_key=None, id="qwen2.5:latest"),
    user_id="john",
    session_id="john-conversation",
    # Pass the database to the Agent
    db=db,
    # Enable user memories
    update_memory_on_run=True,
    # Enable session summaries
    enable_session_summaries=True,
    # Show debug logs so, you can see the memory being created
)

# -*- 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)

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

The memory and session-summary managers use the same Ollama model as this agent. The example uses PostgreSQL for storage; vector search is not involved. Reuse the same user and session IDs to resume this conversation.

Usage

Set up your virtual environment

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

Start the local Ollama service

Install Ollama and start its desktop app or service on http://localhost:11434. If you start it manually with ollama serve, keep that process running in a separate terminal.

In the terminal where you will pull models and run Python, select that server and clear the direct-cloud key. The native Ollama client also reads this key independently of Agno.

export OLLAMA_HOST=http://localhost:11434
unset OLLAMA_API_KEY

Pull the model

ollama pull qwen2.5:latest

Install dependencies

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

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

Run Agent

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

python memory.py