memory.py
"""
This recipe shows how to use personalized memories and summaries in an agent.
Steps:
1. Run: `./cookbook/scripts/run_pgvector.sh` to start a postgres container with pgvector
2. Run: `uv pip install openai sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/agents/personalized_memories_and_summaries.py` to run the agent
"""
from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.meta import Llama
from rich.pretty import pprint
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
# Setup the database
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
db = PostgresDb(db_url=db_url)
agent = Agent(
model=Llama(id="Llama-4-Maverick-17B-128E-Instruct-FP8"),
user_id="test_user",
session_id="test_session",
# 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)
# -*- Print memories and session summary
if agent.db:
pprint(agent.get_user_memories(user_id="test_user"))
pprint(
agent.get_session(session_id="test_session").summary # type: ignore
)
# -*- Share personal information
agent.print_response("I live in NYC", stream=True)
# -*- Print memories and session summary
if agent.db:
pprint(agent.get_user_memories(user_id="test_user"))
pprint(
agent.get_session(session_id="test_session").summary # type: ignore
)
# Ask about the conversation
agent.print_response(
"What have we been talking about, do you know my name?", stream=True
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
pass
Run the Example
1
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
2
Install dependencies
uv pip install -U agno llama-api-client psycopg-binary sqlalchemy
3
Export your Meta Llama API key
export LLAMA_API_KEY="your_llama_api_key_here"
$Env:LLAMA_API_KEY="your_llama_api_key_here"
4
Run PgVector
docker run -d \
-e POSTGRES_DB=ai \
-e POSTGRES_USER=ai \
-e POSTGRES_PASSWORD=ai \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v pgvolume:/var/lib/postgresql/data \
-p 5532:5432 \
--name pgvector \
agnohq/pgvector:18
5
Run the example
Save the code above as
memory.py, then run:python memory.py