本教程演示如何使用 n8n 快速构建AI代理,以回答有关某些示例数据的问题。该代理利用MongoDB作为用于文档检索的向量存储和用于会话持久性的聊天内存存储。
关于MongoDB的 n8n 工作流
以下屏幕截图演示了您将在本教程中构建的工作流程。在此工作流中,您将创建一个使用两个MongoDB节点的AI代理:
它还使用 OpenAI 作为 LLM 和嵌入模型提供商。
当您向代理发送消息时,会触发以下动作:
AI代理节点处理用户消息并确定下一步动作。
AI代理检查是否需要调用任何工具。
如果是,则调用MongoDB Vector Store节点来检索文档。
如果没有,则继续执行下一个动作。
代理使用您为工作流程配置的 LLM 生成响应。
每次交互后, MongoDB聊天内存节点都会存储对话历史记录。

注意
AI助手节点配置中的红色星号表示所需参数。
先决条件
在开始之前,请确保您具备以下内容:
以下MongoDB 集群类型之一:
运行MongoDB 版本的Atlas6.0.11 集群,7.0.2 或更高版本。确保您的 IP解决 包含在Atlas项目的 访问权限列表 中。
使用Atlas CLI创建的本地Atlas部署。要学习;了解更多信息,请参阅创建本地Atlas部署。
安装了Search 和 Vector Search的MongoDB Community或 Enterprise集群。
OpenAI API密钥,或您打算使用的任何其他受支持的 LLM 和嵌入模型提供商的API密钥。
Considerations
使用 n8n 和MongoDB时,请考虑以下事项:
创建向量搜索索引
必须先在集合上创建向量搜索索引,然后才能使用MongoDB向量存储节点。
转到 Collections(快速入门)页面。
单击集群的对应 Browse Collections 按钮。
显示数据浏览器。
开始您的索引配置。
在页面上进行以下选择,然后单击 Next。
Search Type | 选择 Vector Search 索引类型。 |
Index Name and Data Source | 指定以下信息:
|
Configuration Method | For a guided experience, select Visual Editor. To edit the raw index definition, select JSON Editor. |
重要提示:
默认下, MongoDB Search索引名为 default
。如果保留此名称,则该索引将是任何未在运算符中指定其他 index
选项的MongoDB搜索查询的默认搜索索引。如果您要创建多个索引,我们建议您在所有索引之间保持一致的描述性命名约定。
要使用其他索引创建方法,请按照索引创建过程中的步骤操作。使用以下配置:
{ "fields": [ { "type": "vector", "path": "plot_embedding", "numDimensions": 1536, "similarity": "dotProduct" } ] }
在 n8n 中配置档案
在本部分中,您将完成 n8n 中配置所需凭证的步骤。
配置AI助手工作流程
在 n8 n 中完成以下步骤,以使用MongoDB和 n8 n构建AI代理工作流程。要学习;了解有关工作流程的更多信息,请参阅关于MongoDB的 n n 工作流程。8
添加MongoDB Atlas Vector Store节点作为工具。
配置MongoDB Atlas Vector Store节点以启用文档检索。
单击AI代理节点上 Tool 标签旁边的 +Connector。
在搜索面板中,选择 MongoDB Atlas Vector Store。
选择您配置的MongoDB凭证,然后指定以下值:
设置值Operation Mode
Retrieve Documents (As Tool for AI Agent)
Description
"Search through documents about movies to find relevant information"
MongoDB Collection
embedded_movies
Embedding
plot_embedding
Metadata Field
plot
Vector Index Name
vector_index
Limit
4
Include Metadata
切换到 Off
重要提示:
如果不将此设置切换为 Off,则AI代理可能不会运行,因为文档可能太大而无法加载到上下文窗口中。
Rerank Results
切换到 Off
测试工作流程。
现在您已经配置了AI助手节点,可以测试完整的工作流程。发送消息后,工作流会在画布上实时运行。
单击画布底部附近的 Open chat 按钮。
通过运行一些初始提示来测试内存功能。示例:
Hi, my name is Mongo Hello Mongo! Nice to meet you. How can I help you today? What is my name? Your name is Mongo, as you mentioned when you introduced yourself earlier. 通过运行以下提示来测试向量搜索和 RAG 功能。
Recommend me a few movies about time travel Here are a few movies about time travel that you might enjoy: The Time Traveler's Wife - A romantic drama about a Chicago librarian with a gene that causes him to involuntarily time travel, leading to complications in his marriage. Timecop - An officer working for a security agency that regulates time travel must battle for his life against a corrupt politician tied to his past. We Are from the Future (My iz budushchego) - Four modern-day treasure seekers are unexpectedly transported into the midst of a World War II battle set in Russia. About Time - A young man discovers at the age of 21 that he can travel back in time. He attempts to improve his life, including finding love, but things don't turn out to be as simple as they seem. Let me know if you'd like any additional information about these films!