本教程演示如何使用 n8n 快速构建AI代理,以回答有关某些示例数据的问题。该代理利用MongoDB作为用于文档检索的向量存储和用于会话持久性的聊天内存存储。
关于MongoDB的 n8n 工作流
以下屏幕截图演示了您将在本教程中构建的工作流程。在此工作流中,您将创建一个使用两个MongoDB节点的AI代理:
MongoDB Atlas Vector Store Node,用于从示例数据中检索相关文档
用于存储对话历史记录的MongoDB聊天内存节点
它还使用 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集群。
示例数据已加载到MongoDB 部署中。
OpenAI API密钥,或您打算使用的任何其他受支持的 LLM 和嵌入模型提供商的API密钥。
Considerations
使用 n8n 和MongoDB时,请考虑以下事项:
对于MongoDB Atlas用户,请确保将您的 n8n 个IP地址添加到项目的IP访问列表中。要学习;了解更多信息,请参阅网络访问。
配置MongoDB Vector Store节点时,请确保将 Include Metadata 切换为 Off。如果您不将此设置切换为 Off,则AI代理可能不会运行,因为文档可能太大而无法加载到上下文窗口中。
创建向量搜索索引
必须先在集合上创建向量搜索索引,然后才能使用MongoDB向量存储节点。
AtlasGoClusters在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 Clusters。
会显示集群页面。
AtlasGoData Explorer在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Data Explorer。
显示数据浏览器。
注意
您也可以转到 Clusters 页面,然后单击 Shortcuts 标题下的 Data Explorer。
在Atlas中,转到集群的Search & Vector Search 页面。
您可以从 Search & Vector Search 选项或 Data Explorer 转到MongoDB搜索页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Search & Vector Search。
如果没有集群,则请单击 Create cluster 来创建一个。要了解更多信息,请参阅创建集群。
如果您的项目有多个集群,请从 Select cluster 下拉列表中选择要使用的集群,然后单击 Go to Atlas Search。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Data Explorer。
展开数据库并选择集合。
单击该集合的 Indexes 标签页。
将显示 Atlas Search 页面。
开始您的索引配置。
在页面上进行以下选择,然后单击 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 中配置所需凭证的步骤。
为模型提供商添加凭证。
从下拉菜单中,选择要配置的凭证。
示例,选择 OpenAI 并单击 Continue。
要查看为模型提供商输入哪些参数,请检查凭证库。
示例,对于 OpenAI,输入API密钥、组织名称和基本URL。
单击 Save。n8n 将档案添加到您的个人空间。
为MongoDB添加凭证。
按照上一步所述相同说明将MongoDB凭证添加到 n8n。指定以下值:
设置 | 值 |
|---|---|
配置类型 | 在下拉菜单中,选择 Connection String 方法。 |
连接字符串 | 输入您的MongoDB Connection String。 要学习;了解如何查找MongoDB连接字符串,请参阅连接字符串。 |
Database | 输入 sample_mflix。 |
配置AI助手工作流程
在 n8n 中完成以下步骤,以使用MongoDB和 n8n构建AI代理工作流程。要学习;了解有关工作流程的更多信息,请参阅关于MongoDB的 n8n 工作流程。
添加MongoDB聊天内存子节点。
配置MongoDB聊天内存子节点以启用持久对话上下文。
单击AI代理节点上 Memory 标签旁边的 +Connector。
从侧面板中选择 MongoDB Chat Memory。
单击 Select credential,然后选择您的MongoDB凭证。
使用MongoDB聊天内存节点的默认配置。默认下,n8n 在指定数据库中创建一个名为
n8n_chat_histories的集合。点击屏幕左上角的 Back to canvas。在此阶段,您可以跳过此节点的测试。单击 Save。
单击AI助手图标返回AI助手节点配置。
添加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_moviesEmbedding
plot_embeddingMetadata Field
plotVector Index Name
vector_indexLimit
4Include 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!