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

MongoDB 向量搜索工具

MongoDBVectorSearchTool 在 MongoDB Atlas 上执行向量搜索,并提供可选的索引辅助功能。

MongoDBVectorSearchTool

描述

MongoDBVectorSearchTool 可在 MongoDB Atlas 集合上执行向量相似度查询。MongoDB Atlas 支持原生向量搜索。该工具还支持索引创建辅助功能以及嵌入文本的批量插入。

MongoDB 向量搜索适用于希望使用向量搜索数据库通过语义搜索查询数据的应用, 在集合中的其他字段上过滤查询,或通过实现检索增强生成(RAG)为你的 AI 应用和智能体化系统提供支持。

了解更多:https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-overview/

安装

使用 MongoDB extra 安装:

shell
pip install crewai-tools[mongodb]

or

shell
uv add crewai-tools --extra mongodb

初始化参数

  • connection_string (str):必需。用于连接到集群的 MongoDB Atlas 连接字符串凭证。
  • database_name (str):必需。要使用的 MongoDB 数据库名称。
  • collection_name (str):必需。数据库中存储文档和嵌入的集合。
  • vector_index_name (str,默认值:vector_index):可选。要查询的 MongoDB 向量搜索索引名称(必须与 Atlas 中 创建的索引匹配)。
  • text_key (str,默认值:text):可选。存储原始文本的文档字段名称。
  • embedding_key (str,默认值:embedding):可选。存储向量嵌入的文档字段名称。
  • dimensions (int,默认值:1536):可选。嵌入模型的维度数(必须与你的模型匹配)。
  • query_config (MongoDBVectorSearchConfig):可选。用于高级查询设置的 MongoDBVectorSearchConfig 对象,例如 limit 和 oversampling_factor。

MongoDBVectorSearchTool 方法

  • run(query=...):嵌入查询并执行向量相似度搜索,返回匹配的文档。 接受自然语言 query(str)参数以进行嵌入和搜索。
  • add_texts(list of strings):接受字符串列表,将其嵌入并作为文档插入你的 MongoDB Atlas 数据库。
  • create_vector_search_index(dimensions=...):使用正确的维度 和相似度来配置 MongoDB 向量搜索索引。接受 dimensions(int)参数。

快速开始

以下示例演示如何初始化该工具,并针对指定的 MongoDB Atlas 集合执行向量相似度搜索:

python
from crewai_tools import MongoDBVectorSearchTool

tool = MongoDBVectorSearchTool(
  connection_string="mongodb+srv://...",
  database_name="mydb",
  collection_name="docs",
)

print(tool.run(query="how to create vector index"))

常见问题

  • 身份认证失败:确保你的 Atlas IP 访问列表允许你的运行器,并且连接字符串包含凭证。
  • 未找到索引:先创建向量索引;名称必须与 vector_index_name 匹配。
  • 维度不匹配:使嵌入模型的维度与 dimensions 保持一致。

示例

预加载数据库并创建索引

以下示例演示如何用文档预加载你的 MongoDB Atlas 数据库, 并创建 MongoDB 向量搜索索引以查询你的集合:

python
import os
from crewai_tools import MongoDBVectorSearchTool

tool = MongoDBVectorSearchTool(
    database_name="example_database",
    collection_name="example_collection",
    connection_string="<your_mongodb_connection_string>",
    dimensions=3072,
)

# Load text content from a local folder and add to your MongoDB collection
texts = []
for fname in os.listdir("knowledge"):
    path = os.path.join("knowledge", fname)
    if os.path.isfile(path):
        with open(path, "r", encoding="utf-8") as f:
            texts.append(f.read())

tool.add_texts(texts)

# Create the MongoDB Vector Search index (e.g., 3072 dims for text-embedding-3-large)
tool.create_vector_search_index(dimensions=3072)

自定义查询配置和智能体配置

以下示例演示如何向 MongoDBVectorSearchTool 提供自定义查询配置, 并与 CrewAI 智能体集成:

python
from crewai_tools import MongoDBVectorSearchConfig, MongoDBVectorSearchTool

query_config = MongoDBVectorSearchConfig(limit=10, oversampling_factor=2)
tool = MongoDBVectorSearchTool(
    database_name="example_database",
    collection_name="example_collection",
    connection_string="<your_mongodb_connection_string>",
    query_config=query_config,
    vector_index_name="my_vector_index",
)

rag_agent = Agent(
    name="rag_agent",
    role="You are a helpful assistant that can answer questions with the help of the MongoDBVectorSearchTool.",
    goal="...",
    backstory="...",
    tools=[tool],
)

运行 CrewAI 工作流

以下示例演示如何将你的 MongoDBVectorSearchTool 与智能体、任务和 团队集成,以创建并运行 CrewAI 智能体化 RAG 工作流:

python
from crewai import Agent, Task, Crew
from crewai_tools import MongoDBVectorSearchTool

tool = MongoDBVectorSearchTool(
    connection_string="mongodb+srv://...",
    database_name="mydb",
    collection_name="docs",
)

agent = Agent(
    role="RAG Agent",
    goal="Answer using MongoDB vector search",
    backstory="Knowledge retrieval specialist",
    tools=[tool],
    verbose=True,
)

task = Task(
    description="Find relevant content for 'indexing guidance'",
    expected_output="A concise answer citing the most relevant matches",
    agent=agent,
)

crew = Crew(
    agents=[agent], 
    tasks=[task],
    verbose=True,
)

result = crew.kickoff()

延伸阅读

要详细了解 CrewAI 与 MongoDB 的集成,请参阅以下 MongoDB 指南:

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