Skip to content
CrewAI 文档版本 跟随上游 源快照 a3e90234 更新于 2026-10-18

Weaviate 向量搜索

WeaviateVectorSearchTool 旨在使用混合搜索在 Weaviate 向量数据库中搜索语义相似的文档。

概述

WeaviateVectorSearchTool 专为在 Weaviate 向量数据库存储的文档中执行语义搜索而设计。该工具允许你查找与给定查询语义相似的文档,利用向量搜索和关键词搜索的能力,获得更准确、更符合上下文的搜索结果。

Weaviate 是一个存储和查询向量嵌入的向量数据库,支持语义搜索能力。

安装

要将此工具集成到你的项目中,需要安装 Weaviate 客户端:

shell
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。

示例

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

python
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。

进阶配置

你可以自定义该工具使用的向量化器和生成模型:

python
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 数据库中:

python
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 智能体集成的方法:

python
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 向量数据库中搜索语义相似的文档。通过利用向量嵌入,与传统基于关键词的搜索相比,它可以提供更准确、更符合上下文的搜索结果。对于需要根据含义而非精确匹配来查找信息的应用,该工具尤其有用。

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。