DB

Store local Ollama sessions in Postgres and include earlier turns.

db.py
"""Run `uv pip install ddgs sqlalchemy ollama` to install dependencies."""

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.ollama import Ollama
from agno.tools.websearch import WebSearchTools

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

# Setup the database
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
db = PostgresDb(db_url=db_url)

agent = Agent(
    model=Ollama(id="llama3.1:8b"),
    db=db,
    tools=[WebSearchTools()],
    add_history_to_context=True,
)
agent.print_response("How many people live in Canada?")
agent.print_response("What is their national anthem called?")

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass

Run the Example

Set up your virtual environment

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

Install dependencies

uv pip install -U agno "psycopg[binary]" ddgs ollama sqlalchemy

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

Select the local Ollama server

In the shell used for the pull commands and Python example, clear a previous cloud key and point native clients and embeddings at your local server:

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

Without this reset, OLLAMA_API_KEY makes Agno's default Ollama model route to https://ollama.com even when OLLAMA_HOST points locally. Keep a local Ollama server running for the following steps.

Prepare Ollama

Install and start Ollama, then pull the model used by this example:

ollama pull llama3.1:8b

Run the example

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

python db.py

Full source: cookbook/90_models/ollama/chat/db.py