Agent with Knowledge

Give an LMStudio agent a PgVector knowledge base loaded from a Thai recipes PDF and answer questions with markdown output.

Code

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

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

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 agno openai pgvector pypdf "psycopg[binary]" 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

Export the OpenAI API key

The knowledge base uses Agno's default OpenAI embedder.

export OPENAI_API_KEY=your_openai_api_key_here

Save the example

Save the code above as lmstudio_knowledge.py.

Run Agent

python lmstudio_knowledge.py