8
n8n 中文网amn8n.com

使用 OpenAI 和 Redis 会话管理的聊天支持客户认证

高级

这是一个Support, AI领域的自动化工作流,包含 19 个节点。主要使用 Set, Form, Redis, Switch, FormTrigger 等节点,结合人工智能技术实现智能自动化。 使用 OpenAI 和 Redis 会话管理的聊天支持客户认证

前置要求
  • Redis 服务器连接信息
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "408f9fb9940c3cb18ffdef0e0150fe342d6e655c3a9fac21f0f644e8bedabcd9",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "0c0090f9-c3a6-421d-8c67-92baeded6007",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -580,
        -160
      ],
      "webhookId": "0fa189dc-16dc-470f-b69d-3e2809d8c071",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "adf71970-7f0c-4394-bdab-b701eaa7173f",
      "name": "LLM",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -240,
        40
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini",
          "cachedResultName": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "8gccIjcuf3gvaoEr",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "56318a6e-3a05-4f6b-9eb5-15ad6144144e",
      "name": "记忆存储",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -120,
        40
      ],
      "parameters": {
        "sessionKey": "=chat_{{ $('When chat message received').first().json.sessionId }}",
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.3
    },
    {
      "id": "6bde1175-7175-4ccd-ac0b-65ecc8c2d7b3",
      "name": "获取会话",
      "type": "n8n-nodes-base.redis",
      "position": [
        -400,
        -160
      ],
      "parameters": {
        "key": "=chat_{{ $json.sessionId }}",
        "keyType": "string",
        "options": {},
        "operation": "get",
        "propertyName": "data"
      },
      "credentials": {
        "redis": {
          "id": "zU4DA70qSDrZM1El",
          "name": "Redis account (localhost)"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "81f53ae3-22b6-48cd-83b9-3c6c1041818c",
      "name": "当由另一个工作流执行时",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        340,
        -120
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "eventType"
            },
            {
              "name": "data",
              "type": "object"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "ade09ef9-b2bc-4cf0-84b5-7121623b4f7b",
      "name": "切换",
      "type": "n8n-nodes-base.switch",
      "position": [
        540,
        -120
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Get Auth Url",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "55d3d729-baf4-47ad-8262-5d23fc2d3348",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.eventType }}",
                    "rightValue": "get_auth_url"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "7ee9de6a-bb37-4adc-87a2-ddb745d17ab7",
      "name": "获取认证 URL 响应",
      "type": "n8n-nodes-base.set",
      "position": [
        740,
        -120
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "cf2b4fb3-cda0-47f9-b9fa-fda3532a04f5",
              "name": "auth_url",
              "type": "string",
              "value": "=https://<your-n8n-url>/form/auth?sessionId={{ $json.data.sessionId }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "880f7506-0947-47f1-ba0d-d08d6d18133b",
      "name": "获取认证 URL",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        40,
        40
      ],
      "parameters": {
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $workflow.id }}"
        },
        "description": "调用此工具生成登录 URL,允许访客用户以客户身份进行认证。",
        "workflowInputs": {
          "value": {
            "data": "={{\n{\n  \"sessionId\": $('When chat message received').first().json.sessionId\n}\n}}",
            "eventType": "get_auth_url"
          },
          "schema": [
            {
              "id": "eventType",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "eventType",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "data",
              "type": "object",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "data",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "800be1b0-5bec-4eaf-ac13-e6808b955408",
      "name": "更新会话",
      "type": "n8n-nodes-base.redis",
      "position": [
        1620,
        -120
      ],
      "parameters": {
        "key": "=chat_{{ $('Login Form').first().json.formQueryParameters.sessionId }}",
        "ttl": 3600,
        "value": "={{ $json.toJsonString() }}",
        "expire": true,
        "keyType": "string",
        "operation": "set"
      },
      "credentials": {
        "redis": {
          "id": "zU4DA70qSDrZM1El",
          "name": "Redis account (localhost)"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8b3d4728-2ae2-40dd-98ee-9efb915d06fc",
      "name": "确认登录成功",
      "type": "n8n-nodes-base.form",
      "position": [
        1800,
        -120
      ],
      "webhookId": "d939dc43-e333-4cb6-8bde-d6d8868ec9d3",
      "parameters": {
        "options": {},
        "operation": "completion",
        "completionTitle": "Login Successful!",
        "completionMessage": "=Welcome back {{ $json.name }},\nYour chat session is now authenticated.\n\nYou may now close this window and chat as a customer."
      },
      "typeVersion": 1
    },
    {
      "id": "5cb5797a-7f6c-4855-bcaa-4475ef16ec8d",
      "name": "客户支持代理",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -200,
        -160
      ],
      "parameters": {
        "text": "={{ $('When chat message received').item.json.chatInput }}",
        "options": {
          "systemMessage": "=You are a customer support assistant interacting with both anonymous guests and existing customers.\n* Guest users can ask basic questions about publicly available information.\n* Existing Customers can access their accounts, basket and profile.\n* Guests who ask about their accounts, basket or profile must authenticate as a customer before they can access this information. Provide a login url for the user and ask them to return once authenticated. Remind the user not to close the chat window as this will end the session!\n\n### About the login URL \nThe login url will take the user to an external form which when authenticated, will link their chat session with their customer profile. The user should close the form once done and return to the chat window. Upon their next message, their customer profile will be included in the context.\n* If you haven't already, thank the user for authenticating and welcome them back!\n* If the user claims to have authenticated but is still identified as a guest, then it may be that the login attempt was not successful and the user should be advised politely to try again.\n\n---\nThe current user is {{ $json.data ? 'a customer' : 'a guest' }}.\n{{ $json.data ?? '' }}",
          "passthroughBinaryImages": true
        },
        "promptType": "define"
      },
      "typeVersion": 1.9
    },
    {
      "id": "1b1f96f6-6f70-4b86-9831-0ac31a65dbdc",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -640,
        -380
      ],
      "parameters": {
        "color": 7,
        "width": 840,
        "height": 560,
        "content": "## 1. 创建具有会话访问权限的对话代理"
      },
      "typeVersion": 1
    },
    {
      "id": "b41e156c-a70f-422a-a37a-432e5657a999",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        240,
        -380
      ],
      "parameters": {
        "color": 7,
        "width": 720,
        "height": 560,
        "content": "## 2. 通过工具生成登录 URL"
      },
      "typeVersion": 1
    },
    {
      "id": "bd10ad35-7c72-480f-86fd-e15ee4011688",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1000,
        -380
      ],
      "parameters": {
        "color": 7,
        "width": 1020,
        "height": 560,
        "content": "## 3. 将会话链接到客户档案的登录表单"
      },
      "typeVersion": 1
    },
    {
      "id": "f13700b3-69cb-4809-b982-9204c2766948",
      "name": "获取客户档案",
      "type": "n8n-nodes-base.set",
      "position": [
        1440,
        -120
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "{\n  \"id\": 1,\n  \"name\": \"Jim Le\",\n  \"email\": \"jim@example.com\",\n  \"basket\": [\n    \"SanDisk Extreme Portable SSD 1TB (up to 1050 MB/s read, 1,000MB/s write, NVMe SSD, USB-C, External Solid State Drive, IP65 rated for dust and water resistance, Updated Firmware) Black - £84.99\",\n    \"eufy X10 Pro Omni Robot Vacuum Cleaner with Mop, All-in-One Station, 8,000Pa Powerful Suction, Dual Mops, AI Obstacle Avoidance, Auto Mop Washing, Auto-Hot-Air-Drying, Auto-Emptying, Auto-Refilling £799.00\"\n  ] \n}"
      },
      "typeVersion": 3.4
    },
    {
      "id": "66c28c7e-7ff1-4aac-8fbf-35e8c8105a65",
      "name": "替换我!",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1260,
        -120
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "e58f3c0f-c441-4c89-9a19-2c3459733dee",
      "name": "登录表单",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        1080,
        -120
      ],
      "webhookId": "cac0738d-f211-41de-bc6e-6f36103d9c1a",
      "parameters": {
        "options": {
          "path": "auth",
          "ignoreBots": true,
          "buttonLabel": "Submit"
        },
        "formTitle": "Authenticate Chat Session",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Username",
              "requiredField": true
            },
            {
              "fieldLabel": "Password",
              "requiredField": true
            },
            {
              "fieldName": "sessionId",
              "fieldType": "hiddenField"
            }
          ]
        },
        "responseMode": "lastNode",
        "formDescription": "Please login to validate your current chat session."
      },
      "typeVersion": 2.2
    },
    {
      "id": "2f965c47-76e3-4100-8c48-2be4dccece49",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1220,
        60
      ],
      "parameters": {
        "color": 5,
        "width": 200,
        "height": 140,
        "content": "### 替换为认证逻辑!"
      },
      "typeVersion": 1
    },
    {
      "id": "1d5077eb-4a6b-4bcb-9a85-e525a6c013ab",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1160,
        -820
      ],
      "parameters": {
        "width": 460,
        "height": 1160,
        "content": "## 试试看!"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "LLM": {
      "ai_languageModel": [
        [
          {
            "node": "Customer Support Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Memory": {
      "ai_memory": [
        [
          {
            "node": "Customer Support Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Get Auth Url Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Login Form": {
      "main": [
        [
          {
            "node": "Replace Me!",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Session": {
      "main": [
        [
          {
            "node": "Customer Support Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Replace Me!": {
      "main": [
        [
          {
            "node": "Get Customer Profile",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Auth URL": {
      "ai_tool": [
        [
          {
            "node": "Customer Support Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Update Session": {
      "main": [
        [
          {
            "node": "Confirm Login Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Customer Profile": {
      "main": [
        [
          {
            "node": "Update Session",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Get Session",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - 客户支持, 人工智能

需要付费吗?

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

工作流信息
难度等级
高级
节点数量19
分类2
节点类型13
难度说明

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

作者
Jimleuk

Jimleuk

@jimleuk

Freelance consultant based in the UK specialising in AI-powered automations. I work with select clients tackling their most challenging projects. For business enquiries, send me an email at hello@jimle.uk LinkedIn: https://www.linkedin.com/in/jimleuk/ X/Twitter: https://x.com/jimle_uk

外部链接
在 n8n.io 查看

分享此工作流