Upstash

Insert a PDF into an Upstash Vector index, query it with an agent, and delete content by name or metadata.

These examples omit an embedder, so create an index with an Upstash hosted embedding model before copying its URL and token. See the Upstash overview for a custom-embedder alternative.

Code

upstash_db.py
import os

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.upstashdb import UpstashVectorDb

# How to connect to an Upstash Vector index
# - Create an index in Upstash Console with a hosted embedding model
# - Fetch the URL and token from Upstash Console
# - Replace the values below or use environment variables

vector_db = UpstashVectorDb(
    url=os.getenv("UPSTASH_VECTOR_REST_URL"),
    token=os.getenv("UPSTASH_VECTOR_REST_TOKEN"),
)

# Create knowledge base
knowledge = Knowledge(
    name="Basic SDK Knowledge Base",
    description="Agno Knowledge with Upstash Vector DB",
    vector_db=vector_db,
)

# Add content with metadata
knowledge.insert(
    name="Recipes",
    url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
    metadata={"doc_type": "recipe_book"},
)

# Create and use the agent
agent = Agent(knowledge=knowledge)
agent.print_response("How to make Pad Thai?", markdown=True)

vector_db.delete_by_name("Recipes")
# or
vector_db.delete_by_metadata({"doc_type": "recipe_book"})

Usage

Set up your virtual environment

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

Install dependencies

uv pip install -U upstash-vector pypdf openai agno

Set environment variables

export UPSTASH_VECTOR_REST_URL="your-upstash-vector-rest-url"
export UPSTASH_VECTOR_REST_TOKEN="your-upstash-vector-rest-token"
export OPENAI_API_KEY=xxx

Run Agent

python upstash_db.py