Knowledge

Load a PDF of Thai recipes into a PgVector knowledge base with OllamaEmbedder and query it through an Ollama llama3.2 agent.

Code

from agno.agent import Agent
from agno.knowledge.embedder.ollama import OllamaEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.models.ollama import Ollama
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=OllamaEmbedder(id="llama3.2", dimensions=3072, host="http://localhost:11434"),
    ),
)
# Add content to the knowledge
knowledge.insert(
    url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
)

agent = Agent(model=Ollama(host="http://localhost:11434", api_key=None, id="llama3.2"), knowledge=knowledge)
agent.print_response("How to make Thai curry?", markdown=True)

The Docker step below creates the PostgreSQL database used by PgVector. If using an existing server, enable the vector extension and give the connection role permission to create the required schema and tables. Chat and embedding requests both use the local Ollama server.

Usage

Set up your virtual environment

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

Start the local Ollama service

Install Ollama and start its desktop app or service on http://localhost:11434. If you start it manually with ollama serve, keep that process running in a separate terminal.

In the terminal where you will pull models and run Python, select that server and clear the direct-cloud key. The native Ollama client also reads this key independently of Agno.

export OLLAMA_HOST=http://localhost:11434
unset OLLAMA_API_KEY

Pull the model

ollama pull llama3.2

Install dependencies

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

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