Agent with Knowledge

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

Code

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.models.cohere import Cohere
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=Cohere(id="command-a-03-2025"), knowledge=knowledge)
agent.print_response("How to make Thai curry?", markdown=True)

CO_API_KEY authenticates the chat model. The default PgVector embedder uses OpenAI text-embedding-3-small, so inserting and searching this PDF also requires OPENAI_API_KEY.

Usage

Set up your virtual environment

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

Set your API key

export CO_API_KEY=xxx
export OPENAI_API_KEY=xxx

Install dependencies

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

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

Run Agent

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

python knowledge.py