Upstash

Code

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

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