Traditional RAG with PgVector

Retrieve from PgVector for a string input and append references before the first model call.

For a string-input run, set add_knowledge_to_context=True to retrieve before the first model call.

from agno.agent import Agent
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.models.openai import OpenAIResponses
from agno.vectordb.pgvector import PgVector, SearchType

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge = Knowledge(
    vector_db=PgVector(
        table_name="recipes",
        db_url=db_url,
        search_type=SearchType.hybrid,
        embedder=OpenAIEmbedder(id="text-embedding-3-small"),
    ),
)

agent = Agent(
    model=OpenAIResponses(id="gpt-5.2"),
    knowledge=knowledge,
    add_knowledge_to_context=True,
    search_knowledge=False,
    markdown=True,
)

if __name__ == "__main__":
    knowledge.insert(
        name="Thai Recipes",
        url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
    )
    agent.print_response(
        "How do I make chicken and galangal in coconut milk soup?",
        stream=True,
    )

Run the Agent

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

Set up your virtual environment

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

Install dependencies

uv pip install -U agno openai pgvector psycopg pypdf sqlalchemy

Export the API key

export OPENAI_API_KEY=your_openai_api_key_here

Run the agent

python traditional_rag_pgvector.py

How It Works

  1. add_knowledge_to_context=True searches with the run's string input.
  2. Returned documents are appended to the user message inside a <references> block.
  3. search_knowledge=False removes the model-callable search tool.

Next Steps

TaskGuide
Let the model choose when to searchAgentic RAG with PgVector
Tune hybrid rankingHybrid Search
Apply metadata filtersFiltering