Skip to content

使用 n8n 构建 AI 聊天代理#

¥Build an AI chat agent with n8n

欢迎来到使用 n8n 构建 AI 工作流的入门教程。无论你之前是否使用过 n8n,我们都将向你展示 AI 工作流的各个构建模块如何组合在一起,并构建一个可轻松自定义以满足你自身需求的 AI 聊天代理。

¥Welcome to the introductory tutorial for building AI workflows with n8n. Whether you have used n8n before, or this is your first time, we will show you how the building blocks of AI workflows fit together and construct a working AI-powered chat agent which you can easily customize for your own purposes.

"Screenshot of the completed workflow"

许多人发现以视频格式接收新信息更容易。本教程基于 n8n 的热门视频之一,链接如下。观看视频或阅读此处的步骤,或者两者都做!

¥Many people find it easier to take in new information in video format. This tutorial is based on one of n8n's popular videos, linked below. Watch the video or read the steps here, or both!

你需要准备什么?#

¥What you will need

  • n8n:在本教程中,我们建议使用 n8n 云 服务 - 新用户可免费试用!对于自托管服务,请参阅 安装页面

¥n8n: For this tutorial we recommend using the n8n cloud service - there is a free trial for new users! For a self hosted service, refer to the installation pages.

  • 聊天模型的凭据:本教程使用 OpenAI,但你也可以轻松使用 DeepSeek、Google Gemini、Groq、Azure 等(更多信息请参阅 子节点文档)。

¥Credentials for a chat model: This tutorial uses OpenAI, but you can easily use DeepSeek, Google Gemini, Groq, Azure, and others (see the sub-nodes documentation for more).

你将学习什么?#

¥What you will learn

  • n8n 中的 AI 概念

¥AI concepts in n8n

  • 如何使用 AI Agent 节点

¥How to use the AI Agent node

  • 使用聊天输入

¥Working with Chat input

  • 正在连接 AI 模型

¥Connecting with AI models

  • 自定义输入

¥Customising input

  • 观察会话

¥Observing the conversation

  • 添加持久性

¥Adding persistence

n8n 中的 AI 概念#

¥AI concepts in n8n

如果你已经熟悉 AI,则可以跳过此部分。这是对 AI 概念及其在 n8n 工作流中的应用的基本介绍。

¥If you're already familiar with AI, feel free to skip this section. This is a basic introduction to AI concepts and how they can be used in n8n workflows.

AI 代理 基于 大型语言模型 (LLM) 构建,后者通过预测下一个单词,根据输入生成文本。虽然 LLM 仅处理输入以生成输出,但 AI 代理会添加面向目标的功能。它们可以使用 tools、处理其输出并做出决策以完成任务和解决问题。

¥An AI agent builds on Large Language Models (LLMs), which generate text based on input by predicting the next word. While LLMs only process input to produce output, AI agents add goal-oriented functionality. They can use tools, process their outputs, and make decisions to complete tasks and solve problems.

在 n8n 中,AI 代理以节点的形式表示,并带有一些额外的连接。

¥In n8n, the AI agent is represented as a node with some extra connections.

功能 LLM AI 代理
核心功能 文本生成 目标导向型任务完成
决策
使用工具/API
工作流复杂度 单步操作 多步骤
范围 生成语言 执行复杂的实际任务
示例 LLM 正在生成段落 代理正在安排预约

通过将 AI 代理集成到一个节点中,n8n 可以将 AI 驱动的步骤与传统编程相结合,从而实现高效的实际工作流。例如,验证电子邮件地址等简单任务不需要 AI,而处理电子邮件内容或处理多模态输入(例如图片、音频)等复杂任务则非常适合使用 AI 代理。

¥By incorporating the AI agent as a node, n8n can combine AI-driven steps with traditional programming for efficient, real-world workflows. For instance, simpler tasks, like validating an email address, do not require AI, whereas a complex tasks, like processing the content of an email or dealing with multimodal inputs (e.g., images, audio), are excellent uses of an AI agent.

1. 创建新工作流#

¥ Create a new workflow

When you open n8n, you'll see either:

  • An empty workflow: if you have no workflows and you're logging in for the first time. Use this workflow.
  • The Workflows list on the Overview page. Select the universal create resource icon button to create a new workflow.

2. 添加触发节点#

¥ Add a trigger node

每个工作流都需要一个起点。在 n8n 中,这些节点被称为 '触发节点'。在本工作流程中,我们将从一个聊天节点开始。

¥Every workflow needs somewhere to start. In n8n these are called 'trigger nodes'. For this workflow, we want to start with a chat node.

  1. 选择“添加第一步”或按Tab打开节点菜单。

¥Select Add first step or press Tab to open the node menu.

  1. 搜索聊天触发器n8n 会显示与搜索匹配的节点列表。

¥Search for Chat Trigger. n8n shows a list of nodes that match the search.

  1. 选择“聊天触发器”将节点添加到画布。n8n 打开节点。

¥Select Chat Trigger to add the node to the canvas. n8n opens the node.

  1. 关闭节点详情视图(选择“返回画布”)返回画布。

¥Close the node details view (Select Back to canvas) to return to the canvas.

???说明 "更多关于聊天触发器的信息节点...":触发节点在发生触发事件时会生成输出。在这种情况下,我们希望能够输入文本来触发工作流运行。在生产环境中,此触发器可以连接到 n8n 提供的公共聊天界面,或嵌入到其他网站中。要启动这个简单的工作流,我们将使用内置的本地聊天界面进行通信,因此无需进行其他设置。

¥??? explanation "More about the Chat Trigger node..." The trigger node generates output when there is an event causing it to trigger. In this case we want to be able to type in text to cause the workflow to run. In production, this trigger can be hooked up to a public chat interface as provided by n8n or embedded into another website. To start this simple workflow we will just use the built-in local chat interface to communicate, so no further setup is required.

Workflow preview placeholder.

3. 添加 AI 代理节点#

¥ Add an AI Agent Node

AI 代理节点是向工作流程中添加 AI 的核心。

¥The AI Agent node is the core of adding AI to your workflows.

  1. 在触发节点上选择“添加节点 Add node icon”连接器,以打开节点搜索。

¥Select the Add node {.off-glb} connector on the trigger node to bring up the node search.

  1. 开始输入 "AI" 并选择 AI 代理节点以添加它。

¥Start typing "AI" and choose the AI agent node to add it.

  1. 现在将显示 AI 代理的编辑视图。

¥The editing view of the AI agent will now be displayed.

  1. 有些字段可以更改。由于我们使用的是聊天触发器节点,因此无需更改提示的来源和规范的默认设置。

¥There are some fields which can be changed. As we're using the Chat Trigger node, the default setting for the source and specification of the prompt don't need to be changed.

Workflow preview placeholder.

4. 配置节点#

¥ Configure the node

AI 代理需要附加聊天模型来处理传入的提示。

¥AI agents require a chat model to be attached to process the incoming prompts.

  1. 点击 AI 代理节点上聊天模型连接下方的加号 Add node icon 按钮(节点底部第一个连接),添加聊天模型。

¥Add a chat model by clicking the plus {.off-glb} button underneath the Chat Model connection on the AI Agent node (it's the first connection along the bottom of the node).

  1. 搜索对话框将出现,并按 '语言模型' 进行筛选。以下是 n8n 内置支持的模型。在本教程中,我们将使用 OpenAI Chat 模型。

¥The search dialog will appear, filtered on 'Language Models'. These are the models with built-in support in n8n. For this tutorial we will use OpenAI Chat Model.

  1. 从列表中选择 OpenAI Chat 模型会将其附加到 AI Agent 节点并打开节点编辑器。可更改的参数之一是 '模型'。请注意,对于 OpenAI 基本账户,仅允许使用 'gpt-4o-mini' 模型。

¥Selecting the OpenAI Chat model from the list will attach it to the AI Agent node and open the node editor. One of the parameters which can be changed is the 'Model'. Note that for the basic OpenAI accounts, only the 'gpt-4o-mini' model is allowed.

???"使用哪个聊天模型?" 说明:如前所述,LLM 是根据给定的提示生成文本的组件。大型语言模型 (LLM) 需要创建和训练,这通常是一个耗时的过程。不同的学习管理系统 (LLMS) 可能具有不同的功能或专长,具体取决于它们用于训练的数据。

¥??? explanation "Which chat model?" As mentioned earlier, the LLM is the component which generates the text according to a prompt it is given. LLMs have to be created and trained, usually an intensive process. Different LLMS may have different capabilities or specialties, depending on the data they were trained with.

5. 添加凭据(如果需要)#

¥ Add credentials (if needed)

为了使 n8n 能够与聊天模型通信,它需要一些 credentials(登录数据,使其能够访问其他在线服务的账户)。如果你已为 OpenAI 设置凭据,这些凭据应默认显示在凭据选择器中。或者,你可以使用“凭据”选择器来帮助你添加新凭据。

¥In order for n8n to communicate with the chat model, it will need some credentials (login data giving it access to an account on a different online service). If you already have credentials set up for OpenAI, these should appear by default in the credentials selector. Otherwise you can use the Credentials selector to help you add a new credential.

image showing the credentials dialog for OpenAI

  1. 要添加新的凭据,请单击显示“'选择凭据'”的文本。将出现添加新凭证的选项

¥To add a new credential, click on the text which says 'Select credential'. An option to add a new credential will appear

Screenshot showing create a new credential button

  1. 此凭据只需要一个 API 密钥。添加任何类型的凭据时,请检查右侧的文本。在这种情况下,它会提供一个便捷的链接,直接带你访问你的 OpenAI 账户以检索 API 密钥。

¥This credential just needs an API key. When adding credentials of any type, check the text to the right-hand side. In this case it has a handy link to take you straight to your OpenAI account to retrieve the API key.

  1. API 密钥只是一个长字符串。这就是此特定凭据所需的全部信息。从 OpenAI 网站复制文件,并将其粘贴到 API 密钥部分。

¥The API key is just one long string. That's all you need for this particular credential. Copy it from the OpenAI website and paste it into the API key section.

???说明 "保护你的凭据安全":凭证是由应用和服务颁发的私有信息,用于验证你的用户身份,并允许你在应用或服务与 n8n 节点之间连接和共享信息。所需信息的类型取决于相关的应用/服务。你应该谨慎地在 n8n 外部共享或泄露凭据。

¥??? explanation "Keeping your credentials safe" Credentials are private pieces of information issued by apps and services to authenticate you as a user and allow you to connect and share information between the app or service and the n8n node. The type of information required varies depending on the app/service concerned. You should be careful about sharing or revealing the credentials outside of n8n.

6. 测试节点#

¥ Test the node

现在该节点已连接到聊天触发器和聊天模型,我们可以测试工作流的这一部分。

¥Now that the node is connected to the Chat Trigger and a chat model, we can test this part of the workflow.

  1. 点击画布底部附近的 '聊天' 按钮。这将在左侧打开本地聊天窗口,并在右侧打开 AI 代理日志。

¥Click on the 'Chat' button near the bottom of the canvas. This opens up a local chat window on the left and the AI agent logs on the right.

  1. 输入消息并按回车键。现在,你将在消息下方看到来自聊天模型的响应。

¥Type in a message and press Enter. You will now see the response from the chat model appear below your message.

  1. 日志窗口显示 AI 代理的输入和输出。

¥The log window displays the inputs to and outputs from the AI Agent.

image showing a chat session in progress

???说明 "正在访问日志...":即使你未使用聊天界面,也可以访问 AI 节点的日志。打开 AI Agent 节点,然后点击右侧面板中的“日志”选项卡。

¥??? explanation "Accessing the logs..." You can access the logs for the AI node even when you aren't using the chat interface. Open up the AI Agent node and click on the Logs tab in the right hand panel.

screenshot showing the Logs tab in the AIAgent

7. 更改提示#

¥ Changing the prompt

上一步的日志揭示了一些额外数据 - 系统提示。这是 AI 代理初始化聊天模型时使用的默认消息。从日志中可以看到,此项已设置为“"你是一位乐于助人的助手。"”。但是,我们可以更改此提示来改变聊天模型的行为。

¥The logs in the previous step reveal some extra data - the system prompt. This is the default message that the AI Agent primes the chat model with. From the log you can see this is set to "You are a helpful assistant". We can however change this prompt to alter the behavior of the chat model.

  1. 打开 AI Agent 节点。面板底部有一个名为“'选项'”的部分和一个名为“'添加选项'”的选择器。使用此选项选择 '系统消息'。

¥Open the AI Agent node. In the bottom of the panel is a section labeled 'Options' and a selector labeled 'Add Option'. Use this to select 'System message'

  1. 系统消息现已显示。这是我们之前在日志中看到的相同的提示信息。将提示信息更改为其他内容,以不同的方式启动聊天模型。例如,可以尝试类似 "你是一位才华横溢的诗人,总是用押韵的对句回复。" 的搜索。

¥The system message is now displayed. This is the same priming prompt we noticed before in the logs. Change the prompt to something else to prime the chat model in a different way. You could try something like "You are a brilliant poet who always replies in rhyming couplets" for example.

  1. 关闭节点并返回聊天窗口。重复你的消息,并注意输出的变化。

¥Close the node and return to the chat window. Repeat your message and notice how the output has changed.

image showing changed text for chat, now it rhymes; if you can believe that

8. 添加持久性#

¥ Adding persistence

聊天模型现在可以提供有用的输出,但存在一些问题,当你尝试进行对话时就会发现。

¥The chat model is now giving us useful output, but there is something wrong with it which will become apparent when you try to have a conversation.

  1. 使用聊天功能,并告知聊天模型你的名称,例如 "你好,我叫 Nick"。

¥Use the chat and tell the chat model your name, for example "Hi there, my name is Nick".

  1. 等待响应,然后输入消息 "我的名字是什么?"。人工智能无法告知你任何信息,无论它看起来多么抱歉。原因是我们没有保存上下文。AI 代理没有 memory

¥Wait for the response, then type the message "What's my name?". The AI will not be able to tell you, however apologetic it may seem. The reason for this is we are not saving the context. The AI Agent has no memory.

image showing a conversation illustrating the above

  1. 为了记住对话中发生的事情,AI 代理需要保留上下文。我们可以通过向 AI Agent 节点添加内存来实现这一点。在画布上,单击标记为 "内存" 的 AI 代理节点底部的 Add node icon

¥In order to remember what has happened in the conversation, the AI Agent needs to preserve context. We can do this by adding memory to the AI Agent node. On the canvas click on the {.off-glb} on the bottom of the AI Agent node labeled "Memory".

  1. 从出现的面板中,选择“"Simple Memory"”。这将使用运行 n8n 的实例的内存,通常足以满足简单使用。此处默认值 5 次交互应该足够了,但请记住此选项的位置,以便日后更改。

¥From the panel which appears, select "Simple Memory". This will use the memory from the instance running n8n, and is usually sufficient for simple usage. The default value of 5 interactions should be sufficient here, but remember where this option is if you may want to change it later.

  1. 重复上述对话练习,看看 AI 代理现在是否记住了你的名字。

¥Repeat the exercise of having a conversation above, and see that the AI Agent now remembers your name.

9. 保存工作流#

¥ Saving the workflow

离开工作流编辑器前,请务必保存工作流,否则所有更改都将丢失。

¥Before we leave the workflow editor, remember to save the workflow or all your changes will be lost.

  1. 点击编辑器窗口右上角的 "保存" 按钮。你的工作流现已保存,你可以稍后返回进行聊天或添加新功能。

¥Click on the "Save" button in the top right of the editor window. Your workflow will now be saved and you can return to it later to chat again or add new features.

恭喜!#

¥Congratulations!

你已迈出使用 AI 构建实用高效工作流的第一步。在本教程中,我们研究了 AI 工作流程的基本构建模块,添加了 AI 代理和聊天模型,并调整了提示以获得所需的输出。我们还添加了内存,以便聊天能够在消息之间保留上下文。

¥You have taken your first steps in building useful and effective workflows with AI. In this tutorial we have investigated the basic building blocks of an AI workflow, added an AI Agent and a chat model, and adjusted the prompt to get the kind of output we wanted. We also added memory so the chat could retain context between messages.

Workflow preview placeholder.

下一步步骤#

¥Next steps

现在你已经了解了如何创建基本的 AI 工作流,你可以利用丰富的资源来拓展这些知识,并参考大量示例来了解下一步的方向:

¥Now you have seen how to create a basic AI workflow, there are plenty of resources to build on that knowledge and plenty of examples to give you ideas of where to go next:

  • 了解更多关于 AI 概念的信息,并在 示例和概念 中查看示例。

¥Learn more about AI concepts and view examples in Examples and concepts.

¥Browse AI Workflow templates.

¥Find out how to enhance the AI agent with tools.