Weaviate Async

Code

1import asyncio
2
3from kern.agent import Agent
4from kern.knowledge.knowledge import Knowledge
5from kern.vectordb.search import SearchType
6from kern.vectordb.weaviate import Distance, VectorIndex, Weaviate
7
8vector_db = Weaviate(
9 collection="recipes_async",
10 search_type=SearchType.hybrid,
11 vector_index=VectorIndex.HNSW,
12 distance=Distance.COSINE,
13 local=True, # Set to False if using Weaviate Cloud and True if using local instance
14)
15# Create knowledge base
16knowledge = Knowledge(
17 vector_db=vector_db,
18)
19
20agent = Agent(
21 knowledge=knowledge,
22 search_knowledge=True,
23)
24
25if __name__ == "__main__":
26 # Comment out after first run
27 asyncio.run(
28 knowledge.ainsert(
29 name="Recipes",
30 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
31 )
32 )
33
34 # Create and use the agent
35 asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))

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 weaviate-client pypdf openai kern-ai

Set environment variables

1export OPENAI_API_KEY=xxx

Setup Weaviate

1# 1. Create account at https://console.weaviate.cloud/
2# 2. Create a cluster and copy the "REST endpoint" and "Admin" API Key
3# 3. Set environment variables:
4export WCD_URL="your-cluster-url"
5export WCD_API_KEY="your-api-key"
6# 4. Set local=False in the code
1# 1. Install Docker from https://docs.docker.com/get-docker/
2# 2. Run Weaviate locally:
3docker run -d \
4 -p 8080:8080 \
5 -p 50051:50051 \
6 --name weaviate \
7 cr.weaviate.io/semitechnologies/weaviate:1.28.4
8# 3. Set local=True in the code

Run Agent

1python cookbook/08_knowledge/vector_db/weaviate_db/async_weaviate_db.py