Code
cookbook/knowledge/vector_db/weaviate_db/weaviate_db.py
Copy
Ask AI
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.search import SearchType
from agno.vectordb.weaviate import Weaviate
from agno.vectordb.weaviate.index import Distance, VectorIndex
vector_db = Weaviate(
collection="vectors",
search_type=SearchType.vector,
vector_index=VectorIndex.HNSW,
distance=Distance.COSINE,
local=False, # Set to True if using Weaviate locally
)
# Create Knowledge Instance with Weaviate
knowledge = Knowledge(
name="Basic SDK Knowledge Base",
description="Agno 2.0 Knowledge Implementation with Weaviate",
vector_db=vector_db,
)
knowledge.add_content(
name="Recipes",
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
metadata={"doc_type": "recipe_book"},
skip_if_exists=True,
)
# Create and use the agent
agent = Agent(knowledge=knowledge)
agent.print_response("List down the ingredients to make Massaman Gai", markdown=True)
# Delete operations
vector_db.delete_by_name("Recipes")
# or
vector_db.delete_by_metadata({"doc_type": "recipe_book"})
Usage
1
Create a virtual environment
Open the
Terminal and create a python virtual environment.Copy
Ask AI
python3 -m venv .venv
source .venv/bin/activate
2
Install libraries
Copy
Ask AI
pip install -U weaviate-client pypdf openai agno
3
Setup Weaviate
Copy
Ask AI
# 1. Create account at https://console.weaviate.cloud/
# 2. Create a cluster and copy the "REST endpoint" and "Admin" API Key
# 3. Set environment variables:
export WCD_URL="your-cluster-url"
export WCD_API_KEY="your-api-key"
# 4. Set local=False in the code
4
Set environment variables
Copy
Ask AI
export OPENAI_API_KEY=xxx
5
Run Agent
Copy
Ask AI
python cookbook/knowledge/vector_db/weaviate_db/weaviate_db.py