跳转到主要内容

概述

WeaviateVectorSearchTool 专门用于在存储在 Weaviate 向量数据库中的文档中进行语义搜索。此工具允许您查找与给定查询语义相似的文档,利用向量和关键词搜索的强大功能,从而获得更准确、更具上下文相关性的搜索结果。 Weaviate 是一个向量数据库,用于存储和查询向量嵌入,从而实现语义搜索功能。

安装

要将此工具集成到您的项目中,您需要安装 Weaviate 客户端
uv add weaviate-client

开始步骤

要有效使用 WeaviateVectorSearchTool,请遵循以下步骤
  1. 软件包安装:确认您的 Python 环境中已安装 crewai[tools]weaviate-client 软件包。
  2. Weaviate 设置:设置 Weaviate 集群。您可以按照 Weaviate 文档 中的说明进行操作。
  3. API 密钥:获取您的 Weaviate 集群 URL 和 API 密钥。
  4. 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_openainomic-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 向量数据库中搜索语义相似的文档。通过利用向量嵌入,它与传统的基于关键词的搜索相比,可以实现更准确、更具上下文相关性的搜索结果。此工具对于需要根据含义而不是精确匹配来查找信息的应用程序特别有用。