Weaviate 向量搜索
WeaviateVectorSearchTool 专为在存储于 Weaviate 向量数据库中的文档上执行语义搜索而设计。此工具可以帮助你找到与给定查询在语义上相似的文档,结合向量搜索与关键字搜索的能力,获得更准确且更贴近上下文的搜索结果。
Weaviate 是一个存储和查询向量嵌入的向量数据库,支持语义搜索能力。
要将此工具集成到项目中,你需要安装 Weaviate 客户端:
uv add weaviate-client要有效使用 WeaviateVectorSearchTool,请按以下步骤操作:
- 安装包:确认你的 Python 环境中已安装
crewai[tools]和weaviate-client。 - 设置 Weaviate:配置一个 Weaviate 集群。你可以参考 Weaviate 文档 获取说明。
- API Keys:获取你的 Weaviate 集群 URL 和 API key。
- OpenAI API Key:确保你已将 OpenAI API key 设为环境变量
OPENAI_API_KEY。
下面的示例演示如何初始化工具并执行搜索:
from crewai_tools import WeaviateVectorSearchTool
# Initialize the tooltool = 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",)
@agentdef 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 key。
- limit:可选。返回结果数量。默认值为
3。 - alpha:可选。控制向量搜索与关键字(BM25)搜索之间的权重。alpha = 0 表示仅 BM25,alpha = 1 表示仅向量搜索。默认值为
0.75。 - vectorizer:可选。要使用的向量化器。如果未提供,则使用带有
nomic-embed-text模型的text2vec_openai。 - generative_model:可选。要使用的生成模型。如果未提供,则使用 OpenAI 的
gpt-4o。
你可以自定义工具所使用的 vectorizer 和生成模型:
from crewai_tools import WeaviateVectorSearchToolfrom weaviate.classes.config import Configure
# Setup custom model for vectorizer and generative modeltool = 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 osfrom crewai_tools import WeaviateVectorSearchToolimport weaviatefrom weaviate.classes.init import Auth
# Connect to Weaviateclient = 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 collectiontest_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 documentsdocs_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 tooltool = 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",)代理集成示例
Section titled “代理集成示例”以下示例展示了如何将 WeaviateVectorSearchTool 与 CrewAI 代理集成:
from crewai import Agentfrom crewai_tools import WeaviateVectorSearchTool
# Initialize the toolweaviate_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 toolrag_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 向量数据库中搜索语义相似文档。通过利用向量嵌入,它比传统基于关键字的搜索更准确,也更贴近上下文。对于需要基于含义而非精确匹配来查找信息的应用,这个工具尤其有用。