8
n8n 中文网amn8n.com

构建您的第一个AI数据分析师聊天机器人

高级

这是一个Sales, Finance, Product, AI领域的自动化工作流,包含 29 个节点。主要使用 Code, Filter, Aggregate, HttpRequest, Agent 等节点,结合人工智能技术实现智能自动化。 构建您的第一个AI数据分析师聊天机器人

前置要求
  • 可能需要目标 API 的认证凭证
  • Google Sheets API 凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "d6b502dfa4d9dd072cdc5c2bb763558661053f651289291352a84403e01b3d1b",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "0951fd33-1811-4a89-b84f-f46dc9e6fde1",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        20,
        -340
      ],
      "webhookId": "cdc03fce-33b6-4eed-86b5-f628994e5e31",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "699c2f89-5547-4d28-92a9-5e216aecb251",
      "name": "AI 代理",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        240,
        -340
      ],
      "parameters": {
        "options": {
          "maxIterations": 15,
          "systemMessage": "=You are a helpful assistant.\nCurrent timestamp is {{ $now }}"
        }
      },
      "typeVersion": 1.7
    },
    {
      "id": "640c29f7-b67e-49f6-a864-c9b396c446b7",
      "name": "OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        160,
        -100
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o",
          "cachedResultName": "gpt-4o"
        },
        "options": {
          "temperature": 0.2
        }
      },
      "credentials": {
        "openAiApi": {
          "id": "5LVOlVwHUgB8MAj2",
          "name": "OpenAI - n8n project"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "807630b4-c138-4b66-a438-fb70eab12a07",
      "name": "计算器",
      "type": "@n8n/n8n-nodes-langchain.toolCalculator",
      "position": [
        840,
        60
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "132a97a3-239c-403f-843f-55b652e3efc5",
      "name": "代码",
      "type": "n8n-nodes-base.code",
      "position": [
        840,
        640
      ],
      "parameters": {
        "jsCode": "// Ensure there's at least one input item.\nif (!items || items.length === 0) {\n  throw new Error(\"No input items found.\");\n}\n\n// Our input is expected to have a 'data' property containing the JSONP string.\nconst input = items[0].json;\n\nif (!input.data) {\n  throw new Error(\"Input JSON does not have a 'data' property.\");\n}\n\nconst rawData = input.data;\n\n// Use a regex to extract the JSON content from the Google Visualization JSONP response.\nconst regex = /google\\.visualization\\.Query\\.setResponse\\((.*)\\);?$/s;\nconst match = rawData.match(regex);\n\nif (!match) {\n  throw new Error(\"Input data does not match the expected Google Visualization JSONP format.\");\n}\n\nconst jsonString = match[1];\n\n// Parse the extracted JSON string.\nlet parsed;\ntry {\n  parsed = JSON.parse(jsonString);\n} catch (error) {\n  throw new Error(\"Failed to parse JSON: \" + error.message);\n}\n\n// Verify that the parsed JSON has the expected 'table' structure with 'cols' and 'rows'.\nif (!parsed.table || !Array.isArray(parsed.table.cols) || !Array.isArray(parsed.table.rows)) {\n  throw new Error(\"Parsed JSON does not have the expected 'table' structure with 'cols' and 'rows'.\");\n}\n\nconst cols = parsed.table.cols;\nconst rows = parsed.table.rows;\n\n// Helper function to convert date string from \"Date(YYYY,M,D)\" to \"YYYY-MM-DD\"\nfunction formatDate(dateStr) {\n  const match = dateStr.match(/^Date\\((\\d+),(\\d+),(\\d+)\\)$/);\n  if (!match) return dateStr;\n  const year = parseInt(match[1], 10);\n  const month = parseInt(match[2], 10) + 1; // JavaScript months are 0-indexed\n  const day = parseInt(match[3], 10);\n  // Format with leading zeros\n  return `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`;\n}\n\n// Map each row into an object using the column labels as keys.\nconst newItems = rows.map(row => {\n  const obj = {};\n  cols.forEach((col, index) => {\n    let value = row.c && row.c[index] ? row.c[index].v : null;\n    // If the column type is \"date\" and the value is a string that looks like \"Date(YYYY,M,D)\"\n    if (col.type === \"date\" && typeof value === \"string\") {\n      value = formatDate(value);\n    }\n    obj[col.label] = value;\n  });\n  return { json: obj };\n});\n\n// Return the new array of items.\nreturn newItems;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "3dc1e670-bfb1-4b63-b9c8-85656134c843",
      "name": "当由另一个工作流执行时",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        280,
        640
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "start_date"
            },
            {
              "name": "end_date"
            },
            {
              "name": "status"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "52a26e43-12a5-4b4a-a224-d70cdabf6aaf",
      "name": "按日期记录",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "position": [
        1020,
        -120
      ],
      "parameters": {
        "name": "records_by_date_and_or_status",
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "a2BIIjr2gLBay06M",
          "cachedResultName": "Template | Your first AI Data Analyst"
        },
        "description": "使用此工具获取按日期筛选的记录。如果需要,您也可以同时按状态筛选。",
        "workflowInputs": {
          "value": {
            "status": "={{ $fromAI(\"status\", \"Status of the transaction. Can be Completed, Refund or Error. Leave empty if you don't need this now.\", \"string\") }}",
            "end_date": "={{ $fromAI(\"end_date\", \"End date in format YYYY-MM-DD\", \"string\") }}",
            "start_date": "={{ $fromAI(\"start_date\", \"Start date in format YYYY-MM-DD\", \"string\") }}"
          },
          "schema": [
            {
              "id": "start_date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "start_date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "end_date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "end_date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      },
      "typeVersion": 2
    },
    {
      "id": "e1811519-8699-4243-8c64-0db1ab26004d",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1280,
        640
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "3b129abd-ac9a-460c-abb3-007e2c94e284",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1220,
        400
      ],
      "parameters": {
        "color": 7,
        "width": 220,
        "height": 400,
        "content": "要将所有项目发送回 AI,我们需要将所有内容聚合到单个项目中完成。"
      },
      "typeVersion": 1
    },
    {
      "id": "645ac0f9-8022-4f2c-8c6c-5aadd6cf09cc",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        460,
        400
      ],
      "parameters": {
        "color": 7,
        "width": 300,
        "height": 400,
        "content": "此节点向 Google Sheets API 发送自定义 HTTP 请求。"
      },
      "typeVersion": 1
    },
    {
      "id": "14221a72-914d-4c75-866a-d64ba7f8109f",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        780,
        400
      ],
      "parameters": {
        "color": 7,
        "width": 220,
        "height": 400,
        "content": "这个复杂请求的输出也很混乱。"
      },
      "typeVersion": 1
    },
    {
      "id": "f12668ea-b59d-4caf-a997-381f78b7cfe7",
      "name": "Google Sheets 请求",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        560,
        640
      ],
      "parameters": {
        "url": "https://docs.google.com/spreadsheets/d/18A4d7KYrk8-uEMbu7shoQe_UIzmbTLV1FMN43bjA7qc/gviz/tq",
        "options": {},
        "sendQuery": true,
        "authentication": "predefinedCredentialType",
        "queryParameters": {
          "parameters": [
            {
              "name": "sheet",
              "value": "Sheet1"
            },
            {
              "name": "tq",
              "value": "=SELECT * WHERE A >= DATE \"{{ $json.start_date }}\" AND A <= DATE \"{{ $json.end_date }}\""
            }
          ]
        },
        "nodeCredentialType": "googleSheetsOAuth2Api"
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YR4pbjuZM5Xs4CTD",
          "name": "Google Sheets"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "f59a2606-0981-43d1-9a07-b802891b9220",
      "name": "按产品名称获取交易",
      "type": "n8n-nodes-base.googleSheetsTool",
      "position": [
        1020,
        -320
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $fromAI(\"product_name\", \"The product name\", \"string\") }}",
              "lookupColumn": "Product"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/18A4d7KYrk8-uEMbu7shoQe_UIzmbTLV1FMN43bjA7qc/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "https://docs.google.com/spreadsheets/d/18A4d7KYrk8-uEMbu7shoQe_UIzmbTLV1FMN43bjA7qc/edit?usp=sharing"
        },
        "descriptionType": "manual",
        "toolDescription": "Find transactions by product.\nOur products are:\n- Widget A\n- Widget B\n- Widget C\n- Widget D"
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YR4pbjuZM5Xs4CTD",
          "name": "Google Sheets"
        }
      },
      "typeVersion": 4.5
    },
    {
      "id": "1ed7168c-1639-4b3b-a3b4-ed162bcef880",
      "name": "获取所有交易",
      "type": "n8n-nodes-base.googleSheetsTool",
      "position": [
        840,
        -120
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/18A4d7KYrk8-uEMbu7shoQe_UIzmbTLV1FMN43bjA7qc/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "https://docs.google.com/spreadsheets/d/18A4d7KYrk8-uEMbu7shoQe_UIzmbTLV1FMN43bjA7qc/edit?usp=sharing"
        },
        "descriptionType": "manual",
        "toolDescription": "Only use this as last resort, because it will pull all data at once."
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YR4pbjuZM5Xs4CTD",
          "name": "Google Sheets"
        }
      },
      "typeVersion": 4.5
    },
    {
      "id": "798453da-8a65-4d14-ae0a-778d64ab02ad",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -360,
        -340
      ],
      "parameters": {
        "color": 4,
        "width": 320,
        "height": 340,
        "content": "## 可以尝试的问题"
      },
      "typeVersion": 1
    },
    {
      "id": "b8336f1a-3855-4247-9589-2f9aa35d211f",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -780,
        -340
      ],
      "parameters": {
        "color": 4,
        "width": 400,
        "content": "## 将此 Sheets 文件复制到您的 Google Drive"
      },
      "typeVersion": 1
    },
    {
      "id": "99a55b39-965b-4454-b416-d3991f0bdfbc",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        940,
        60
      ],
      "parameters": {
        "color": 7,
        "width": 200,
        "height": 140,
        "content": "### 👈"
      },
      "typeVersion": 1
    },
    {
      "id": "7ebebf56-e065-41c4-8270-f636785b0def",
      "name": "### 需要帮助?",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -780,
        -160
      ],
      "parameters": {
        "color": 4,
        "width": 400,
        "content": "### 如何连接到 Google Sheets?"
      },
      "typeVersion": 1
    },
    {
      "id": "b64df0dd-6425-4fc2-9f60-8c5a85412d61",
      "name": "## 试试看!",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        120,
        20
      ],
      "parameters": {
        "color": 7,
        "width": 170,
        "height": 260,
        "content": "## 👆"
      },
      "typeVersion": 1
    },
    {
      "id": "23c7bb52-b189-45f1-949b-ea588f065583",
      "name": "GET 模型",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        340,
        20
      ],
      "parameters": {
        "color": 7,
        "width": 150,
        "height": 260,
        "content": "## 👆"
      },
      "typeVersion": 1
    },
    {
      "id": "6097e5a1-139b-4329-81ff-4fda16ea5221",
      "name": "缓冲区记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        360,
        -100
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "6de4a7f2-5c58-4401-bd7c-19c5a73ba775",
      "name": "## 1. 创建新的自定义 OpenAI 凭据",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1160,
        -320
      ],
      "parameters": {
        "color": 7,
        "width": 340,
        "height": 180,
        "content": "**AI 工具代理** 可以同时访问所有工具。它使用名称和描述来决定何时使用每个工具。"
      },
      "typeVersion": 1
    },
    {
      "id": "a308d895-bc18-4b2c-9567-78f6c29f79e8",
      "name": "便签 11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1160,
        -120
      ],
      "parameters": {
        "color": 7,
        "width": 340,
        "height": 320,
        "content": "## 👈 这是一个特殊工具"
      },
      "typeVersion": 1
    },
    {
      "id": "0a6d94bc-21e1-4949-b7f4-c93abbecf08c",
      "name": "便签 12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        120,
        340
      ],
      "parameters": {
        "color": 7,
        "width": 1380,
        "height": 520,
        "content": "# 子工作流"
      },
      "typeVersion": 1
    },
    {
      "id": "3e424615-6e49-4bd3-b066-005b9f0f773e",
      "name": "按状态筛选",
      "type": "n8n-nodes-base.filter",
      "position": [
        1060,
        640
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "e50da873-bbbd-41d3-a418-83193907977c",
              "operator": {
                "type": "string",
                "operation": "contains"
              },
              "leftValue": "={{ $json.Status }}",
              "rightValue": "={{ $('When Executed by Another Workflow').item.json.status }}"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "0ad0102c-adb9-4ec9-bdf3-b1ce425b88ba",
      "name": "按状态获取交易",
      "type": "n8n-nodes-base.googleSheetsTool",
      "position": [
        840,
        -320
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $fromAI(\"transaction_status\", \"Transaction status can be Refund, Completed or Error\", \"string\") }}",
              "lookupColumn": "Status"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/18A4d7KYrk8-uEMbu7shoQe_UIzmbTLV1FMN43bjA7qc/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": "https://docs.google.com/spreadsheets/d/18A4d7KYrk8-uEMbu7shoQe_UIzmbTLV1FMN43bjA7qc/edit?usp=sharing"
        },
        "descriptionType": "manual",
        "toolDescription": "Find transactions by status"
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YR4pbjuZM5Xs4CTD",
          "name": "Google Sheets"
        }
      },
      "typeVersion": 4.5
    },
    {
      "id": "5b80cb08-6e19-47b2-8146-c299e709a34a",
      "name": "便利贴13",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        820,
        -540
      ],
      "parameters": {
        "color": 4,
        "width": 300,
        "content": "## 在所有 Sheets 工具中更改 Sheets 文件的 URL 👇"
      },
      "typeVersion": 1
    },
    {
      "id": "ddc1351e-0ad0-480f-9742-30f2aa860d61",
      "name": "便签14",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        500,
        820
      ],
      "parameters": {
        "color": 4,
        "width": 260,
        "height": 100,
        "content": "## 👆 更改 Sheets 文件的 URL"
      },
      "typeVersion": 1
    },
    {
      "id": "ab837a10-932f-4b14-8e2c-546077ca2c86",
      "name": "便签10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -780,
        20
      ],
      "parameters": {
        "color": 7,
        "width": 740,
        "height": 580,
        "content": "# 作者"
      },
      "typeVersion": 1
    },
    {
      "id": "e58351b3-3b18-4c03-9435-27ba853d03bb",
      "name": "便签15",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -780,
        620
      ],
      "parameters": {
        "width": 740,
        "height": 180,
        "content": "# 需要帮助?"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Filter by status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculator": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Buffer Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Records by date": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Filter by status": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Get all transactions": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets request": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get transactions by status": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get transactions by product name": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Google Sheets request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - 销售, 财务, 产品, 人工智能

需要付费吗?

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

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

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

作者
Solomon

Solomon

@solomon

Freelance consultant from Brazil, specializing in automations and data analysis. I work with select clients, addressing their toughest projects. For business inquiries, email me at automations.solomon@gmail.com or message me on Telegram for a faster response.

外部链接
在 n8n.io 查看

分享此工作流