CrewAI智能体开发:Weaviate 向量搜索
WeaviateVectorSearchTool 旨在通过混合搜索在 Weaviate 向量数据库中搜索语义相似的文档。
概述
WeaviateVectorSearchTool 专门用于在存储在 Weaviate 向量数据库中的文档中进行语义搜索。此工具允许您查找与给定查询语义相似的文档,利用向量和关键词搜索的强大功能,从而获得更准确、更具上下文相关性的搜索结果。Weaviate 是一个向量数据库,用于存储和查询向量嵌入,从而实现语义搜索功能。
安装要将此工具集成到您的项目中,您需要安装 Weaviate 客户端
uv add weaviate-client
开始步骤要有效使用 WeaviateVectorSearchTool,请遵循以下步骤
- 软件包安装:确认您的 Python 环境中已安装
crewai[tools]和weaviate-client软件包。 - Weaviate 设置:设置 Weaviate 集群。您可以按照 Weaviate 文档 中的说明进行操作。
- API 密钥:获取您的 Weaviate 集群 URL 和 API 密钥。
- OpenAI API 密钥:确保您已在环境变量中设置了 OpenAI API 密钥,名为
OPENAI_API_KEY。
示例以下示例演示了如何初始化工具并执行搜索
from crewai_tools import WeaviateVectorSearchTool
# Initialize the tool
tool = WeaviateVectorSearchTool(
collection_name='example_collections',
limit=3,
alpha=0.75,
weaviate_cluster_url="https://your-weaviate-cluster-url.com",
weaviate_api_key="your-weaviate-api-key",
)
@agent
def search_agent(self) -> Agent:
'''
This agent uses the WeaviateVectorSearchTool to search for
semantically similar documents in a Weaviate vector database.
'''
return Agent(
config=self.agents_config["search_agent"],
tools=[tool]
)
参数WeaviateVectorSearchTool 接受以下参数
- collection_name:必填。要搜索的集合的名称。
- weaviate_cluster_url:必填。Weaviate 集群的 URL。
- weaviate_api_key:必填。Weaviate 集群的 API 密钥。
- limit:可选。返回结果的数量。默认为
3。 - alpha:可选。控制向量和关键词(BM25)搜索之间的权重。alpha = 0 -> 仅 BM25,alpha = 1 -> 仅向量搜索。默认为
0.75。 - vectorizer:可选。要使用的向量化器。如果未提供,将使用
text2vec_openai和nomic-embed-text模型。 - generative_model:可选。要使用的生成模型。如果未提供,将使用 OpenAI 的
gpt-4o。
高级配置您可以自定义工具使用的向量化器和生成模型
from crewai_tools import WeaviateVectorSearchTool
from weaviate.classes.config import Configure
# Setup custom model for vectorizer and generative model
tool = WeaviateVectorSearchTool(
collection_name='example_collections',
limit=3,
alpha=0.75,
vectorizer=Configure.Vectorizer.text2vec_openai(model="nomic-embed-text"),
generative_model=Configure.Generative.openai(model="gpt-4o-mini"),
weaviate_cluster_url="https://your-weaviate-cluster-url.com",
weaviate_api_key="your-weaviate-api-key",
)
预加载文档您可以在使用工具之前将文档预加载到 Weaviate 数据库中
import os
from crewai_tools import WeaviateVectorSearchTool
import weaviate
from weaviate.classes.init import Auth
# Connect to Weaviate
client = weaviate.connect_to_weaviate_cloud(
cluster_url="https://your-weaviate-cluster-url.com",
auth_credentials=Auth.api_key("your-weaviate-api-key"),
headers={"X-OpenAI-Api-Key": "your-openai-api-key"}
)
# Get or create collection
test_docs = client.collections.get("example_collections")
if not test_docs:
test_docs = client.collections.create(
name="example_collections",
vectorizer_config=Configure.Vectorizer.text2vec_openai(model="nomic-embed-text"),
generative_config=Configure.Generative.openai(model="gpt-4o"),
)
# Load documents
docs_to_load = os.listdir("knowledge")
with test_docs.batch.dynamic() as batch:
for d in docs_to_load:
with open(os.path.join("knowledge", d), "r") as f:
content = f.read()
batch.add_object(
{
"content": content,
"year": d.split("_")[0],
}
)
# Initialize the tool
tool = WeaviateVectorSearchTool(
collection_name='example_collections',
limit=3,
alpha=0.75,
weaviate_cluster_url="https://your-weaviate-cluster-url.com",
weaviate_api_key="your-weaviate-api-key",
)
代理集成示例以下是如何将 WeaviateVectorSearchTool 与 CrewAI 代理集成的示例
from crewai import Agent
from crewai_tools import WeaviateVectorSearchTool
# Initialize the tool
weaviate_tool = WeaviateVectorSearchTool(
collection_name='example_collections',
limit=3,
alpha=0.75,
weaviate_cluster_url="https://your-weaviate-cluster-url.com",
weaviate_api_key="your-weaviate-api-key",
)
# Create an agent with the tool
rag_agent = Agent(
name="rag_agent",
role="You are a helpful assistant that can answer questions with the help of the WeaviateVectorSearchTool.",
llm="gpt-4o-mini",
tools=[weaviate_tool],
)
结论
WeaviateVectorSearchTool 提供了一种强大的方式来在 Weaviate 向量数据库中搜索语义相似的文档。通过利用向量嵌入,它与传统的基于关键词的搜索相比,可以实现更准确、更具上下文相关性的搜索结果。此工具对于需要根据含义而不是精确匹配来查找信息的应用程序特别有用。
《动手学PyTorch建模与应用:从深度学习到大模型》是一本从零基础上手深度学习和大模型的PyTorch实战指南。全书共11章,前6章涵盖深度学习基础,包括张量运算、神经网络原理、数据预处理及卷积神经网络等;后5章进阶探讨图像、文本、音频建模技术,并结合Transformer架构解析大语言模型的开发实践。书中通过房价预测、图像分类等案例讲解模型构建方法,每章附有动手练习题,帮助读者巩固实战能力。内容兼顾数学原理与工程实现,适配PyTorch框架最新技术发展趋势。

更多推荐



所有评论(0)