Agent with Storage

Persist an LMStudio agent's chat history to PostgreSQL and give it web search tools with add_history_to_context enabled.

Code

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.models.lmstudio import LMStudio
from agno.tools.websearch import WebSearchTools

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

agent = Agent(
    model=LMStudio(id="qwen2.5-7b-instruct-1m"),
    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?")

Load a model that supports tool calling in LM Studio.

Usage

Set up your virtual environment

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

Start the LM Studio API server

Install LM Studio, download and load a model, then open Developer and start the API server on port 1234. Keep the server running while you run Python in a terminal.

Check the model list:

curl http://127.0.0.1:1234/v1/models

Set LMStudio(id=...) in the example to the exact model id returned by your server. If you change the server port, set the matching base_url on LMStudio.

Install dependencies

uv pip install -U ddgs sqlalchemy "psycopg[binary]" 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 db.py, then run:

python db.py