Agent with Knowledge

Give a Perplexity-powered Agno agent knowledge from a PDF stored in PgVector.

Sonar's built-in web search is separate from Agno function tools. The current Perplexity adapter does not forward Agno tool definitions. Use eager knowledge retrieval with add_knowledge_to_context=True and search_knowledge=False, and an explicit tool-capable MemoryManager model for memory extraction.

This recipe retrieves relevant PDF passages before generation and adds them to the model context. Sonar's own web search does not search your PgVector database.

Code

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

agent = Agent(
    model=Perplexity(id="sonar-pro"),
    knowledge=knowledge,
    add_knowledge_to_context=True,
    search_knowledge=False,
)
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

Perplexity handles chat. OpenAI handles PDF embeddings.

export PERPLEXITY_API_KEY="YOUR_PERPLEXITY_API_KEY"
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"

Install dependencies

uv pip install -U agno openai 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 \
  -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