8
n8n 中文网amn8n.com

A/B 拆分测试

高级

这是一个Building Blocks, AI领域的自动化工作流,包含 16 个节点。主要使用 If, Set, Supabase, Agent, ChatTrigger 等节点,结合人工智能技术实现智能自动化。 使用 Supabase 和 OpenAI 拆分测试不同智能体提示

前置要求
  • Supabase URL 和 API Key
  • OpenAI API Key
  • PostgreSQL 数据库连接信息
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "TEA7K9MSVQGCWKe6",
  "meta": {
    "instanceId": "ac63467607103d9c95dd644384984672b90b1cb03e07edbaf18fe72b2a6c45bb",
    "templateCredsSetupCompleted": true
  },
  "name": "A/B 拆分测试",
  "tags": [],
  "nodes": [
    {
      "id": "e8404493-4297-4169-a72f-89e668ae5fbc",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -1460,
        -140
      ],
      "webhookId": "334e3a8d-73d2-4d3c-9927-158c1169ef5e",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "582e1c1b-12ff-42ff-8130-48f94eebd706",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        220,
        -160
      ],
      "parameters": {
        "text": "={{ $('When chat message received').item.json.chatInput }}",
        "options": {
          "systemMessage": "={{ $json.prompt }}"
        },
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "39ca5c70-11d4-4f86-bde5-0f9827297be9",
      "name": "检查会话是否存在",
      "type": "n8n-nodes-base.supabase",
      "position": [
        -960,
        -140
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "session_id",
              "keyValue": "={{ $('When chat message received').item.json.sessionId }}"
            }
          ]
        },
        "tableId": "split_test_sessions",
        "operation": "get"
      },
      "credentials": {
        "supabaseApi": {
          "id": "1iEg1EzFrF29iqp2",
          "name": "Supabase (bsde.ai)"
        }
      },
      "executeOnce": false,
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "35f2c270-9571-41ba-ab7c-47a6742d7d90",
      "name": "如果会话存在",
      "type": "n8n-nodes-base.if",
      "position": [
        -720,
        -140
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "4270c464-6874-45d2-aa3b-606f45544c3d",
              "operator": {
                "type": "number",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "ec00ad92-96e9-4936-a547-2a2715ff5c32",
      "name": "为会话分配路径",
      "type": "n8n-nodes-base.supabase",
      "position": [
        -400,
        -20
      ],
      "parameters": {
        "tableId": "split_test_sessions",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "show_alternative",
              "fieldValue": "={{ Math.random() < 0.5 }}"
            },
            {
              "fieldId": "session_id",
              "fieldValue": "={{ $('When chat message received').item.json.sessionId }}"
            }
          ]
        }
      },
      "credentials": {
        "supabaseApi": {
          "id": "1iEg1EzFrF29iqp2",
          "name": "Supabase (bsde.ai)"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "92ee7145-30ae-41e9-bc04-eef03b84485e",
      "name": "定义路径值",
      "type": "n8n-nodes-base.set",
      "position": [
        -1200,
        -140
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "9581a184-a120-4b1f-8408-cfe97520d107",
              "name": "baseline_value",
              "type": "string",
              "value": "The dog's name is Ben"
            },
            {
              "id": "1752f2c4-4ce4-4893-b8db-1c59131c298a",
              "name": "alternative_value",
              "type": "string",
              "value": "The dog's name is Tom"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "23c1b4e2-2ba2-4237-bb4b-b92da127d201",
      "name": "OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        300,
        60
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "1OMpAMAKR9l3eUDI",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "be3f14b9-68c7-457d-b5bf-a6abbadf5b67",
      "name": "Postgres 聊天记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryPostgresChat",
      "position": [
        480,
        60
      ],
      "parameters": {
        "tableName": "n8n_split_test_chat_histories",
        "sessionKey": "={{ $('When chat message received').item.json.sessionId }}",
        "sessionIdType": "customKey"
      },
      "credentials": {
        "postgres": {
          "id": "tzLXHvhykxvYghPC",
          "name": "bsde.ai Supabase (Session Pooler)"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "1c20d274-2482-4551-a4ea-64860eb35276",
      "name": "便签 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1520,
        -260
      ],
      "parameters": {
        "color": 7,
        "width": 220,
        "height": 300,
        "content": "## 1. 接收消息"
      },
      "typeVersion": 1
    },
    {
      "id": "ee22d3b1-d447-4e35-8ac4-d093edf6deee",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1280,
        -340
      ],
      "parameters": {
        "color": 7,
        "width": 1340,
        "height": 500,
        "content": "## 2. 确定 LLM 提示词"
      },
      "typeVersion": 1
    },
    {
      "id": "7d90ec00-5fca-4b0d-bc1f-e8b8c179b960",
      "name": "便签 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        -240
      ],
      "parameters": {
        "color": 7,
        "width": 520,
        "height": 440,
        "content": "## 3. AI Agent"
      },
      "typeVersion": 1
    },
    {
      "id": "b9b9e0e8-53c1-4d6a-bbdc-c2a13d740dfb",
      "name": "获取正确提示词",
      "type": "n8n-nodes-base.set",
      "position": [
        -80,
        -160
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "08de68ec-0f12-43ee-98ab-59d8a414f114",
              "name": "prompt",
              "type": "string",
              "value": "={{ $json.show_alternative ? $('Define Path Values').item.json.alternative_value : $('Define Path Values').item.json.baseline_value }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "2b78ce9b-e6b4-4744-8ddf-00f8ae990fc8",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1260,
        -240
      ],
      "parameters": {
        "color": 5,
        "width": 220,
        "height": 260,
        "content": "### 修改"
      },
      "typeVersion": 1
    },
    {
      "id": "0646f176-407a-41ee-b602-34bd681fc421",
      "name": "便签8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        100,
        40
      ],
      "parameters": {
        "color": 5,
        "width": 340,
        "height": 140,
        "content": "### 修改"
      },
      "typeVersion": 1
    },
    {
      "id": "a391018c-5d28-4384-89e1-0435758a6945",
      "name": "便签 5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1480,
        -600
      ],
      "parameters": {
        "width": 520,
        "height": 240,
        "content": "### 设置"
      },
      "typeVersion": 1
    },
    {
      "id": "2382d146-f0c1-4de6-9e90-b17c304df692",
      "name": "便签 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2120,
        -360
      ],
      "parameters": {
        "width": 560,
        "height": 480,
        "content": ""
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "339c6f2f-e4d1-4922-9442-2c1a78e96067",
  "connections": {
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Define Path Values": {
      "main": [
        [
          {
            "node": "Check If Session Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Correct Prompt": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "If Session Does Exist": {
      "main": [
        [
          {
            "node": "Get Correct Prompt",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Assign Path To Session",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Path To Session": {
      "main": [
        [
          {
            "node": "Get Correct Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Session Exists": {
      "main": [
        [
          {
            "node": "If Session Does Exist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Define Path Values",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - 构建模块, 人工智能

需要付费吗?

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

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

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

作者
Chris Carr

Chris Carr

@chriscarr

Founder of bsde.ai, a workflow and AI agency based in the UK.

外部链接
在 n8n.io 查看

分享此工作流