【n8n教程】:5分钟搭建带“记忆”的 AI 聊天机器人

今天我们不写一行代码,通过 n8n 搭建一个真正“聪明”的 AI 助手

很多初学者用 AI 时会发现:它记不住名字!你上一句告诉它“我是小明”,下一句问它“我是谁”,它就懵了。
本教程的目标:教你用 n8n 的 AI Agent 节点,配合 Memory(记忆)模块,打造一个能记住上下文的专属聊天机器人。


核心概念:AI Agent vs LLM

在开始之前,只需理解一个简单的区别:

在 n8n 中,我们就是用 AI Agent 节点把这些能力组装起来。


实战步骤

第一步:设置入口 (Chat Trigger)

任何自动化都需要一个“触发器”。

  1. 1. 在 n8n 画布中点击 + (Add first step)。
  2. 2. 搜索并选择 Chat Trigger
  3. 3. 这个节点不用配置,它就是我们和机器人对话的窗口。

第二步:添加大脑 (AI Agent)

这是本教程的核心。

  1. 1. 点击 Chat Trigger 节点右侧的 + 号。
  2. 2. 搜索 AI Agent 并添加。
  3. 3. 打开节点,你会看到它像一个“插线板”,底部有很多插槽。

第三步:连接模型 (Chat Model)

AI Agent 需要一个大脑来思考。

  1. 1. 在 AI Agent 节点下方,找到 Chat Model 的插槽,点击 +
  2. 2. 搜索 OpenAI Chat Model (或者 DeepSeek, Anthropic 等你有的模型)。
  3. 3. 关键操作:在弹出的窗口中配置 Credential (凭证)。将你的 API Key 填入即可。
  4. 4. 模型选择:推荐使用 gpt-4o-mini,便宜且速度快。

第四步:注入灵魂 (System Prompt)

想让你的 AI 变成“暴躁程序员”还是“温柔心理师”?

  1. 1. 在 AI Agent 设置面板底部,找到 Options
  2. 2. 点击 Add Option -> 选择 System Message
  3. 3. 输入提示词,例如:“你是一个说话押韵的诗人,无论我问什么,你都要用写诗的方式回答。”

第五步:赋予记忆 (Memory) —— 最重要的一步

为了让它记住你是谁:

  1. 1. 回到画布,看 AI Agent 节点底部的 Memory 插槽。
  2. 2. 点击 + 号,搜索 Window Buffer Memory (简单记忆)。
  3. 3. 默认设置即可(它会记住最近的 5 轮对话)。

效果测试

  1. 1. 点击画布下方的 Chat 按钮。
  2. 2. 输入:“你好,我叫阿强。”
  3. 3. 等它回复后,再问:“我叫什么名字?”
  4. 4. 如果它能回答“你是阿强”,恭喜你!你已经成功搭建了一个具备上下文记忆的 AI Agent。

拿来即用:完整工作流代码

我为你准备了完整的 JSON 代码。复制下面的代码,在 n8n 画布上按 Ctrl+V (或 Cmd+V) 即可直接导入,配置好你的 API Key 就能跑!


    
    
    
  {
  "name"
: "Basic AI Chat with Memory",
  "nodes"
: [
    {

      "parameters"
: {},
      "id"
: "1",
      "name"
: "When chat message received",
      "type"
: "n8n-nodes-base.chatTrigger",
      "typeVersion"
: 1.1,
      "position"
: [
        460
,
        340

      ]
,
      "webhookId"
: "b1f8c7a2-1234-4567-8901-abcdef123456"
    }
,
    {

      "parameters"
: {
        "options"
: {
          "systemMessage"
: "You are a helpful assistant with a great memory."
        }

      }
,
      "id"
: "2",
      "name"
: "AI Agent",
      "type"
: "n8n-nodes-base.aiAgent",
      "typeVersion"
: 1,
      "position"
: [
        680
,
        340

      ]

    }
,
    {

      "parameters"
: {
        "model"
: "gpt-4o-mini",
        "options"
: {}
      }
,
      "id"
: "3",
      "name"
: "OpenAI Chat Model",
      "type"
: "n8n-nodes-base.openAiChatModel",
      "typeVersion"
: 1,
      "position"
: [
        680
,
        540

      ]
,
      "credentials"
: {
        "openAiApi"
: {
          "id"
: "YOUR_CREDENTIAL_ID_HERE",
          "name"
: "OpenAI account"
        }

      }

    }
,
    {

      "parameters"
: {
        "contextWindowLength"
: 10
      }
,
      "id"
: "4",
      "name"
: "Window Buffer Memory",
      "type"
: "n8n-nodes-base.memoryBufferWindow",
      "typeVersion"
: 1,
      "position"
: [
        800
,
        540

      ]

    }

  ]
,
  "connections"
: {
    "When chat message received"
: {
      "main"
: [
        [

          {

            "node"
: "AI Agent",
            "type"
: "main",
            "index"
: 0
          }

        ]

      ]

    }
,
    "OpenAI Chat Model"
: {
      "ai_languageModel"
: [
        [

          {

            "node"
: "AI Agent",
            "type"
: "ai_languageModel",
            "index"
: 0
          }

        ]

      ]

    }
,
    "Window Buffer Memory"
: {
      "ai_memory"
: [
        [

          {

            "node"
: "AI Agent",
            "type"
: "ai_memory",
            "index"
: 0
          }

        ]

      ]

    }

  }

}

引用链接

[1] 官方文档: https://docs.n8n.io/advanced-ai/intro-tutorial/
[2] n8n系列教程: https://www.undsky.com/blog/?category=n8n%E6%95%99%E7%A8%8B#