Upstash Async

Load and query an Upstash Vector knowledge base asynchronously with ainsert() and aprint_response().

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

async_upstash_db.py
import asyncio
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,
)

# Create the agent
agent = Agent(knowledge=knowledge)

if __name__ == "__main__":
    # Comment out after first run
    asyncio.run(
        knowledge.ainsert(
            name="Recipes",
            url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
            metadata={"doc_type": "recipe_book"},
        )
    )

    asyncio.run(
        agent.aprint_response("How to make Pad Thai?", markdown=True)
    )

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 async_upstash_db.py