Agent with Knowledge

Answer questions from a PDF using a WatsonX agent and PgVector.

Code

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.models.ibm import WatsonX
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),
)
# Add content to the knowledge
knowledge.insert(url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf")

agent = Agent(
    model=WatsonX(id="mistralai/mistral-small-3-1-24b-instruct-2503"),
    knowledge=knowledge,
)
agent.print_response("How to make Thai curry?", markdown=True)

Usage

Set up your virtual environment

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

Set your API keys

export IBM_WATSONX_API_KEY=xxx
export IBM_WATSONX_PROJECT_ID=xxx
export OPENAI_API_KEY=***  # Used by the default OpenAIEmbedder

Install dependencies

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

Set up PostgreSQL with pgvector

You need a PostgreSQL database with the pgvector extension installed. Adjust the db_url in the code to match your database configuration.

Run Agent

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

python knowledge.py

For subsequent runs

After the first run, comment out the knowledge.insert(...) line to avoid reloading the PDF.

The example loads a PDF from a URL, processes it into a vector database (PostgreSQL with pgvector), and creates an IBM WatsonX agent that can query this knowledge base.