Upstash Async

Code

1import asyncio
2import os
3
4from kern.agent import Agent
5from kern.knowledge.knowledge import Knowledge
6from kern.vectordb.upstashdb import UpstashVectorDb
7
8# How to connect to an Upstash Vector index
9# - Create a new index in Upstash Console with the correct dimension
10# - Fetch the URL and token from Upstash Console
11# - Replace the values below or use environment variables
12
13vector_db = UpstashVectorDb(
14 url=os.getenv("UPSTASH_VECTOR_REST_URL"),
15 token=os.getenv("UPSTASH_VECTOR_REST_TOKEN"),
16)
17
18# Initialize Upstash DB
19knowledge = Knowledge(
20 name="Basic SDK Knowledge Base",
21 description="Kern 2.0 Knowledge Implementation with Upstash Vector DB",
22 vector_db=vector_db,
23)
24
25# Create and use the agent
26agent = Agent(knowledge=knowledge)
27
28if __name__ == "__main__":
29 # Comment out after first run
30 asyncio.run(
31 knowledge.ainsert(
32 name="Recipes",
33 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
34 metadata={"doc_type": "recipe_book"},
35 )
36 )
37
38 # Create and use the agent
39 asyncio.run(
40 agent.aprint_response("How to make Pad Thai?", markdown=True)
41 )

Usage

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U upstash-vector pypdf openai kern-ai

Set environment variables

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

Run Agent

1python cookbook/08_knowledge/vector_db/upstash_db/upstash_db.py