8
n8n 中文网amn8n.com

使用 OpenAI、LangChain 和 API 集成的工作流自动化初学者指南

高级

这是一个Content Creation, Multimodal AI领域的自动化工作流,包含 33 个节点。主要使用 If, Set, Code, Merge, Webhook 等节点。 使用 OpenAI、LangChain 和 API 集成的工作流自动化初学者指南

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
  • 可能需要目标 API 的认证凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "cfcdd4aa7acb600106c0a4bff4e6959e8e3b32b3b8cbc7d93531b2a4c9012a32"
  },
  "nodes": [
    {
      "id": "0a919d77-d00e-46a4-9dd4-c89f16cb5467",
      "name": "从此开始 - 手动触发器",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -400,
        288
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "af159130-a22f-4a30-a426-3c3dece6777b",
      "name": "欢迎说明",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -528,
        -128
      ],
      "parameters": {
        "color": 4,
        "width": 348,
        "height": 596,
        "content": "## 欢迎使用 n8n!"
      },
      "typeVersion": 1
    },
    {
      "id": "7e354aac-bf00-4137-b6a2-fb96971b27a9",
      "name": "设置初始数据",
      "type": "n8n-nodes-base.set",
      "position": [
        -48,
        288
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3.3
    },
    {
      "id": "597d33d2-acf4-4f37-8d9b-b91b8e632500",
      "name": "关于设置节点",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        -128
      ],
      "parameters": {
        "color": 5,
        "width": 340,
        "height": 592,
        "content": "## 设置节点"
      },
      "typeVersion": 1
    },
    {
      "id": "6ed38d45-da78-4c65-80c3-31622b13df7a",
      "name": "代码节点 - 自定义处理",
      "type": "n8n-nodes-base.code",
      "position": [
        320,
        288
      ],
      "parameters": {
        "jsCode": "// Code Node - Write custom JavaScript\n\n// Access all input items\nconst items = $input.all();\n\n// Process each item\nconst results = items.map((item, index) => {\n  const data = item.json;\n  \n  return {\n    json: {\n      // Keep original data\n      ...data,\n      // Add new calculated fields\n      doubled_number: data.sample_number * 2,\n      message_length: data.message.length,\n      processed_at: new Date().toISOString(),\n      item_index: index,\n      // Example of string manipulation\n      uppercase_message: data.message.toUpperCase(),\n      // Example array\n      tags: ['beginner', 'tutorial', 'n8n']\n    }\n  };\n});\n\nreturn results;"
      },
      "typeVersion": 2
    },
    {
      "id": "1721a18e-125b-4b05-a49a-61751809244d",
      "name": "关于代码节点",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        192,
        -176
      ],
      "parameters": {
        "color": 5,
        "width": 340,
        "height": 648,
        "content": "## 代码节点"
      },
      "typeVersion": 1
    },
    {
      "id": "b1b6de48-2d84-4133-a9ca-c40efd8d40cb",
      "name": "IF 条件 - 路由数据",
      "type": "n8n-nodes-base.if",
      "position": [
        688,
        288
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "condition-001",
              "operator": {
                "type": "number",
                "operation": "smaller"
              },
              "leftValue": "={{ $json.sample_number }}",
              "rightValue": 50
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "dd24f45a-9770-49ee-bf1d-dafce878acb1",
      "name": "关于 IF 节点",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        -160
      ],
      "parameters": {
        "color": 5,
        "width": 340,
        "height": 636,
        "content": "## IF 节点"
      },
      "typeVersion": 1
    },
    {
      "id": "982434c7-64a0-46b3-a3d0-ce388c5a3447",
      "name": "真分支数据",
      "type": "n8n-nodes-base.set",
      "position": [
        976,
        128
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3.3
    },
    {
      "id": "d418f9a5-197b-4754-a242-e41130fdfd37",
      "name": "假分支数据",
      "type": "n8n-nodes-base.set",
      "position": [
        976,
        320
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3.3
    },
    {
      "id": "35c673ad-958d-45fd-bf70-a659862ad007",
      "name": "合并两个分支",
      "type": "n8n-nodes-base.merge",
      "position": [
        1280,
        224
      ],
      "parameters": {
        "mode": "combine",
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "a8fa8326-b655-4f81-81b5-6188ac70213b",
      "name": "关于合并节点",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1152,
        -160
      ],
      "parameters": {
        "color": 5,
        "width": 340,
        "height": 540,
        "content": "## 合并节点"
      },
      "typeVersion": 1
    },
    {
      "id": "e68f97f4-e769-4e19-b005-81afe32e19bd",
      "name": "HTTP 请求 - 获取用户",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -16,
        944
      ],
      "parameters": {
        "url": "https://jsonplaceholder.typicode.com/users",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "aec22fa9-c3b0-493f-9e7d-18396bbc89e7",
      "name": "关于 HTTP 请求",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -144,
        480
      ],
      "parameters": {
        "color": 6,
        "width": 360,
        "height": 656,
        "content": "## HTTP 请求节点"
      },
      "typeVersion": 1
    },
    {
      "id": "2a386f39-b829-4fa6-9feb-a3514cdce8e0",
      "name": "拆分用户数组",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        320,
        944
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "data"
      },
      "typeVersion": 1
    },
    {
      "id": "b7224087-b8e0-4aa3-ac02-420d8cf48e85",
      "name": "关于拆分输出",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        224,
        480
      ],
      "parameters": {
        "color": 6,
        "width": 308,
        "height": 648,
        "content": "## 拆分输出节点"
      },
      "typeVersion": 1
    },
    {
      "id": "ae144427-e63f-47d6-9e3a-a01fff3eba3f",
      "name": "提取用户字段",
      "type": "n8n-nodes-base.set",
      "position": [
        576,
        944
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "assignment-001",
              "name": "user_name",
              "type": "string",
              "value": "={{ $json.name }}"
            },
            {
              "id": "assignment-002",
              "name": "user_email",
              "type": "string",
              "value": "={{ $json.email }}"
            },
            {
              "id": "assignment-003",
              "name": "company",
              "type": "string",
              "value": "={{ $json.company.name }}"
            }
          ]
        }
      },
      "typeVersion": 3.3
    },
    {
      "id": "9a02665d-32f7-41d3-8961-584d39a24fde",
      "name": "OpenAI API - 生成趣味事实",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        864,
        944
      ],
      "parameters": {
        "url": "https://api.openai.com/v1/chat/completions",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "gpt-4o-mini"
            },
            {
              "name": "messages",
              "value": "=[{\"role\": \"user\", \"content\": \"Generate a fun fact about {{ $json.user_name }}'s company: {{ $json.company }}\"}]"
            },
            {
              "name": "temperature",
              "value": "0.7"
            },
            {
              "name": "max_tokens",
              "value": "100"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "nodeCredentialType": "openAiApi"
      },
      "typeVersion": 4.2
    },
    {
      "id": "f06c7f4c-8577-472d-bdd2-ffea5d6f4abf",
      "name": "关于 OpenAI",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        736,
        480
      ],
      "parameters": {
        "color": 6,
        "width": 360,
        "height": 652,
        "content": "## OpenAI 集成"
      },
      "typeVersion": 1
    },
    {
      "id": "27523b11-3a92-4b5f-b457-811ee30e188f",
      "name": "Webhook - 接收数据",
      "type": "n8n-nodes-base.webhook",
      "position": [
        1344,
        944
      ],
      "webhookId": "88d6f248-451b-477a-b754-1b9ce326e67c",
      "parameters": {
        "path": "beginner-webhook",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "40cee779-e780-4766-a057-3457c27dc1ea",
      "name": "关于 Webhooks",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1264,
        480
      ],
      "parameters": {
        "color": 7,
        "width": 492,
        "height": 660,
        "content": "## Webhook 节点"
      },
      "typeVersion": 1
    },
    {
      "id": "5d91498e-32e9-4f9e-89aa-3d4a079f1109",
      "name": "发送 Webhook 响应",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1552,
        944
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={{ { \"status\": \"success\", \"received\": $json.body, \"processed_at\": $now.toISO() } }}"
      },
      "typeVersion": 1.1
    },
    {
      "id": "2bf3244b-8dfd-4cb8-8daa-47e6617d91f0",
      "name": "聊天触发器 - AI 助手",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -400,
        1568
      ],
      "webhookId": "beginner-chat-assistant",
      "parameters": {
        "public": true,
        "options": {
          "title": "n8n Beginner's Assistant",
          "subtitle": "Your AI helper for learning n8n",
          "responseMode": "lastNode"
        },
        "initialMessages": "Hello! I'm an n8n AI assistant. Ask me anything about n8n workflows!\n\nTry asking:\n- How do I use the IF node?\n- What's the Code node for?\n- How do I call external APIs?"
      },
      "typeVersion": 1.1
    },
    {
      "id": "ac85dfaf-f606-44b5-b2eb-65f3988f1874",
      "name": "关于聊天 + AI",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -528,
        1168
      ],
      "parameters": {
        "color": 3,
        "width": 380,
        "height": 576,
        "content": "## 聊天触发器 + AI 助手"
      },
      "typeVersion": 1
    },
    {
      "id": "597eee76-e669-439b-a063-d1efc915d67f",
      "name": "窗口缓冲内存",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        192,
        1536
      ],
      "parameters": {
        "contextWindowLength": 10
      },
      "typeVersion": 1.2
    },
    {
      "id": "29e89cad-0ecb-4bd4-9c70-1b11a78a8dad",
      "name": "OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        0,
        1536
      ],
      "parameters": {
        "options": {
          "maxTokens": 500,
          "temperature": 0.7
        }
      },
      "typeVersion": 1
    },
    {
      "id": "c835131a-07ec-4e77-82bc-aaf9644d17b7",
      "name": "工具:当前时间",
      "type": "@n8n/n8n-nodes-langchain.toolCode",
      "position": [
        448,
        1584
      ],
      "parameters": {
        "name": "get_current_time",
        "description": "返回当前日期和时间。当用户询问当前时间、日期、星期几等时使用此工具。"
      },
      "typeVersion": 1
    },
    {
      "id": "68818e55-db72-4660-9810-709877f2f90b",
      "name": "工具:计算器",
      "type": "@n8n/n8n-nodes-langchain.toolCode",
      "position": [
        640,
        1584
      ],
      "parameters": {
        "name": "calculate",
        "jsCode": "// Parse input string and calculate\nconst input = $input;\nconst expression = typeof input === 'string' ? input : JSON.stringify(input);\n\ntry {\n  // Simple eval for basic math (in production, use a proper math parser)\n  const result = eval(expression.replace(/[^0-9+\\-*/().\\s]/g, ''));\n  return JSON.stringify({ result: result, expression: expression });\n} catch (error) {\n  return JSON.stringify({ error: 'Invalid mathematical expression', input: expression });\n}",
        "description": "执行数学计算。输入应为数学表达式字符串,如 '5 + 3' 或 '10 * 2'。"
      },
      "typeVersion": 1
    },
    {
      "id": "9bfbc587-b48c-445d-afa4-32ed86183ce5",
      "name": "AI 助手 - n8n 助手",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        0,
        1232
      ],
      "parameters": {
        "options": {
          "systemMessage": "=You are a helpful n8n workflow assistant for beginners.\n\nYour role:\n- Explain n8n concepts in simple terms\n- Provide examples and best practices\n- Help users understand nodes and their connections\n- Be encouraging and patient\n\nKey Topics:\n- Triggers (Manual, Webhook, Schedule, Chat)\n- Data Processing (Set, Code, Edit Fields)\n- Logic (IF, Switch, Merge)\n- Integrations (HTTP Request, APIs)\n- AI Features (Chat Trigger, AI Agent, Tools)\n\nAlways:\n- Use clear, beginner-friendly language\n- Provide practical examples\n- Suggest next learning steps"
        },
        "hasOutputParser": true
      },
      "typeVersion": 1.7
    },
    {
      "id": "3c1d13db-6558-4296-9928-cebe9163de4c",
      "name": "关于 AI 工具",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        400,
        1152
      ],
      "parameters": {
        "color": 3,
        "width": 360,
        "height": 588,
        "content": "## AI 助手工具"
      },
      "typeVersion": 1
    },
    {
      "id": "098527ad-4c10-4865-8fd4-922bf0e8a3ef",
      "name": "Schedule - Every Monday 9 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -400,
        976
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 9 * * 1"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "051f647b-7a26-495a-b268-3c32f4b97185",
      "name": "About Schedules",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -528,
        480
      ],
      "parameters": {
        "color": 7,
        "width": 360,
        "height": 660,
        "content": "## Schedule Trigger\n\n**Purpose:** Run workflows on schedule\n\n**Options:**\n- **Seconds/Minutes** - Short intervals\n- **Hours** - Hourly tasks\n- **Days** - Daily jobs\n- **Weeks** - Weekly reports\n- **Cron** - Custom schedules\n\n**Example Cron:**\n- `0 9 * * 1` - Monday 9 AM\n- `0 */6 * * *` - Every 6 hours\n- `0 0 1 * *` - First day of month\n\n**Use Cases:**\n- Daily reports\n- Data synchronization\n- Cleanup tasks"
      },
      "typeVersion": 1
    },
    {
      "id": "1d7cb17d-2ef8-4500-8199-e48e3505b831",
      "name": "Final Summary",
      "type": "n8n-nodes-base.set",
      "position": [
        1552,
        224
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "final-summary",
              "name": "workflow_summary",
              "type": "string",
              "value": "=Workflow executed successfully!\n\nProcessed items: {{ $items().length }}\nTimestamp: {{ $now.toISO() }}\n\nThis beginner workflow demonstrated:\n- Data creation and manipulation\n- Conditional logic\n- API integrations\n- AI capabilities\n\nNext: Explore n8n documentation and build your own workflows!"
            }
          ]
        }
      },
      "typeVersion": 3.3
    }
  ],
  "pinData": {},
  "connections": {
    "Set Initial Data": {
      "main": [
        [
          {
            "node": "Code Node - Custom Processing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tool: Calculator": {
      "ai_tool": [
        [
          {
            "node": "AI Agent - n8n Assistant",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "True Branch Data": {
      "main": [
        [
          {
            "node": "Merge Both Branches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "False Branch Data": {
      "main": [
        [
          {
            "node": "Merge Both Branches",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent - n8n Assistant",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Split Users Array": {
      "main": [
        [
          {
            "node": "Extract User Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tool: Current Time": {
      "ai_tool": [
        [
          {
            "node": "AI Agent - n8n Assistant",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Extract User Fields": {
      "main": [
        [
          {
            "node": "OpenAI API - Generate Fun Fact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Both Branches": {
      "main": [
        [
          {
            "node": "Final Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Window Buffer Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent - n8n Assistant",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - Receive Data": {
      "main": [
        [
          {
            "node": "Send Webhook Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Condition - Route Data": {
      "main": [
        [
          {
            "node": "True Branch Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "False Branch Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Fetch Users": {
      "main": [
        [
          {
            "node": "Split Users Array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chat Trigger - AI Assistant": {
      "main": [
        [
          {
            "node": "AI Agent - n8n Assistant",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Here - Manual Trigger": {
      "main": [
        [
          {
            "node": "Set Initial Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code Node - Custom Processing": {
      "main": [
        [
          {
            "node": "IF Condition - Route Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - 内容创作, 多模态 AI

需要付费吗?

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

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

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

作者

AI Developer & Automation Consultant

外部链接
在 n8n.io 查看

分享此工作流