Agent with Knowledge Base

Answer questions from a PDF using an AzureOpenAI agent and PgVector.

Code

import asyncio

from agno.agent import Agent
from agno.knowledge.embedder.azure_openai import AzureOpenAIEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.models.azure import AzureOpenAI
from agno.vectordb.pgvector import PgVector

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

knowledge = Knowledge(
    vector_db=PgVector(
        table_name="recipes",
        db_url=db_url,
        embedder=AzureOpenAIEmbedder(),
    ),
)
# Add content to the knowledge
asyncio.run(
    knowledge.ainsert(
        url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
    )
)

agent = Agent(
    model=AzureOpenAI(id="gpt-5.2"),
    knowledge=knowledge,
)
agent.print_response("How to make Thai curry?", markdown=True)

Usage

The AzureOpenAIEmbedder() example also needs a separate text-embedding-3-small deployment (1536 dimensions). Set AZURE_EMBEDDER_DEPLOYMENT to that deployment name, with its own resource endpoint and key in the AZURE_EMBEDDER_OPENAI_* variables, before starting Python. These settings are independent of the chat deployment.

Create or select a gpt-5.2 chat deployment. Set AZURE_OPENAI_DEPLOYMENT to its actual deployment name, and use the key and resource endpoint for that deployment. You may omit the deployment setting only when an existing deployment is named exactly like id. Keep id aligned with the deployed model family.

Set up your virtual environment

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

Set your API key

export AZURE_OPENAI_API_KEY=xxx
export AZURE_OPENAI_ENDPOINT=xxx
export AZURE_OPENAI_DEPLOYMENT="your_chat_deployment"
export AZURE_EMBEDDER_OPENAI_API_KEY=xxx
export AZURE_EMBEDDER_OPENAI_ENDPOINT=xxx
export AZURE_EMBEDDER_DEPLOYMENT=xxx

Install dependencies

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

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

Run Agent

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

python knowledge.py