8
n8n 中文网amn8n.com

使用GPT-4o问答助手分析文档和网页内容

中级

这是一个自动化工作流,包含 12 个节点。主要使用 If, Code, HttpRequest, ReadBinaryFile, Agent 等节点。 使用GPT-4o问答助手分析文档和网页内容

前置要求
  • 可能需要目标 API 的认证凭证
  • OpenAI API Key

分类

-
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "NMAA4tOidWSLW3On",
  "meta": {
    "instanceId": "4a409a91d2a3d01c353bbc5fe897fb8ad93edeeea7f4f1264dcf7a9735497f14",
    "templateCredsSetupCompleted": true
  },
  "name": "文档分析器和问答",
  "tags": [],
  "nodes": [
    {
      "id": "8ec9d857-a965-47e0-a367-3172a1056232",
      "name": "文档问答聊天",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        192,
        208
      ],
      "webhookId": "simple-doc-analyzer-chat",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "aaaefabe-e8fe-45b0-b841-109afc0049b3",
      "name": "解析文档和问题",
      "type": "n8n-nodes-base.code",
      "position": [
        432,
        208
      ],
      "parameters": {
        "jsCode": "const chatInput = $input.first().json.chatInput;\nconst parts = chatInput.split('|').map(part => part.trim());\n\nif (parts.length < 2) {\n  throw new Error('Please provide input in format: \"document_path_or_url | your_question\"\\nExamples:\\n- \"/Users/docs/readme.md | What is this project about?\"\\n- \"https://docs.example.com/api | What are the endpoints?\"');\n}\n\nconst documentPath = parts[0];\nconst userQuestion = parts.slice(1).join('|').trim();\n\nif (!documentPath || documentPath.length < 3) {\n  throw new Error('Please provide a valid document path or URL');\n}\n\nif (!userQuestion || userQuestion.length < 5) {\n  throw new Error('Please provide a detailed question (minimum 5 characters)');\n}\n\nconst isUrl = documentPath.startsWith('http://') || documentPath.startsWith('https://');\n\nlet fileType = 'unknown';\nlet inputType = 'file';\n\nif (isUrl) {\n  inputType = 'url';\n  fileType = 'html';\n  \n  const urlParts = documentPath.split('.');\n  if (urlParts.length > 1) {\n    const possibleExtension = urlParts[urlParts.length - 1].split(/[?#]/)[0].toLowerCase();\n    const supportedTypes = ['pdf', 'md', 'txt', 'doc', 'docx', 'json', 'yaml', 'yml'];\n    if (supportedTypes.includes(possibleExtension)) {\n      fileType = possibleExtension;\n    }\n  }\n} else {\n  const fileExtension = documentPath.split('.').pop().toLowerCase();\n  const supportedTypes = ['pdf', 'md', 'txt', 'doc', 'docx', 'json', 'yaml', 'yml'];\n  \n  if (!supportedTypes.includes(fileExtension)) {\n    throw new Error(`Unsupported file type: ${fileExtension}. Supported types: ${supportedTypes.join(', ')}`);\n  }\n  \n  fileType = fileExtension;\n}\n\nreturn {\n  documentPath: documentPath,\n  userQuestion: userQuestion,\n  fileType: fileType,\n  inputType: inputType,\n  isUrl: isUrl,\n  timestamp: new Date().toISOString()\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "8ad92e8a-54e5-4896-84aa-a054abc5c7f3",
      "name": "文件路径检查",
      "type": "n8n-nodes-base.if",
      "position": [
        672,
        96
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "file-condition",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              },
              "leftValue": "={{ $json.isUrl }}",
              "rightValue": false
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "afd1bad4-bc51-4f3d-8099-98dd9de1aac4",
      "name": "读取文档文件",
      "type": "n8n-nodes-base.readBinaryFile",
      "position": [
        832,
        96
      ],
      "parameters": {
        "filePath": "={{ $json.documentPath }}"
      },
      "typeVersion": 1
    },
    {
      "id": "c7024fab-3b1c-4892-abab-152a91c94b53",
      "name": "URL 检查",
      "type": "n8n-nodes-base.if",
      "position": [
        672,
        320
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "url-condition",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              },
              "leftValue": "={{ $json.isUrl }}",
              "rightValue": true
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "bdb7e2fd-1441-4cac-a66e-dc20d8f04aeb",
      "name": "获取网页内容",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        832,
        320
      ],
      "parameters": {
        "url": "={{ $json.documentPath }}",
        "options": {
          "timeout": 30000,
          "redirect": {
            "redirect": {
              "maxRedirects": 5
            }
          },
          "response": {
            "response": {
              "responseFormat": "text"
            }
          }
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "User-Agent",
              "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
            },
            {
              "name": "Accept",
              "value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
            },
            {
              "name": "Accept-Language",
              "value": "en-US,en;q=0.9"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "9a96077e-2158-42c0-853f-3dd43188c28d",
      "name": "提取文档内容",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        992,
        96
      ],
      "parameters": {
        "options": {},
        "operation": "text"
      },
      "typeVersion": 1
    },
    {
      "id": "053e4752-6062-4641-8119-3d7aacbf6af1",
      "name": "处理文档内容",
      "type": "n8n-nodes-base.code",
      "position": [
        1200,
        208
      ],
      "parameters": {
        "jsCode": "const fileType = $node['Parse Document & Question'].json.fileType;\nconst inputType = $node['Parse Document & Question'].json.inputType;\nconst isUrl = $node['Parse Document & Question'].json.isUrl;\nconst documentPath = $node['Parse Document & Question'].json.documentPath;\n\nlet extractedContent;\nlet processedContent;\n\nconst inputData = $input.first();\n\nif (isUrl) {\n  extractedContent = inputData.json.body || inputData.json.data || inputData.json || '';\n  if (fileType === 'html') {\n    const htmlString = typeof extractedContent === 'object' ? JSON.stringify(extractedContent) : String(extractedContent || '');\n    processedContent = htmlString\n      .replace(/<script[^>]*>[\\s\\S]*?<\\/script>/gi, '')\n      .replace(/<style[^>]*>[\\s\\S]*?<\\/style>/gi, '')\n      .replace(/<[^>]*>/g, ' ')\n      .replace(/&nbsp;/g, ' ')\n      .replace(/&amp;/g, '&')\n      .replace(/&lt;/g, '<')\n      .replace(/&gt;/g, '>')\n      .replace(/&quot;/g, '\"')\n      .replace(/&#39;/g, \"'\")\n      .replace(/\\s+/g, ' ')\n      .trim();\n  } else {\n    processedContent = typeof extractedContent === 'object' ? JSON.stringify(extractedContent) : String(extractedContent || '');\n  }\n} else {\n  extractedContent = inputData.json.data || inputData.json.text || inputData.json || inputData.binary?.data?.toString() || '';\n  processedContent = typeof extractedContent === 'object' ? JSON.stringify(extractedContent) : String(extractedContent || '');\n}\nswitch(fileType) {\n  case 'json':\n    try {\n      const jsonData = JSON.parse(processedContent);\n      processedContent = JSON.stringify(jsonData, null, 2);\n    } catch (e) {\n      processedContent = processedContent;\n    }\n    break;\n    \n  case 'yaml':\n  case 'yml':\n    break;\n    \n  case 'md':\n    processedContent = processedContent\n      .replace(/^\\s*```[^\\n]*\\n/gm, '\\n--- CODE BLOCK ---\\n')\n      .replace(/^\\s*```\\s*$/gm, '\\n--- END CODE BLOCK ---\\n');\n    break;\n    \n  case 'html':\n    break;\n    \n  default:\n    processedContent = processedContent\n      .replace(/\\s+/g, ' ')\n      .trim();\n}\n\nif (!processedContent || processedContent.length < 10) {\n  throw new Error(`Failed to extract meaningful content from ${isUrl ? 'web page' : 'document'}. Content may be empty, corrupted, or unsupported.`);\n}\n\nconst maxLength = 15000;\nconst originalLength = processedContent.length;\nif (processedContent.length > maxLength) {\n  processedContent = processedContent.substring(0, maxLength) + '\\n\\n[Content truncated for processing...]';\n}\n\nreturn {\n  documentContent: processedContent,\n  documentPath: documentPath,\n  fileType: fileType,\n  inputType: inputType,\n  isUrl: isUrl,\n  contentLength: processedContent.length,\n  isContentTruncated: originalLength > maxLength\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "ce10391d-1e7b-4a68-9708-690961633dd1",
      "name": "OpenAI 文档分析器",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1408,
        368
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "cjWgmpi35pveOOWw",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "62a54e68-13ae-468c-acf0-b164138a19c0",
      "name": "分析文档并回答",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1408,
        208
      ],
      "parameters": {
        "text": "=You are an expert document analysis assistant. Your task is to carefully read and understand the provided content, then answer the user's question accurately and comprehensively with well-formatted, human-readable output.\n\n**Content Information:**\n- Source: {{ $node['Parse Document & Question'].json.isUrl ? 'Web URL' : 'Local File' }}\n- Path/URL: {{ $node['Parse Document & Question'].json.documentPath }}\n- Content Type: {{ $node['Process Document Content'].json.fileType }}\n- Input Type: {{ $node['Process Document Content'].json.inputType }}\n- Content Length: {{ $node['Process Document Content'].json.contentLength }} characters\n- Content Truncated: {{ $node['Process Document Content'].json.isContentTruncated }}\n\n**Content:**\n{{ $node['Process Document Content'].json.documentContent }}\n\n**User Question:**\n{{ $node['Parse Document & Question'].json.userQuestion }}\n\n**Instructions:**\n1. Start your response with a header showing the source and question\n2. **Carefully read and analyze** the entire content provided above\n3. **Understand the context** and structure of the content\n4. **Answer the user's question** based on the content\n5. **Format your response** for maximum readability using proper formatting\n6. **Use tables, bullet points, and structured layouts** when appropriate\n7. **If the answer is not in the content**, clearly state that the information is not available\n8. **Provide context** around your answer when helpful\n9. **For web content**, focus on the main content and ignore navigation/footer elements\n\n**Response Formatting Requirements:**\n- Use bullet points (•) for lists and key points\n- Create tables when presenting structured data or comparisons\n- Use headings and subheadings for organization\n- Include numbered steps for processes or procedures\n- Quote relevant sections with proper formatting\n- Use emojis sparingly for visual appeal and clarity\n\n**Response Structure:**\n\n# 📊 Document Analysis Report\n\n## 🎯 Query Information\n• **Question Asked:** {{ $node['Parse Document & Question'].json.userQuestion }}\n• **Source Type:** {{ $node['Parse Document & Question'].json.isUrl ? '🌐 Web Page' : '📄 Local File' }}\n• **Source:** {{ $node['Parse Document & Question'].json.documentPath }}\n• **Analysis Status:** ✅ Complete\n\n---\n\n## 📋 **Direct Answer**\n[Provide a clear, concise answer to the user's question]\n\n## 📖 **Key Information**\n• [Main point 1]\n• [Main point 2] \n• [Main point 3]\n\n## 📊 **Detailed Analysis**\n[Use tables, bullet points, or structured format as appropriate]\n\n| Aspect | Details |\n|--------|----------|\n| [Key 1] | [Value 1] |\n| [Key 2] | [Value 2] |\n\n## 🔍 **Supporting Evidence**\n> \"[Relevant quote from content]\"\n\n• **Section Reference:** [Where this information was found]\n• **Context:** [Additional context if needed]\n\n## ⚠️ **Important Notes**\n• [Any limitations, caveats, or important considerations]\n• [Whether content was truncated and might affect completeness]\n\n## 💡 **Summary**\n[Brief summary if the response was complex]\n\n**Remember:** Format your response to be highly readable with proper structure, bullet points, tables, and clear organization. Base your answer ONLY on the content provided.",
        "options": {
          "systemMessage": "You are a helpful document analysis assistant that provides clear, well-formatted answers based on document content."
        },
        "promptType": "define"
      },
      "typeVersion": 2.1
    },
    {
      "id": "890ac685-4897-46aa-9e1a-e96566891b81",
      "name": "工作流信息",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        192,
        -352
      ],
      "parameters": {
        "color": 4,
        "width": 416,
        "height": 528,
        "content": "## 混合文档和网页分析器"
      },
      "typeVersion": 1
    },
    {
      "id": "149ed301-6176-4f63-9890-7b69aaf8b264",
      "name": "流水线信息",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        -160
      ],
      "parameters": {
        "color": 5,
        "width": 500,
        "height": 216,
        "content": "## 简化的处理流程"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "0b3b020f-958d-4e8c-acc8-5648076da1a5",
  "connections": {
    "c7024fab-3b1c-4892-abab-152a91c94b53": {
      "main": [
        [
          {
            "node": "bdb7e2fd-1441-4cac-a66e-dc20d8f04aeb",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "8ad92e8a-54e5-4896-84aa-a054abc5c7f3": {
      "main": [
        [
          {
            "node": "afd1bad4-bc51-4f3d-8099-98dd9de1aac4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "8ec9d857-a965-47e0-a367-3172a1056232": {
      "main": [
        [
          {
            "node": "aaaefabe-e8fe-45b0-b841-109afc0049b3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "bdb7e2fd-1441-4cac-a66e-dc20d8f04aeb": {
      "main": [
        [
          {
            "node": "053e4752-6062-4641-8119-3d7aacbf6af1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "afd1bad4-bc51-4f3d-8099-98dd9de1aac4": {
      "main": [
        [
          {
            "node": "9a96077e-2158-42c0-853f-3dd43188c28d",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "9a96077e-2158-42c0-853f-3dd43188c28d": {
      "main": [
        [
          {
            "node": "053e4752-6062-4641-8119-3d7aacbf6af1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ce10391d-1e7b-4a68-9708-690961633dd1": {
      "ai_languageModel": [
        [
          {
            "node": "62a54e68-13ae-468c-acf0-b164138a19c0",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "053e4752-6062-4641-8119-3d7aacbf6af1": {
      "main": [
        [
          {
            "node": "62a54e68-13ae-468c-acf0-b164138a19c0",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "aaaefabe-e8fe-45b0-b841-109afc0049b3": {
      "main": [
        [
          {
            "node": "8ad92e8a-54e5-4896-84aa-a054abc5c7f3",
            "type": "main",
            "index": 0
          },
          {
            "node": "c7024fab-3b1c-4892-abab-152a91c94b53",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级

需要付费吗?

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

工作流信息
难度等级
中级
节点数量12
分类-
节点类型9
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Aadarsh Jain

Aadarsh Jain

@aadarsh-jain

An enthusiast DevOps Engineer with 3+ years of experience in devops tools and automation.

外部链接
在 n8n.io 查看

分享此工作流

分类

分类: 34