8
n8n 中文网amn8n.com

在 n8n 中为自然 AI 对话实现可扩展的消息缓冲区

高级

这是一个Miscellaneous, AI Chatbot, Multimodal AI领域的自动化工作流,包含 24 个节点。主要使用 If, Wait, Redis, Agent, ChatTrigger 等节点。 使用 Redis 和 GPT-4-mini 为 AI 聊天实现智能消息缓冲

前置要求
  • Redis 服务器连接信息
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "AwApYNYyap3QWQCh",
  "meta": {
    "instanceId": "34b0d0e99edc6fd6ff56c1433b02b593911416243044265caed0be2f3275a537"
  },
  "name": "在 n8n 中为自然 AI 对话实现可扩展的消息缓冲区",
  "tags": [],
  "nodes": [
    {
      "id": "2f166c61-c613-46ef-9d58-d24873c6a477",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1408,
        -64
      ],
      "parameters": {
        "color": 5,
        "width": 380,
        "height": 724,
        "content": "## 🚀 欢迎使用可扩展聊天缓冲区工作流!"
      },
      "typeVersion": 1
    },
    {
      "id": "d09b934e-a97b-4e33-a34e-85044c7ab8ae",
      "name": "便签 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1008,
        528
      ],
      "parameters": {
        "width": 280,
        "height": 352,
        "content": "## 📥 消息入口点"
      },
      "typeVersion": 1
    },
    {
      "id": "983f720c-dde7-4ba1-847f-f10524aa4018",
      "name": "便签 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -752,
        80
      ],
      "parameters": {
        "color": 2,
        "width": 280,
        "height": 260,
        "content": "## 🗄️ 消息队列插入"
      },
      "typeVersion": 1
    },
    {
      "id": "f38bceca-c322-4b22-9033-c9f48052510b",
      "name": "便签 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        128,
        -112
      ],
      "parameters": {
        "color": 4,
        "width": 320,
        "height": 372,
        "content": "## ⏰ 智能等待逻辑"
      },
      "typeVersion": 1
    },
    {
      "id": "fe0c902c-995b-41ae-b322-9cf1d4dd844b",
      "name": "便签 4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1040,
        -96
      ],
      "parameters": {
        "color": 3,
        "width": 320,
        "height": 348,
        "content": "## 🔄 消息提取和上下文构建"
      },
      "typeVersion": 1
    },
    {
      "id": "df0d2487-ee4d-432d-8877-bcdf869eb28a",
      "name": "便签 5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1648,
        -80
      ],
      "parameters": {
        "color": 6,
        "width": 320,
        "height": 336,
        "content": "## 🤖 AI Agent 处理"
      },
      "typeVersion": 1
    },
    {
      "id": "89b74088-597b-45d0-9bc2-de9f70647221",
      "name": "检查延迟",
      "type": "n8n-nodes-base.if",
      "position": [
        576,
        272
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "32ce777d-b762-4635-9618-c772bac2337b",
              "operator": {
                "type": "number",
                "operation": "lt"
              },
              "leftValue": "={{ $json.timestamp.toNumber() + 15 }}",
              "rightValue": "={{ $now.toSeconds() }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "e39363aa-5a75-4666-b8bf-0e2b4eb4df91",
      "name": "检查第一条消息",
      "type": "n8n-nodes-base.if",
      "position": [
        -320,
        368
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "de69235c-bae4-4140-b47f-aff3a24b4be6",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.values()[0] }}",
              "rightValue": 1
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "1f0a5bf6-2a3e-422c-8210-aad82f9c867e",
      "name": "获取时间戳",
      "type": "n8n-nodes-base.redis",
      "position": [
        352,
        272
      ],
      "parameters": {
        "key": "=timestamp_{{ $('chat').first().json.sessionId }}",
        "keyType": "string",
        "options": {},
        "operation": "get",
        "propertyName": "timestamp"
      },
      "typeVersion": 1
    },
    {
      "id": "c7e250c9-98bf-45b6-9e50-c9567ff0b691",
      "name": "时间戳",
      "type": "n8n-nodes-base.redis",
      "position": [
        -96,
        272
      ],
      "parameters": {
        "key": "=timestamp_{{ $('chat').first().json.sessionId }}",
        "ttl": 25,
        "value": "={{ $now.toSeconds() }}",
        "expire": true,
        "keyType": "string",
        "operation": "set"
      },
      "typeVersion": 1
    },
    {
      "id": "9bf0ef1e-1d0d-499d-bcfb-05fd72be84a2",
      "name": "OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1696,
        496
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4-mini"
        },
        "options": {
          "topP": 1,
          "temperature": 0.8,
          "frequencyPenalty": 0.8
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "a4f025ec-398c-491f-b260-20b2a0c25f9c",
      "name": "无",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -96,
        464
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "73504fdd-3fe1-453f-a6f4-16cf4cc8c775",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1696,
        272
      ],
      "parameters": {
        "text": "={{ $json.message }}",
        "options": {
          "systemMessage": "You are a helpful AI assistant. Respond naturally to the complete context of what the user is saying."
        },
        "promptType": "define"
      },
      "typeVersion": 2,
      "alwaysOutputData": true
    },
    {
      "id": "8d0fe1a5-0617-45cb-b254-c2d500d24526",
      "name": "redis_chat_memory",
      "type": "@n8n/n8n-nodes-langchain.memoryRedisChat",
      "position": [
        1824,
        496
      ],
      "parameters": {
        "sessionKey": "=memory_{{ $('chat').first().json.sessionId }}",
        "sessionTTL": 7200,
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.5
    },
    {
      "id": "790ddc78-a3c7-4b31-ad1d-af6ca2ee978a",
      "name": "聊天",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -992,
        368
      ],
      "webhookId": "chat-buffer-webhook",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.3
    },
    {
      "id": "ddbf7ea0-ed49-4c6b-a7bc-2a2cacfb043d",
      "name": "存储",
      "type": "n8n-nodes-base.redis",
      "position": [
        -768,
        368
      ],
      "parameters": {
        "list": "=chat_{{ $json.sessionId }}",
        "operation": "push",
        "messageData": "={{ $json.chatInput }}"
      },
      "typeVersion": 1
    },
    {
      "id": "4c9a61a7-ec30-4ffc-ba1e-670fd2f0f0c2",
      "name": "计数",
      "type": "n8n-nodes-base.redis",
      "position": [
        -544,
        368
      ],
      "parameters": {
        "key": "=counter_{{ $json.sessionId }}",
        "ttl": 25,
        "expire": true,
        "operation": "incr"
      },
      "typeVersion": 1
    },
    {
      "id": "42ca5892-fc88-4f91-9810-6997209f64e3",
      "name": "提取",
      "type": "n8n-nodes-base.redis",
      "position": [
        800,
        272
      ],
      "parameters": {
        "list": "=chat_{{ $('chat').first().json.sessionId }}",
        "tail": true,
        "options": {},
        "operation": "pop",
        "propertyName": "text"
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "9285cfa5-5c37-4a43-bccb-37a5eb1d2027",
      "name": "等待",
      "type": "n8n-nodes-base.wait",
      "position": [
        128,
        272
      ],
      "webhookId": "wait-webhook",
      "parameters": {},
      "typeVersion": 1.1
    },
    {
      "id": "b75f167e-b117-4b90-9746-a24fc88763f4",
      "name": "获取消息",
      "type": "n8n-nodes-base.redis",
      "position": [
        1024,
        272
      ],
      "parameters": {
        "key": "=message_{{ $('chat').first().json.sessionId }}",
        "keyType": "string",
        "options": {},
        "operation": "get",
        "propertyName": "message"
      },
      "typeVersion": 1
    },
    {
      "id": "7f405702-5983-431a-96fe-b04524c04ae4",
      "name": "设置消息",
      "type": "n8n-nodes-base.redis",
      "position": [
        1248,
        272
      ],
      "parameters": {
        "key": "=message_{{ $('chat').first().json.sessionId }}",
        "ttl": 5,
        "value": "={{ $json.message ? $json.message : \"\" }}{{ $('extract').first().json.text }}\n",
        "expire": true,
        "keyType": "string",
        "operation": "set"
      },
      "typeVersion": 1
    },
    {
      "id": "403218e5-4032-4947-b8ca-c336a88fbb4d",
      "name": "检查队列是否为空",
      "type": "n8n-nodes-base.if",
      "position": [
        1472,
        272
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "db8d3308-4158-423c-817e-b55786bc13ca",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $('extract').first().json.text }}",
              "rightValue": "={{ $json.values()[0] }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "250117dc-ae26-4ea2-b782-a581ad2b8790",
      "name": "便利贴 6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -416,
        592
      ],
      "parameters": {
        "color": 7,
        "width": 328,
        "height": 312,
        "content": "## 🔍 关键决策点"
      },
      "typeVersion": 1
    },
    {
      "id": "022aa490-78eb-4ad3-ad61-7ce621541443",
      "name": "便利贴 7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2080,
        48
      ],
      "parameters": {
        "color": 5,
        "width": 320,
        "height": 372,
        "content": "## ⚡ 性能提示"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "aa753eee-4ff4-448c-8696-cd277fe2301f",
  "connections": {
    "chat": {
      "main": [
        [
          {
            "node": "store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "wait": {
      "main": [
        [
          {
            "node": "get_timestamp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "count": {
      "main": [
        [
          {
            "node": "check_first_message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "store": {
      "main": [
        [
          {
            "node": "count",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "extract": {
      "main": [
        [
          {
            "node": "get_message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "timestamp": {
      "main": [
        [
          {
            "node": "wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "check_delay": {
      "main": [
        [
          {
            "node": "extract",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get_message": {
      "main": [
        [
          {
            "node": "set_message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "set_message": {
      "main": [
        [
          {
            "node": "check_queue_is_empty",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get_timestamp": {
      "main": [
        [
          {
            "node": "check_delay",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "redis_chat_memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "check_first_message": {
      "main": [
        [
          {
            "node": "timestamp",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "check_queue_is_empty": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "extract",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 杂项, AI 聊天机器人, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量24
分类3
节点类型9
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

外部链接
在 n8n.io 查看

分享此工作流