Skip to main content
agents.py
"""
Learning Demo: Shared Agent
===========================
A single ops assistant with every learning store enabled:

- User Profile: structured fields (name, role, preferences)
- User Memory: unstructured observations about the user
- Session Context: a running summary of each session
- Entity Memory: facts, events, and relationships about external things
- Learned Knowledge: insights that transfer across users (pgvector)
- Decision Log: significant decisions with reasoning

Requires the pgvector container:
    ./cookbook/scripts/run_pgvector.sh
"""

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.knowledge import Knowledge
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.learn import (
    LearningMachine,
)
from agno.models.openai import OpenAIResponses
from agno.vectordb.pgvector import PgVector, SearchType

# ---------------------------------------------------------------------------
# Database
# ---------------------------------------------------------------------------
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
db = PostgresDb(id="learning-demo-db", db_url=db_url)

# Learned Knowledge needs a vector store for semantic search.
knowledge = Knowledge(
    vector_db=PgVector(
        db_url=db_url,
        table_name="learning_demo_knowledge",
        search_type=SearchType.hybrid,
        embedder=OpenAIEmbedder(id="text-embedding-3-small"),
    ),
)

# ---------------------------------------------------------------------------
# Learning Machine: all six stores enabled
# ---------------------------------------------------------------------------
learning = LearningMachine(
    db=db,
    model=OpenAIResponses(id="gpt-5.5"),
    knowledge=knowledge,
    user_profile=True,
    user_memory=True,
    session_context=True,
    entity_memory=True,
    learned_knowledge=True,
    decision_log=True,
)

# ---------------------------------------------------------------------------
# Agent
# ---------------------------------------------------------------------------
ops_assistant = Agent(
    id="ops-assistant",
    name="Ops Assistant",
    model=OpenAIResponses(id="gpt-5.5"),
    db=db,
    learning=learning,
    instructions=[
        "You are an engineering operations assistant.",
        "Keep answers short and practical.",
        "Search your learnings before answering substantive questions.",
        "When the user shares a team-wide insight or asks you to remember one, save it with the save_learning tool.",
        "When you make a significant recommendation, record it with the log_decision tool, including your reasoning and the alternatives you considered.",
    ],
    markdown=True,
)

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 openai pgvector psycopg-binary sqlalchemy
3

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"
$Env:OPENAI_API_KEY="your_openai_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 agents.py, then run:
python agents.py
Full source: cookbook/08_learning/10_demo/agents.py