对于AI助手:文档索引位于 https://www.mongodb.com/zh-cn/docs/llms.txt — 通过将 .md 附加到任何URL路径,可以获得所有页面的降价版本。
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs 菜单

将MongoDB与 CrewAI 集成

您可以将MongoDB与CrewAI集成,以构建具有专门角色、工具和任务的自主AI代理和多代理应用程序。具体来说,您可以利用适用于 CrewAI 的 MongoDB Vector Search 工具,启用团队中的 AI助手 从数据中检索相关信息,帮助完成任务。

要完成使用 CrewAI 和MongoDB 的教程,请参阅使用 CrewAI 和MongoDB构建代理 RAG 应用程序。

要安装适用于 CrewAI 的MongoDB Vector Search 工具,请根据您的Python包管理器运行以下命令之一:

pip install 'crewai-tools[mongodb]'
uv add crewai-tools --extra mongodb

注意

Python版本兼容性可能与 CrewAI 官方文档不同。在撰写本文时,crewai-tools包依赖于 embedchain,这需要介于 3.9 和 3.13.2 之间的Python版本(含)。

要使用MongoDB Vector Search Tool,请对其进行初始化,然后将其传递给代理。

要初始化该工具,您必须指定以下内容:

from crewai_tools import MongoDBVectorSearchTool
tool = MongoDBVectorSearchTool(
connection_string="<connection-string>",
database_name="<database-name>",
collection_name="<collection-name>",
# Other optional parameters...
)
# To test the tool
print(tool.run(query="<test-query>"))
# To use the tool in an agent
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],
)

(可选)您可以通过为工具的构造函数指定 MongoDBVectorSearchConfig 的实例来自定义工具的向量搜索查询。

要学习;了解有关向量搜索查询的更多信息,请参阅运行向量搜索查询。

from crewai_tools import MongoDBVectorSearchConfig, MongoDBVectorSearchTool
# Custom query configuration
query_config = MongoDBVectorSearchConfig(
limit = 10,
oversampling_factor = 2,
)
tool = MongoDBVectorSearchTool(
database_name="example_database",
collection_name="example_collection",
connection_string="<connection_string>",
query_config=query_config,
# Other optional parameters...
)
# To test the tool
print(tool.run(query="<test-query>"))
# To use the tool in an agent
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],
)

使用这些参数配置向量搜索工具。

Parameter
必要性
说明

connection_string

必需

MongoDB实例的连接字符串。要学习有关查找连接字符串的更多信息,请参阅通过客户端库连接到集群。

对于本地部署,连接字符串应使用以下格式:

mongodb://localhost:<port-number>/?directConnection=true

要学习;了解有关连接字符串的更多信息,请参阅连接字符串。

database_name

必需

MongoDB database的名称。

collection_name

必需

MongoDB集合的名称。

query_config

Optional

用于自定义向量搜索查询的MongoDBVectorSearchConfig实例。

embedding_model

Optional

用于生成向量嵌入的 OpenAI 嵌入模型。默认为 text-embedding-3-large

vector_index_name

Optional

MongoDB Vector Search索引的名称。默认为 vector_index

text_key

Optional

包含文本内容的文档字段。默认为 text

embedding_key

Optional

存储向量嵌入的文档字段。默认为 embedding

dimensions

Optional

向量嵌入的维数。默认为 1536

使用这些参数自定义向量搜索查询。

Parameter
必要性
说明

limit

Optional

要返回的最大文档数。默认值为 4

pre_filter

Optional

MongoDB $match表达式,用于在向量搜索之前过滤文档。

post_filter_pipeline

Optional

在向量搜索后应用的MongoDB聚合阶段列表。

oversampling_factor

Optional

limit 的乘数,用于确定搜索过程中考虑的候选数 (numCandidates)。默认为 10

include_embeddings

bool

如果为 True,则每个结果的向量嵌入都包含在输出中。默认为 False

有关更多信息,请参阅CrewAI MongoDB Vector Search 工具Docs

MongoDBVectorSearchTool 类提供以下方法:

  • add_texts():将文本文档添加到指定的MongoDB集合。

  • create_vector_search_index():在集合上创建向量搜索索引。

  • run():对数据运行向量搜索查询。

import os
from crewai_tools import MongoDBVectorSearchTool
tool = MongoDBVectorSearchTool(
connection_string="<connection-string>",
database_name="<database-name>",
collection_name="<collection-name>"
)
# Example of loading text content from a local folder
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())
# Method to add documents to the vector store
tool.add_texts(texts)
# Method to create the vector search index
tool.create_vector_search_index(dimensions=<number-of-dimensions>)
# Method to test the tool by running a vector search query
tool.run(query="<search-query>")

使用这些参数配置向量索引。

Parameter
必要性
说明

dimensions

必需

嵌入向量的维数。

relevance_score_fn

Optional

相似度指标。euclideancosinedotProduct。默认为 cosine

auto_index_timeout

Optional

等待索引就绪的时间(以秒为单位)。默认为 15

要学习;了解有关向量搜索索引的更多信息,请参阅如何为向量搜索的字段编制索引。

使用这些参数可配置MongoDB摄取文档的方式。

Parameter
必要性
说明

texts

必需

要添加的可遍历文本文档的数组。

metadatas

Optional

元数据文档列表,每个文本文档一个。

ids

Optional

每个文档的唯一 ID 列表。

batch_size

Optional

在单个批处理中要进程和插入的文档数。默认为 100