WeaviateVectorSearchTool

描述

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_KEY 作为 OpenAI API 密钥。

示例

以下示例演示了如何初始化工具并执行搜索

代码
from crewai_tools import WeaviateVectorSearchTool

# Initialize the tool
tool = WeaviateVectorSearchTool(
    collection_name='example_collections',
    limit=3,
    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
  • 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,
    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,
    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,
    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 向量数据库中搜索语义相似文档的强大方法。通过利用向量嵌入,它能够比传统的基于关键词的搜索提供更准确、上下文更相关的搜索结果。此工具对于需要基于含义而非精确匹配查找信息的应用特别有用。