Knowledge
Answer questions from a PDF knowledge base in PgVector using a Perplexity sonar-pro agent.
This adapter does not forward local function tools, including Agno's automatic knowledge-search tool. Apply the context settings below to retrieve PDF content before the model request. Perplexity's native web search is a separate provider feature.
"""Run `uv pip install ddgs sqlalchemy pgvector pypdf openai google.generativeai` to install dependencies."""
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
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
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)
agent.print_response("How to make Thai curry?", markdown=True)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
passRun the Example
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U agno "psycopg[binary]" beautifulsoup4 openai pgvector pypdf sqlalchemyExport your API keys
export OPENAI_API_KEY="your_openai_api_key_here"
export PERPLEXITY_API_KEY="your_perplexity_api_key_here"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:18Retrieve knowledge before generating
Add add_knowledge_to_context=True, search_knowledge=False to Agent(...). Agno will query the configured knowledge base locally and inject its results into the model context. Retrieval failures are logged and can leave the request without PDF context; check ingestion and retrieval before relying on an answer.
Run the example
Save the code above as knowledge.py, then run:
python knowledge.pyFull source: cookbook/90_models/perplexity/knowledge.py