Memory
Store personalized memories and session summaries with Cohere Command A and PostgreSQL.
"""
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 cohere sqlalchemy 'psycopg[binary]' pgvector` to install the dependencies
3. Run: `python cookbook/90_models/cohere/memory.py` to run the agent
"""
from agno.agent.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.cohere import Cohere
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
agent = Agent(
model=Cohere(id="command-a-03-2025"),
# Store agent sessions in a database
db=PostgresDb(db_url=db_url),
update_memory_on_run=True,
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)
# 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__":
passRun the Example
Use the standalone steps below; the source’s repository-relative commands assume a cookbook checkout. This single-user demo uses the default user bucket for memories. Pass a stable authenticated user_id for each application user and reuse session_id to resume a conversation after a restart.
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno "psycopg[binary]" cohere sqlalchemyExport your Cohere API key
export CO_API_KEY="your_co_api_key_here"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:18Run the example
Save the code above as memory.py, then run:
python memory.pyFull source: cookbook/90_models/cohere/memory.py