Agent with Knowledge

Give a Cerebras agent knowledge from a PDF stored in PgVector.

Code

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.models.cerebras import Cerebras
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=Cerebras(id="gpt-oss-120b"), 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 key

export CEREBRAS_API_KEY=xxx
export OPENAI_API_KEY=xxx

Install dependencies

uv pip install -U agno openai sqlalchemy psycopg pgvector pypdf cerebras-cloud-sdk

Start your PostgreSQL server with pgvector installed and a database role allowed to create the `vector` extension

Ensure your PostgreSQL server with pgvector installed and a database role allowed to create the vector extension is running and accessible at the connection string used in db_url.

Run Agent (first time)

The first run will load and index the PDF. This may take a while. Save the code above as knowledge.py, then run:

python knowledge.py

Subsequent Runs

After the first run, comment out or remove the knowledge.insert(...) call to avoid reloading the PDF each time.