8
n8n 中文网amn8n.com

使用PDF向量和Webhooks构建文档问答API

中级

这是一个Internal Wiki, AI RAG, Multimodal AI领域的自动化工作流,包含 11 个节点。主要使用 If, Code, Webhook, PdfVector, RespondToWebhook 等节点。 使用PDF向量和Webhooks构建文档问答API

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "placeholder"
  },
  "nodes": [
    {
      "id": "overview-note",
      "name": "API 概览",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        50,
        50
      ],
      "parameters": {
        "color": 5,
        "width": 350,
        "height": 160,
        "content": "## 🤖 文档问答 API"
      },
      "typeVersion": 1
    },
    {
      "id": "request-note",
      "name": "请求格式",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        450,
        450
      ],
      "parameters": {
        "width": 280,
        "height": 180,
        "content": "## 📥 API 请求"
      },
      "typeVersion": 1
    },
    {
      "id": "process-note",
      "name": "问答处理",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        850,
        450
      ],
      "parameters": {
        "width": 260,
        "height": 160,
        "content": "## 🔍 AI 处理"
      },
      "typeVersion": 1
    },
    {
      "id": "response-note",
      "name": "响应格式",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1150,
        450
      ],
      "parameters": {
        "color": 6,
        "width": 260,
        "height": 180,
        "content": "## 📤 API 响应"
      },
      "typeVersion": 1
    },
    {
      "id": "webhook-trigger",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "notes": "API endpoint for document Q&A",
      "position": [
        250,
        300
      ],
      "webhookId": "doc-qa-webhook",
      "parameters": {
        "path": "doc-qa",
        "httpMethod": "POST"
      },
      "typeVersion": 1
    },
    {
      "id": "validate-request",
      "name": "验证请求",
      "type": "n8n-nodes-base.code",
      "notes": "Validate and prepare request",
      "position": [
        450,
        300
      ],
      "parameters": {
        "jsCode": "// Validate incoming request\nconst body = $input.first().json.body;\nconst errors = [];\n\nif (!body.documentUrl && !body.documentId) {\n  errors.push('Either documentUrl or documentId is required');\n}\nif (!body.question) {\n  errors.push('Question is required');\n}\n\n// Generate session ID if not provided\nconst sessionId = body.sessionId || `session-${Date.now()}`;\n\nreturn [{\n  json: {\n    ...body,\n    sessionId,\n    valid: errors.length === 0,\n    errors,\n    timestamp: new Date().toISOString()\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "check-valid",
      "name": "有效请求?",
      "type": "n8n-nodes-base.if",
      "position": [
        650,
        300
      ],
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.valid }}",
              "value2": true
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "pdfvector-ask",
      "name": "PDF 向量 - 提问",
      "type": "n8n-nodes-pdfvector.pdfVector",
      "notes": "Get answer from document",
      "position": [
        850,
        250
      ],
      "parameters": {
        "url": "={{ $json.documentUrl }}",
        "prompt": "Answer the following question about this document or image: {{ $json.question }}",
        "resource": "document",
        "inputType": "url",
        "operation": "ask"
      },
      "typeVersion": 1
    },
    {
      "id": "format-success",
      "name": "格式化成功响应",
      "type": "n8n-nodes-base.code",
      "notes": "Prepare successful response",
      "position": [
        1050,
        250
      ],
      "parameters": {
        "jsCode": "// Prepare successful response\nconst answer = $json.answer;\nconst request = $node['Validate Request'].json;\n\n// Calculate confidence score based on answer length and keywords\nlet confidence = 0.8; // Base confidence\nif (answer.length > 100) confidence += 0.1;\nif (answer.toLowerCase().includes('specifically') || answer.toLowerCase().includes('according to')) confidence += 0.1;\nconfidence = Math.min(confidence, 1.0);\n\nreturn [{\n  json: {\n    success: true,\n    data: {\n      answer,\n      confidence,\n      sessionId: request.sessionId,\n      documentUrl: request.documentUrl,\n      question: request.question\n    },\n    metadata: {\n      processedAt: new Date().toISOString(),\n      responseTime: Date.now() - new Date(request.timestamp).getTime(),\n      creditsUsed: 1\n    }\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "format-error",
      "name": "格式化错误响应",
      "type": "n8n-nodes-base.code",
      "notes": "Prepare error response",
      "position": [
        850,
        350
      ],
      "parameters": {
        "jsCode": "// Prepare error response\nconst errors = $json.errors || ['An error occurred processing your request'];\n\nreturn [{\n  json: {\n    success: false,\n    errors,\n    message: 'Invalid request',\n    timestamp: new Date().toISOString()\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "webhook-response",
      "name": "发送响应",
      "type": "n8n-nodes-base.respondToWebhook",
      "notes": "Send API response",
      "position": [
        1250,
        300
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "responseHeaders": {
          "entries": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Validate Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid Request?": {
      "main": [
        [
          {
            "node": "PDF Vector - Ask Question",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Format Error Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Request": {
      "main": [
        [
          {
            "node": "Valid Request?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Error Response": {
      "main": [
        [
          {
            "node": "Send Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Success Response": {
      "main": [
        [
          {
            "node": "Send Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PDF Vector - Ask Question": {
      "main": [
        [
          {
            "node": "Format Success Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 内部知识库, AI RAG 检索增强, 多模态 AI

需要付费吗?

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

工作流信息
难度等级
中级
节点数量11
分类3
节点类型6
难度说明

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

作者
PDF Vector

PDF Vector

@pdfvector

A fully featured PDF APIs for developers - Parse any PDF or Word document, extract structured data, and access millions of academic papers - all through simple APIs.

外部链接
在 n8n.io 查看

分享此工作流