Code

cookbook/models/ibm/watsonx/knowledge.py
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.models.ibm import WatsonX
from agno.vectordb.pgvector import PgVector

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

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=PgVector(table_name="recipes", db_url=db_url),
)
knowledge_base.load(recreate=True)  # Comment out after first run

agent = Agent(
    model=WatsonX(id="ibm/granite-20b-code-instruct"),
    knowledge=knowledge_base,
    show_tool_calls=True,
)
agent.print_response("How to make Thai curry?", markdown=True)

Usage

1

Create a virtual environment

Open the Terminal and create a python virtual environment.

python3 -m venv .venv
source .venv/bin/activate
2

Set your API key

export IBM_WATSONX_API_KEY=xxx
export IBM_WATSONX_PROJECT_ID=xxx
3

Install libraries

pip install -U ibm-watsonx-ai sqlalchemy pgvector psycopg pypdf openai agno
4

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.

5

Run Agent

python cookbook/models/ibm/watsonx/knowledge.py
6

For subsequent runs

After the first run, comment out the knowledge_base.load(recreate=True) line to avoid reloading the PDF.

This example shows how to integrate a knowledge base with IBM WatsonX. It loads a PDF from a URL, processes it into a vector database (PostgreSQL with pgvector in this case), and then creates an agent that can query this knowledge base.

Note: You need to install several packages (pgvector, pypdf, etc.) and have a PostgreSQL database with the pgvector extension available.