8
n8n 中文网amn8n.com

RAG系统 V2 - 更新文档

高级

这是一个Document Extraction, AI RAG领域的自动化工作流,包含 22 个节点。主要使用 If, Set, Limit, Switch, Supabase 等节点。 通过Google Drive文件变更保持RAG系统在Supabase向量数据库中的更新

前置要求
  • Supabase URL 和 API Key
  • Google Drive API 凭证
  • 可能需要目标 API 的认证凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "YVAwvooLBNvqFhyp",
  "meta": {
    "instanceId": "90587241b61f532dbe71be441310f86b24bd62a247fd87ce3f4b074464fa220d",
    "templateCredsSetupCompleted": true
  },
  "name": "RAG 系统 V2 - 更新文档",
  "tags": [
    {
      "id": "X3XA7mO6MQZQ8uNT",
      "name": "RAG",
      "createdAt": "2024-12-23T14:38:50.002Z",
      "updatedAt": "2024-12-23T14:38:50.002Z"
    },
    {
      "id": "bQac7pgkCzmq5Nt5",
      "name": "template",
      "createdAt": "2025-01-14T11:43:47.391Z",
      "updatedAt": "2025-01-14T11:43:47.391Z"
    },
    {
      "id": "pms2jtgY49JCgVDW",
      "name": "chatbot",
      "createdAt": "2024-12-19T04:39:23.549Z",
      "updatedAt": "2024-12-19T04:39:23.549Z"
    }
  ],
  "nodes": [
    {
      "id": "f57918de-1d00-432b-b352-b6013118664a",
      "name": "设置文件 ID",
      "type": "n8n-nodes-base.set",
      "position": [
        220,
        2200
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "10646eae-ae46-4327-a4dc-9987c2d76173",
              "name": "file_id",
              "type": "string",
              "value": "={{ $('File Updated').item.json.id }}"
            },
            {
              "id": "f4536df5-d0b1-4392-bf17-b8137fb31a44",
              "name": "file_type",
              "type": "string",
              "value": "={{ $('File Updated').item.json.mimeType }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "2899dfbc-2fba-4f35-9805-384dc30eebab",
      "name": "删除旧文档行",
      "type": "n8n-nodes-base.supabase",
      "position": [
        600,
        2200
      ],
      "parameters": {
        "tableId": "documents",
        "operation": "delete",
        "filterType": "string",
        "filterString": "=metadata->>file_id=like.*{{ $('Set File ID').item.json.file_id }}*"
      },
      "credentials": {
        "supabaseApi": {
          "id": "J5U7ge3sFHh1WYka",
          "name": "Supabase API"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "aae30af6-491e-48f7-89cb-b6dd2ed5ed9e",
      "name": "下载文件",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        1540,
        2360
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Set File ID').item.json.file_id }}"
        },
        "options": {
          "googleFileConversion": {
            "conversion": {
              "docsToFormat": "text/plain"
            }
          }
        },
        "operation": "download"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yGiuxvKzLUzVYa2d",
          "name": "Google Drive account"
        }
      },
      "executeOnce": true,
      "typeVersion": 3
    },
    {
      "id": "efdfe418-a0bc-4db1-87e2-5558f00a48ec",
      "name": "如果",
      "type": "n8n-nodes-base.if",
      "onError": "continueRegularOutput",
      "position": [
        380,
        2200
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "928b5cae-77cd-48a2-81ef-2f76dc441b0a",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $('File Updated').item.json.mimeType }}",
              "rightValue": "application/vnd.google-apps.document"
            },
            {
              "id": "6368b5ad-32b0-491d-a27d-5cc3ad5c2a11",
              "operator": {
                "type": "number",
                "operation": "lt"
              },
              "leftValue": "={{ ((Date.now() - Date.parse($('File Updated').item.json.createdTime)) / 1000) }}",
              "rightValue": 60
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "19edee69-a34b-48c7-8f2e-c2547fb226ff",
      "name": "聚合",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        2300,
        2300
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "93ddcfd1-9320-4808-b636-ae28664f41a8",
      "name": "总结",
      "type": "n8n-nodes-base.summarize",
      "position": [
        2500,
        2300
      ],
      "parameters": {
        "options": {},
        "fieldsToSummarize": {
          "values": [
            {
              "field": "data",
              "aggregation": "concatenate"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f864d1f7-39a2-4d36-9dc5-d75fcc08ba7d",
      "name": "提取 PDF 文本1",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        2100,
        1960
      ],
      "parameters": {
        "options": {},
        "operation": "pdf"
      },
      "typeVersion": 1
    },
    {
      "id": "32d1f985-b143-451e-a6d1-bf3da9f568a1",
      "name": "从 Excel 提取1",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        2100,
        2300
      ],
      "parameters": {
        "options": {},
        "operation": "xlsx"
      },
      "typeVersion": 1
    },
    {
      "id": "2ea2790c-f4ab-4c59-b4b6-0a22e8047f48",
      "name": "转换为 Google 文档2",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2100,
        2500
      ],
      "parameters": {
        "url": "=https://www.googleapis.com/drive/v3/files/{{ $('Set File ID').item.json.file_id }}/copy",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "name",
              "value": "={{ $('Set File ID').item.json.name }}"
            },
            {
              "name": "mimeType",
              "value": "application/vnd.google-apps.document"
            }
          ]
        },
        "nodeCredentialType": "googleDriveOAuth2Api"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yGiuxvKzLUzVYa2d",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "69166736-3369-4a33-8543-fae475344593",
      "name": "删除文件1",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        2300,
        2500
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Set File ID').item.json.file_id }}"
        },
        "options": {},
        "operation": "deleteFile"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yGiuxvKzLUzVYa2d",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "e2574cdd-5d41-4be1-bca5-370d2a8e2ee2",
      "name": "递归字符文本分割器1",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        3080,
        2420
      ],
      "parameters": {
        "options": {},
        "chunkSize": 2000,
        "chunkOverlap": 200
      },
      "typeVersion": 1
    },
    {
      "id": "7dfe4ffd-0fda-4f7d-a005-4e4e9a62048e",
      "name": "OpenAI 嵌入2",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        2820,
        2280
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "9SPpIIxBvgYPZlo8",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "394d1238-bf7c-4882-b3ac-6db474140854",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -120,
        1820
      ],
      "parameters": {
        "color": 5,
        "width": 3507,
        "height": 1028,
        "content": "# 监视触发器(Drive)- 文件更新"
      },
      "typeVersion": 1
    },
    {
      "id": "270a7c5d-b9e1-48c1-aa0d-76677c15ab34",
      "name": "增强型默认数据加载器2",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        3000,
        2200
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "=file_id",
                "value": "={{ $('Set File ID').item.json.file_id }}"
              },
              {
                "name": "=version",
                "value": "={{ $('Set Version').item.json.message.content.version }}"
              },
              {
                "name": "=creator",
                "value": "={{ $('File Updated').item.json.owners[0].displayName }}"
              },
              {
                "name": "=created_at",
                "value": "={{ $('File Updated').item.json.createdTime }}"
              },
              {
                "name": "=last_modified",
                "value": "={{ $('File Updated').item.json.modifiedTime }}"
              },
              {
                "name": "=folder_path",
                "value": "DOCUMENTS"
              },
              {
                "name": "=file_name",
                "value": "={{ $('File Updated').item.json.name }}"
              },
              {
                "name": "=file_extension",
                "value": "={{ $('File Updated').item.json.mimeType }}"
              }
            ]
          }
        },
        "jsonData": "={{ $json.data || $json.text || $json.concatenated_data }}",
        "jsonMode": "expressionData"
      },
      "typeVersion": 1
    },
    {
      "id": "a82a1191-9271-473a-b4b5-a57217c013b7",
      "name": "遍历项目1",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1380,
        2200
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "423cdd3c-a1a8-406f-abb3-5c6f0d04d669",
      "name": "文件已更新",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        40,
        2200
      ],
      "parameters": {
        "event": "fileUpdated",
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "mode": "list",
          "value": "1M9h9OnDSWa0kV7_Yj7fdSFHhM57pMPX8",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1M9h9OnDSWa0kV7_Yj7fdSFHhM57pMPX8",
          "cachedResultName": "DOCUMENTS"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "yGiuxvKzLUzVYa2d",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2fd0b826-577d-44d7-a3a9-2024bc4ec4cd",
      "name": "插入到 Supabase 向量存储",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "position": [
        2780,
        1960
      ],
      "parameters": {
        "mode": "insert",
        "options": {
          "queryName": "match_documents"
        },
        "tableName": {
          "__rl": true,
          "mode": "id",
          "value": "documents"
        }
      },
      "credentials": {
        "supabaseApi": {
          "id": "J5U7ge3sFHh1WYka",
          "name": "Supabase API"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "49523689-c6f0-444f-a8b3-ea6a1b4eda39",
      "name": "切换",
      "type": "n8n-nodes-base.switch",
      "position": [
        1800,
        2180
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "PDF",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                    "rightValue": "application/pdf"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Text File",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "fc193b06-363b-4699-a97d-e5a850138b0e",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                    "rightValue": "application/vnd.google-apps.document"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Excel",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "2ae7faa7-a936-4621-a680-60c512163034",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                    "rightValue": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Windows Doc(1)",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "7ddfa924-1c26-4ee3-9890-5c9d2b96717a",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                    "rightValue": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Windows Doc(2)",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "f53caf8b-7a8f-4d1e-98f4-deaf0e0171f0",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                    "rightValue": "application/msword"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Windows Doc(3)",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "cd9ca627-d00d-4c30-903a-41f603c8e36f",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Set File ID').item.json.file_type }}",
                    "rightValue": "application/vnd.ms-word"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutput": 2
        }
      },
      "typeVersion": 3
    },
    {
      "id": "c4ea8b5a-695e-438a-8764-96247168dc5b",
      "name": "从文本文件提取1",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        2100,
        2120
      ],
      "parameters": {
        "options": {},
        "operation": "text"
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "8632f57f-e05e-4cd6-a84e-4111c106702c",
      "name": "限制",
      "type": "n8n-nodes-base.limit",
      "position": [
        760,
        2200
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "0e1b5c79-46a7-49d6-a4ad-934a9f2441a8",
      "name": "设置版本",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        980,
        2200
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "GPT-4O-MINI"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "Your goal is to take the incoming version number and add 1 to it. \n\n[Examples]\nInput: v1\nOutput: v2\n\nInput: v2\nOutput: v3\n\nInput: v4\nOutput: v5 \n\nThe output field should always be called \"version\""
            },
            {
              "content": "=incoming version number: {{ $json.metadata.version }}"
            }
          ]
        },
        "jsonOutput": true
      },
      "credentials": {
        "openAiApi": {
          "id": "9SPpIIxBvgYPZlo8",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.7
    },
    {
      "id": "55b08438-df0c-4a13-98d0-bcb632d3d8be",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -120,
        2900
      ],
      "parameters": {
        "width": 560,
        "content": "## 致谢"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "fba7627e-45c8-4121-8f6a-123ca7d6c8d0",
  "connections": {
    "If": {
      "main": [
        [],
        [
          {
            "node": "Delete Old Doc Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Limit": {
      "main": [
        [
          {
            "node": "Set Version",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Extract PDF Text1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from Text File1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from Excel1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Convert to Google Doc2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Convert to Google Doc2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Convert to Google Doc2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize": {
      "main": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set File ID": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Version": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "File Updated": {
      "main": [
        [
          {
            "node": "Set File ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download File": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items1": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Download File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract PDF Text1": {
      "main": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI2": {
      "ai_embedding": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Delete Old Doc Rows": {
      "main": [
        [
          {
            "node": "Limit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from Excel1": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to Google Doc2": {
      "main": [
        [
          {
            "node": "Delete File1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from Text File1": {
      "main": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enhanced Default Data Loader2": {
      "ai_document": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter1": {
      "ai_textSplitter": [
        [
          {
            "node": "Enhanced Default Data Loader2",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

高级 - 文档提取, AI RAG 检索增强

需要付费吗?

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

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

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

外部链接
在 n8n.io 查看

分享此工作流