{
  "name": "My workflow 2529",
  "nodes": [
    {
      "parameters": {
        "model": "gpt-4.1-mini",
        "options": {}
      },
      "id": "c66a158d-b06e-4e58-a589-6562f3397bac",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1,
      "position": [
        1024,
        304
      ]
    },
    {
      "parameters": {
        "jsonMode": "expressionData",
        "jsonData": "={{ $json.data || $json.text || $json.concatenated_data }}",
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "=file_id",
                "value": "={{ $('Set File ID_!').first().json.file_id }}"
              },
              {
                "name": "file_title",
                "value": "={{ $('Set File ID_!').first().json.file_title }}"
              }
            ]
          }
        }
      },
      "id": "e72d49b0-4cfe-4c36-b45a-ac4f9d35c978",
      "name": "Default Data Loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "typeVersion": 1,
      "position": [
        3024,
        1152
      ]
    },
    {
      "parameters": {
        "model": "text-embedding-3-small",
        "options": {}
      },
      "id": "7dc46000-dad3-4a0e-a667-8a719590b4ff",
      "name": "Embeddings OpenAI1",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1,
      "position": [
        2752,
        1088
      ]
    },
    {
      "parameters": {
        "content": "## Agent Tools for RAG",
        "height": 469,
        "width": 583,
        "color": 4
      },
      "id": "685370a0-031d-4e8b-9ef8-27556e6c3ecb",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1792,
        0
      ]
    },
    {
      "parameters": {
        "content": "## Tool to Add a Google Drive File to Vector DB",
        "height": 80,
        "width": 573,
        "color": 5
      },
      "id": "ddbe3a06-e251-4b1e-a069-16fe7ac20c1b",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        32,
        480
      ]
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "value": "={{ $('Set File ID_!').item.json.file_id }}",
          "mode": "id"
        },
        "options": {
          "googleFileConversion": {
            "conversion": {
              "docsToFormat": "text/plain",
              "sheetsToFormat": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            }
          }
        }
      },
      "id": "0a7c7261-d061-4c1b-b8c2-8e789411fef8",
      "name": "Download File",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1152,
        800
      ],
      "executeOnce": true
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "authentication": "serviceAccount",
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "value": "1es_xg6E6Wuluod7Yo-ZBcs38dPQvZuSV",
          "mode": "list",
          "cachedResultName": "05. UD_Doc1",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1es_xg6E6Wuluod7Yo-ZBcs38dPQvZuSV"
        },
        "event": "fileCreated",
        "options": {}
      },
      "id": "ef3e37e5-f2f6-4077-970a-e8ebded73de4",
      "name": "File Created",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "typeVersion": 1,
      "position": [
        -224,
        560
      ]
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "authentication": "serviceAccount",
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "value": "1es_xg6E6Wuluod7Yo-ZBcs38dPQvZuSV",
          "mode": "list",
          "cachedResultName": "05. UD_Doc1",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1es_xg6E6Wuluod7Yo-ZBcs38dPQvZuSV"
        },
        "event": "fileUpdated",
        "options": {}
      },
      "id": "eb94c0b9-83d2-4ff6-a8ca-7a4662d29b10",
      "name": "File Updated",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "typeVersion": 1,
      "position": [
        -208,
        800
      ]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Edit Fields').item.json.sessionId }}"
      },
      "id": "2e62dbb1-76db-47d7-bafb-eec1dd2a0391",
      "name": "Postgres Chat Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryPostgresChat",
      "typeVersion": 1,
      "position": [
        1168,
        304
      ],
      "notesInFlow": false
    },
    {
      "parameters": {
        "operation": "delete",
        "tableId": "documents",
        "filterType": "string",
        "filterString": "=metadata->>file_id=like.*{{ $json.file_id }}*"
      },
      "id": "b2a2fbe9-fba7-42c1-a474-8ffd863cc33f",
      "name": "Delete Old Doc Rows",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        544,
        608
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "content": "## RAG AI Agent with Chat Interface",
        "height": 464.8027193303974,
        "width": 1035.6381264595484
      },
      "id": "5d20a396-1225-4b46-a578-b183fdce3e16",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        720,
        0
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "dadaa298-dae4-4a06-bd87-4bbb4cd08091",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1584,
        80
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "9a9a245e-f1a1-4282-bb02-a81ffe629f0f",
              "name": "chatInput",
              "value": "={{ $json?.chatInput || $json.body.chatInput }}",
              "type": "string"
            },
            {
              "id": "b80831d8-c653-4203-8706-adedfdb98f77",
              "name": "sessionId",
              "value": "={{ $json?.sessionId || $json.body.sessionId}}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "2f935cda-f3a7-47f8-93fb-77eec6aee022",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1024,
        80
      ]
    },
    {
      "parameters": {
        "public": true,
        "options": {}
      },
      "id": "d1a60c04-5969-44bf-ad4d-ce4d5fceaa1e",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        768,
        80
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "5e7bc971-122a-4d85-9bbd-34b5cf75104d",
        "authentication": "headerAuth",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "850b3453-0f00-4316-ba57-eab74ac9bb31",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        768,
        272
      ]
    },
    {
      "parameters": {
        "operation": "pdf",
        "options": {}
      },
      "id": "67ad1dfd-1cb9-48eb-9170-b418d3a986e3",
      "name": "Extract PDF Text",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        2160,
        544
      ]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "id": "c9b2ccbf-ac06-4a31-a645-068247e4ca56",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        2224,
        704
      ]
    },
    {
      "parameters": {
        "chunkOverlap": 100
      },
      "id": "cae84cbc-cfdc-4743-bc16-34d9c433a09a",
      "name": "Character Text Splitter",
      "type": "@n8n/n8n-nodes-langchain.textSplitterCharacterTextSplitter",
      "typeVersion": 1,
      "position": [
        3104,
        1328
      ]
    },
    {
      "parameters": {
        "fieldsToSummarize": {
          "values": [
            {
              "aggregation": "concatenate",
              "field": "data"
            }
          ]
        },
        "options": {}
      },
      "id": "8d885dca-7ce6-4fa6-a4da-5b21bcfb3352",
      "name": "Summarize",
      "type": "n8n-nodes-base.summarize",
      "typeVersion": 1,
      "position": [
        2432,
        784
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.chatInput }}",
        "options": {
          "systemMessage": "=# Prompt cho AI Agent N8n - Tr\u1ee3 l\u00fd T\u00e0i li\u1ec7u Th\u00f4ng minh\n\n## Vai tr\u00f2 v\u00e0 Ch\u1ee9c n\u0103ng\nB\u1ea1n l\u00e0 m\u1ed9t tr\u1ee3 l\u00fd AI chuy\u00ean nghi\u1ec7p, gi\u00fap t\u00ecm ki\u1ebfm v\u00e0 truy xu\u1ea5t th\u00f4ng tin t\u1eeb kho t\u00e0i li\u1ec7u \u0111a \u0111\u1ecbnh d\u1ea1ng. Nhi\u1ec7m v\u1ee5 ch\u00ednh c\u1ee7a b\u1ea1n l\u00e0:\n\n- T\u00ecm ki\u1ebfm th\u00f4ng tin ch\u00ednh x\u00e1c t\u1eeb c\u00e1c t\u00e0i li\u1ec7u v\u0103n b\u1ea3n (TXT, DOC, PDF)\n- Truy xu\u1ea5t d\u1eef li\u1ec7u t\u1eeb c\u00e1c file b\u1ea3ng t\u00ednh (CSV, Excel)\n- X\u1eed l\u00fd v\u00e0 cung c\u1ea5p link truy c\u1eadp video khi \u0111\u01b0\u1ee3c y\u00eau c\u1ea7u\n- \u0110\u1ea3m b\u1ea3o t\u00ednh ch\u00ednh x\u00e1c v\u00e0 \u0111\u1ea7y \u0111\u1ee7 c\u1ee7a th\u00f4ng tin \u0111\u01b0\u1ee3c cung c\u1ea5p\n\n## Quy tr\u00ecnh X\u1eed l\u00fd Y\u00eau c\u1ea7u\n\n### B\u01b0\u1edbc 1: Ph\u00e2n t\u00edch Y\u00eau c\u1ea7u v\u00e0 Ki\u1ec3m tra Ti\u00eau \u0111\u1ec1\n- \u0110\u1ecdc k\u1ef9 c\u00e2u h\u1ecfi c\u1ee7a ng\u01b0\u1eddi d\u00f9ng\n- **LU\u00d4N KI\u1ec2M TRA TI\u00caU \u0110\u1ec0 T\u00c0I LI\u1ec6U TR\u01af\u1edaC**: Qu\u00e9t qua t\u1ea5t c\u1ea3 ti\u00eau \u0111\u1ec1 \u0111\u1ec3 t\u00ecm t\u00e0i li\u1ec7u li\u00ean quan\n- \u01afu ti\u00ean t\u00ecm file .mp4 c\u00f3 ti\u00eau \u0111\u1ec1 kh\u1edbp v\u1edbi y\u00eau c\u1ea7u (v\u00ed d\u1ee5: \"h\u01b0\u1edbng d\u1eabn l\u1eb7p qu\u1ea1t 3 c\u00e1nh\" \u2192 t\u00ecm ti\u00eau \u0111\u1ec1 ch\u1ee9a \"qu\u1ea1t 3 c\u00e1nh\", \"l\u1eb7p qu\u1ea1t\", \"h\u01b0\u1edbng d\u1eabn l\u1eafp qu\u1ea1t\")\n- N\u1ebfu c\u00e2u h\u1ecfi kh\u00f4ng r\u00f5 r\u00e0ng, y\u00eau c\u1ea7u l\u00e0m r\u00f5 th\u00eam\n\n### B\u01b0\u1edbc 2: Truy v\u1ea5n Supabase Vector Store\n- **B\u1eaeT BU\u1ed8C**: Lu\u00f4n th\u1ef1c hi\u1ec7n t\u00ecm ki\u1ebfm trong Supabase Vector Store\n- S\u1eed d\u1ee5ng t\u1eeb kh\u00f3a li\u00ean quan \u0111\u1ec3 t\u00ecm ki\u1ebfm to\u00e0n di\u1ec7n\n- Ki\u1ec3m tra t\u1ea5t c\u1ea3 k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1, bao g\u1ed3m c\u1ea3 metadata c\u1ee7a video\n\n### B\u01b0\u1edbc 3: Ki\u1ec3m tra v\u00e0 X\u00e1c th\u1ef1c D\u1eef li\u1ec7u\n- Ki\u1ec3m tra k\u1ef9 ti\u00eau \u0111\u1ec1 v\u00e0 n\u1ed9i dung c\u1ee7a t\u00e0i li\u1ec7u t\u00ecm \u0111\u01b0\u1ee3c\n- \u0110\u1ed1i chi\u1ebfu th\u00f4ng tin v\u1edbi y\u00eau c\u1ea7u c\u1ee7a ng\u01b0\u1eddi d\u00f9ng\n- X\u00e1c nh\u1eadn t\u00ednh ch\u00ednh x\u00e1c tr\u01b0\u1edbc khi ph\u1ea3n h\u1ed3i\n\n### B\u01b0\u1edbc 4: X\u1eed l\u00fd \u0110\u1eb7c bi\u1ec7t cho Video v\u00e0 H\u01b0\u1edbng d\u1eabn\n- **QUY T\u1eaeC QUAN TR\u1eccNG**: Ch\u1ec9 d\u1ef1a v\u00e0o TI\u00caU \u0110\u1ec0 file .mp4 \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh n\u1ed9i dung video\n- **KH\u00d4NG XEM** \u0111\u01b0\u1ee3c n\u1ed9i dung b\u00ean trong video, ch\u1ec9 ph\u00e2n t\u00edch ti\u00eau \u0111\u1ec1\n- **TH\u1ee8 T\u1ef0 \u01afU TI\u00caN**:\n  1. T\u00ecm file .mp4 c\u00f3 ti\u00eau \u0111\u1ec1 kh\u1edbp v\u1edbi y\u00eau c\u1ea7u \u2192 Tr\u1ea3 link ngay l\u1eadp t\u1ee9c\n  2. Sau \u0111\u00f3 m\u1edbi t\u00ecm n\u1ed9i dung trong t\u00e0i li\u1ec7u v\u0103n b\u1ea3n kh\u00e1c\n- **V\u00cd D\u1ee4**: H\u1ecfi \"h\u01b0\u1edbng d\u1eabn l\u1eb7p qu\u1ea1t 3 c\u00e1nh\" \u2192 T\u00ecm ti\u00eau \u0111\u1ec1 .mp4 ch\u1ee9a \"qu\u1ea1t 3 c\u00e1nh\" ho\u1eb7c \"l\u1eb7p qu\u1ea1t\"\n- Ki\u1ec3m tra trong file Excel/CSV c\u00f3 ch\u1ee9a URL video v\u1edbi ti\u00eau \u0111\u1ec1 ph\u00f9 h\u1ee3p\n\n## Nguy\u00ean t\u1eafc Ho\u1ea1t \u0111\u1ed9ng\n\n### \u2705 PH\u1ea2I L\u00c0M\n- **LU\u00d4N KI\u1ec2M TRA TI\u00caU \u0110\u1ec0 TR\u01af\u1edaC**: Qu\u00e9t to\u00e0n b\u1ed9 danh s\u00e1ch ti\u00eau \u0111\u1ec1 t\u00e0i li\u1ec7u tr\u01b0\u1edbc khi t\u00ecm ki\u1ebfm\n- **\u01afU TI\u00caN VIDEO .MP4**: T\u00ecm file .mp4 c\u00f3 ti\u00eau \u0111\u1ec1 li\u00ean quan \u0111\u1ea7u ti\u00ean\n- **CH\u1ec8 D\u1ef0A V\u00c0O TI\u00caU \u0110\u1ec0**: Kh\u00f4ng suy \u0111o\u00e1n n\u1ed9i dung video, ch\u1ec9 ph\u00e2n t\u00edch ti\u00eau \u0111\u1ec1 file\n- **TH\u1ee8 T\u1ef0**: Video .mp4 tr\u01b0\u1edbc \u2192 Link ngay \u2192 Sau \u0111\u00f3 m\u1edbi t\u00ecm t\u00e0i li\u1ec7u kh\u00e1c\n- Cung c\u1ea5p th\u00f4ng tin ch\u00ednh x\u00e1c 100% t\u1eeb ngu\u1ed3n d\u1eef li\u1ec7u\n- Tr\u1ea3 v\u1ec1 link video clickable \u0111\u1ec3 ng\u01b0\u1eddi d\u00f9ng xem tr\u1ef1c ti\u1ebfp\n\n### \u274c KH\u00d4NG \u0110\u01af\u1ee2C L\u00c0M\n- B\u1ecba \u0111\u1eb7t ho\u1eb7c suy di\u1ec5n n\u1ed9i dung video t\u1eeb ti\u00eau \u0111\u1ec1\n- **KH\u00d4NG \u0110O\u00c1N** n\u1ed9i dung b\u00ean trong file .mp4\n- B\u1ecf qua vi\u1ec7c ki\u1ec3m tra ti\u00eau \u0111\u1ec1 tr\u01b0\u1edbc khi t\u00ecm ki\u1ebfm\n- T\u00ecm t\u00e0i li\u1ec7u kh\u00e1c tr\u01b0\u1edbc khi ki\u1ec3m tra video .mp4\n- Tr\u1ea3 l\u1eddi m\u00e0 kh\u00f4ng ki\u1ec3m tra danh s\u00e1ch ti\u00eau \u0111\u1ec1 \u0111\u1ea7y \u0111\u1ee7\n- \u0110\u01b0a ra th\u00f4ng tin kh\u00f4ng ch\u00ednh x\u00e1c v\u1ec1 video\n\n## C\u00e1ch Ph\u1ea3n h\u1ed3i\n\n### Khi t\u00ecm th\u1ea5y video .mp4 ph\u00f9 h\u1ee3p:\n```\n\ud83c\udfa5 **T\u00ccM TH\u1ea4Y VIDEO H\u01af\u1edaNG D\u1eaaN**: \n\n\ud83d\udc49 **[Ti\u00eau \u0111\u1ec1 video]** - [Link video tr\u1ef1c ti\u1ebfp - Click \u0111\u1ec3 xem]\n\n\ud83d\udcdd **Ghi ch\u00fa**: Video \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh d\u1ef1a tr\u00ean ti\u00eau \u0111\u1ec1 \"[ti\u00eau \u0111\u1ec1 file]\"\n\n---\n\ud83d\udcda **Th\u00f4ng tin b\u1ed5 sung t\u1eeb t\u00e0i li\u1ec7u kh\u00e1c** (n\u1ebfu c\u00f3):\n[N\u1ed9i dung t\u1eeb t\u00e0i li\u1ec7u v\u0103n b\u1ea3n li\u00ean quan]\n```\n\n### Khi t\u00ecm th\u1ea5y th\u00f4ng tin kh\u00e1c:\n```\nD\u1ef1a tr\u00ean t\u00e0i li\u1ec7u [t\u00ean t\u00e0i li\u1ec7u], t\u00f4i t\u00ecm th\u1ea5y th\u00f4ng tin sau:\n\n[N\u1ed9i dung ch\u00ednh x\u00e1c t\u1eeb t\u00e0i li\u1ec7u]\n\n[N\u1ebfu c\u00f3 video]: \ud83c\udfa5 Link video: [URL]\nNgu\u1ed3n: [T\u00ean file/t\u00e0i li\u1ec7u]\n```\n\n### Khi kh\u00f4ng t\u00ecm th\u1ea5y th\u00f4ng tin:\n```\nT\u00f4i \u0111\u00e3 t\u00ecm ki\u1ebfm trong to\u00e0n b\u1ed9 kho t\u00e0i li\u1ec7u nh\u01b0ng kh\u00f4ng t\u00ecm th\u1ea5y th\u00f4ng tin v\u1ec1 [ch\u1ee7 \u0111\u1ec1]. \n\nB\u1ea1n c\u00f3 th\u1ec3 cung c\u1ea5p th\u00eam chi ti\u1ebft ho\u1eb7c t\u1eeb kh\u00f3a kh\u00e1c \u0111\u1ec3 t\u00f4i h\u1ed7 tr\u1ee3 t\u1ed1t h\u01a1n kh\u00f4ng?\n```\n\n### Khi c\u1ea7n l\u00e0m r\u00f5:\n```\n\u0110\u1ec3 t\u00f4i c\u00f3 th\u1ec3 h\u1ed7 tr\u1ee3 ch\u00ednh x\u00e1c, b\u1ea1n c\u00f3 th\u1ec3 l\u00e0m r\u00f5 th\u00eam v\u1ec1:\n- [C\u00e2u h\u1ecfi c\u1ee5 th\u1ec3 1]\n- [C\u00e2u h\u1ecfi c\u1ee5 th\u1ec3 2]\n```\n\n## L\u01b0u \u00fd \u0110\u1eb7c bi\u1ec7t\n\n1. **KI\u1ec2M TRA TI\u00caU \u0110\u1ec0 \u0110\u1ea6U TI\u00caN**: Lu\u00f4n qu\u00e9t danh s\u00e1ch ti\u00eau \u0111\u1ec1 t\u00e0i li\u1ec7u tr\u01b0\u1edbc m\u1ecdi thao t\u00e1c kh\u00e1c\n2. **GI\u1edaI H\u1ea0N VIDEO**: Ch\u1ec9 bi\u1ebft n\u1ed9i dung video qua ti\u00eau \u0111\u1ec1 file .mp4, kh\u00f4ng xem \u0111\u01b0\u1ee3c b\u00ean trong\n3. **TH\u1ee8 T\u1ef0 X\u1eec L\u00dd**: Ti\u00eau \u0111\u1ec1 .mp4 \u2192 Link video \u2192 T\u00e0i li\u1ec7u v\u0103n b\u1ea3n \u2192 T\u1ed5ng h\u1ee3p\n4. **V\u00cd D\u1ee4 TH\u1ef0C T\u1ebe**: \n   - H\u1ecfi: \"h\u01b0\u1edbng d\u1eabn l\u1eb7p qu\u1ea1t 3 c\u00e1nh\"\n   - T\u00ecm: Ti\u00eau \u0111\u1ec1 ch\u1ee9a \"qu\u1ea1t 3 c\u00e1nh\", \"l\u1eb7p qu\u1ea1t\", \"h\u01b0\u1edbng d\u1eabn qu\u1ea1t\"\n   - Tr\u1ea3 v\u1ec1: Link video ngay l\u1eadp t\u1ee9c\n5. **T\u1eeb kh\u00f3a Matching**: So kh\u1edbp t\u1eeb kh\u00f3a trong c\u00e2u h\u1ecfi v\u1edbi ti\u00eau \u0111\u1ec1 file\n6. **Link Click-able**: \u0110\u1ea3m b\u1ea3o link video c\u00f3 th\u1ec3 click tr\u1ef1c ti\u1ebfp\n7. **Ki\u1ec3m tra Metadata**: Xem x\u00e9t metadata c\u1ee7a file Excel/CSV ch\u1ee9a URL video\n\n## C\u00f4ng c\u1ee5 C\u1ea7n s\u1eed d\u1ee5ng\n- Supabase Vector Store Query Tool\n- Document Analysis Tool  \n- Excel/CSV Reader Tool\n- Video Link Extractor Tool\n\n---\n*Ghi nh\u1edb: T\u00ednh ch\u00ednh x\u00e1c v\u00e0 \u0111\u1ea7y \u0111\u1ee7 l\u00e0 \u01b0u ti\u00ean h\u00e0ng \u0111\u1ea7u. Lu\u00f4n t\u00ecm ki\u1ebfm tr\u01b0\u1edbc khi tr\u1ea3 l\u1eddi.*"
        }
      },
      "id": "9b4c43c3-9689-4adc-9546-1b660e4b3efe",
      "name": "RAG AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.6,
      "position": [
        1248,
        80
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "application/pdf",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "2d5d8bc1-7088-4b65-a833-301c3987bfbe"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Pdf"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "2ae7faa7-a936-4621-a680-60c512163034",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Excel 1"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "b3b66a9e-8aca-4f0d-9864-f2e03c7a27cd",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Excel 3"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "fc193b06-363b-4699-a97d-e5a850138b0e",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "=application/vnd.google-apps.spreadsheet",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Excel 2"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "bb391e81-4667-4854-8033-e2771d1f69fb",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "application/vnd.ms-excel",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Excel 4"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "f5de71c8-289a-477b-ae62-f336e7286a0c",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "text/csv",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Csv"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "b69f5605-0179-4b02-9a32-e34bb085f82d",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "application/vnd.google-apps.document",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Txt"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "a605d52b-093e-47ce-bed4-6c040b465b79",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "application/msword",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Word 1"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "043262f3-55dc-40b0-af44-f2f67aae3aa9",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Word 2"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "291a9806-5b16-46b4-a253-4806491d92bc",
                    "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
                    "rightValue": "application/vnd.ms-word",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Word 3"
            },
            {
              "renameOutput": true,
              "outputKey": "None"
            }
          ]
        },
        "options": {
          "fallbackOutput": 10
        }
      },
      "id": "ec548f41-3751-4827-be07-54045626607a",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        1328,
        720
      ]
    },
    {
      "parameters": {
        "mode": "insert",
        "tableName": {
          "__rl": true,
          "value": "documents",
          "mode": "list",
          "cachedResultName": "documents"
        },
        "options": {
          "queryName": "match_documents"
        }
      },
      "id": "0be41eff-036e-4f79-9bdb-e333337b4f57",
      "name": "Insert into Supabase Vectorstore",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1,
      "position": [
        2912,
        864
      ]
    },
    {
      "parameters": {
        "operation": "xlsx",
        "options": {}
      },
      "id": "6c8efc0b-cb17-468c-be0d-216b6b90a097",
      "name": "Extract from Excel",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        1792,
        704
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f422e2e0-381c-46ea-8f38-3f58c501d8b9",
              "name": "schema",
              "value": "={{ $('Extract from Excel').isExecuted ? $('Extract from Excel').first().json.keys().toJsonString() : $('Extract from CSV').first().json.keys().toJsonString() }}",
              "type": "string"
            },
            {
              "id": "bb07c71e-5b60-4795-864c-cc3845b6bc46",
              "name": "data",
              "value": "={{ $json.concatenated_data }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2864,
        640
      ],
      "id": "46cb8d28-2514-4ec6-9814-8e18f76e59ae",
      "name": "Set Schema"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        1792,
        880
      ],
      "id": "98fad061-e237-48f4-8d9e-19483af43b68",
      "name": "Extract from CSV"
    },
    {
      "parameters": {
        "content": "## Run Each Node Once to Set Up Database Tables",
        "height": 300,
        "width": 680,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "typeVersion": 1,
      "id": "dcd59518-563b-4c69-bb11-f2412af92c5d",
      "name": "Sticky Note3"
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Use this tool to fetch all available documents, including the table schema if the file is a CSV or Excel file.",
        "operation": "select",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "document_metadata",
          "mode": "list",
          "cachedResultName": "document_metadata"
        },
        "returnAll": true,
        "options": {}
      },
      "type": "n8n-nodes-base.postgresTool",
      "typeVersion": 2.5,
      "position": [
        1312,
        304
      ],
      "id": "798936ad-f8a6-4260-b520-5b7af6abd5ce",
      "name": "List Documents"
    },
    {
      "parameters": {
        "descriptionType": "manual",
        "toolDescription": "Given a file ID, fetches the text from the document.",
        "operation": "executeQuery",
        "query": "SELECT \n    string_agg(content, ' ') as document_text\nFROM documents\n  WHERE metadata->>'file_id' = $1\nGROUP BY metadata->>'file_id';",
        "options": {
          "queryReplacement": "={{ $fromAI('file_id') }}"
        }
      },
      "type": "n8n-nodes-base.postgresTool",
      "typeVersion": 2.5,
      "position": [
        1440,
        304
      ],
      "id": "168975ad-022b-4bc4-acc1-e410f987cbd2",
      "name": "Get File Contents"
    },
    {
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolName": "documents",
        "toolDescription": "Use RAG to look up information in the knowledgebase.",
        "tableName": {
          "__rl": true,
          "value": "documents",
          "mode": "list",
          "cachedResultName": "documents"
        },
        "options": {
          "queryName": "match_documents"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1,
      "position": [
        1888,
        224
      ],
      "id": "939b8941-7824-476e-8b6b-8bcfdbfe281f",
      "name": "Supabase Vector Store1"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        1984,
        352
      ],
      "id": "8dcfc2ca-8bec-470b-9a0c-7c9f37e5833e",
      "name": "Embeddings OpenAI2"
    },
    {
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        32,
        592
      ],
      "id": "1c59d742-b841-4059-8230-743562f99fcf",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "-- K\u00edch ho\u1ea1t extension pgvector\nCREATE EXTENSION IF NOT EXISTS vector;\n\n-- T\u1ea1o b\u1ea3ng l\u01b0u metadata c\u1ee7a t\u00e0i li\u1ec7u\nCREATE TABLE document_metadata (\n    id TEXT PRIMARY KEY,\n    title TEXT,\n    url TEXT,\n    ggdrive_di TEXT,\n    created_at TIMESTAMP DEFAULT NOW(),\n    schema TEXT\n);\n\n-- T\u1ea1o b\u1ea3ng l\u01b0u c\u00e1c d\u00f2ng d\u1eef li\u1ec7u c\u1ee7a t\u00e0i li\u1ec7u\nCREATE TABLE document_rows (\n    id SERIAL PRIMARY KEY,\n    dataset_id TEXT REFERENCES document_metadata(id),\n    row_data JSONB  -- L\u01b0u tr\u1eef d\u1eef li\u1ec7u d\u00f2ng th\u1ef1c t\u1ebf\n);\n\n-- T\u1ea1o b\u1ea3ng l\u01b0u tr\u1eef t\u00e0i li\u1ec7u v\u00e0 embedding c\u1ee7a ch\u00fang\nCREATE TABLE documents (\n  id BIGSERIAL PRIMARY KEY,\n  content TEXT,  -- N\u1ed9i dung t\u00e0i li\u1ec7u\n  metadata JSONB,  -- Metadata c\u1ee7a t\u00e0i li\u1ec7u\n  embedding VECTOR(1536)  -- 1536 l\u00e0 k\u00edch th\u01b0\u1edbc c\u1ee7a embedding, c\u00f3 th\u1ec3 thay \u0111\u1ed5i t\u00f9y theo nhu c\u1ea7u\n);\n\n-- T\u1ea1o h\u00e0m \u0111\u1ec3 t\u00ecm ki\u1ebfm t\u00e0i li\u1ec7u d\u1ef1a tr\u00ean embedding\nCREATE FUNCTION match_documents (\n  query_embedding VECTOR(1536),\n  match_count INT DEFAULT NULL,\n  filter JSONB DEFAULT '{}'\n) RETURNS TABLE (\n  doc_id BIGINT,\n  content TEXT,\n  metadata JSONB,\n  similarity FLOAT\n)\nLANGUAGE plpgsql\nAS $$\nBEGIN\n  RETURN QUERY\n  SELECT\n    documents.id AS doc_id,  -- \u0110\u1ed5i t\u00ean c\u1ed9t 'id' c\u1ee7a b\u1ea3ng 'documents' th\u00e0nh 'doc_id'\n    documents.content,\n    documents.metadata,\n    1 - (documents.embedding <=> query_embedding) AS similarity\n  FROM documents\n  JOIN document_metadata ON documents.id = document_metadata.id  -- Li\u00ean k\u1ebft v\u1edbi b\u1ea3ng document_metadata\n  LEFT JOIN document_rows ON document_metadata.id = document_rows.dataset_id  -- Li\u00ean k\u1ebft v\u1edbi b\u1ea3ng document_rows\n  WHERE documents.metadata @> filter\n  ORDER BY documents.embedding <=> query_embedding\n  LIMIT match_count;\nEND;\n$$;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        288,
        112
      ],
      "id": "dbf51527-9807-4868-ba1d-e80b6aab9eed",
      "name": "Create Documents Table and Match Function"
    },
    {
      "parameters": {
        "operation": "delete",
        "tableId": "document_rows",
        "filters": {
          "conditions": [
            {
              "keyName": "dataset_id",
              "condition": "eq",
              "keyValue": "={{ $('Set File ID_!').item.json.file_id }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        752,
        608
      ],
      "id": "47ddda3b-6b88-40cd-969d-4c6709f8442b",
      "name": "Delete Old Data Rows",
      "alwaysOutputData": true,
      "executeOnce": true
    },
    {
      "parameters": {
        "operation": "upsert",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "document_metadata",
          "mode": "list",
          "cachedResultName": "document_metadata"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "id": "={{ $('Set File ID_!').item.json.file_id }}",
            "title": "={{ $('Set File ID_!').item.json.file_title }}",
            "url": "={{ $('Set File ID_!').item.json.file_url }}",
            "schema": "={{ $('Set File ID_!').item.json.id }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": true,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "title",
              "displayName": "title",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false
            },
            {
              "id": "url",
              "displayName": "url",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": false
            },
            {
              "id": "created_at",
              "displayName": "created_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "schema",
              "displayName": "schema",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        912,
        608
      ],
      "id": "7bc0d010-827d-4dc9-8d11-26da6491bb11",
      "name": "Insert Document Metadata",
      "executeOnce": true
    },
    {
      "parameters": {
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "document_rows",
          "mode": "list",
          "cachedResultName": "document_rows"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "dataset_id": "={{ $('Set File ID_!').item.json.file_id }}",
            "row_data": "={{ $json.toJsonString().replaceAll(/'/g, \"''\") }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": false,
              "defaultMatch": true,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "dataset_id",
              "displayName": "dataset_id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "row_data",
              "displayName": "row_data",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "object",
              "canBeUsedToMatch": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        2224,
        880
      ],
      "id": "90cdd0fd-e7ea-4a99-8347-250850f6281e",
      "name": "Insert Table Rows"
    },
    {
      "parameters": {
        "operation": "upsert",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "document_metadata",
          "mode": "list",
          "cachedResultName": "document_metadata"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "id": "={{ $('Set File ID_!').item.json.file_id }}",
            "schema": "={{ $json.schema }}"
          },
          "matchingColumns": [
            "id"
          ],
          "schema": [
            {
              "id": "id",
              "displayName": "id",
              "required": true,
              "defaultMatch": true,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "title",
              "displayName": "title",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": true
            },
            {
              "id": "url",
              "displayName": "url",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": true
            },
            {
              "id": "created_at",
              "displayName": "created_at",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "dateTime",
              "canBeUsedToMatch": false
            },
            {
              "id": "schema",
              "displayName": "schema",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        3088,
        640
      ],
      "id": "177e6659-239e-466b-88df-97217b680796",
      "name": "Update Schema for Document Metadata"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id,url  FROM public.document_metadata;",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        320,
        1664
      ],
      "id": "82aae5cd-95b5-467c-b97e-8a9ce76d3f1d",
      "name": "Node_postgres",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "7134e92c-2e4f-49b3-a0d1-dbe856ebef2c",
              "leftValue": "={{ $('Code1').item}}",
              "rightValue": "",
              "operator": {
                "type": "object",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1840,
        1664
      ],
      "id": "e7a067a4-5a8e-4a50-bf1d-a1f2e823f196",
      "name": "If"
    },
    {
      "parameters": {
        "operation": "deleteFile",
        "fileId": {
          "__rl": true,
          "value": "={{ $('Set File ID_!').item.json.file_id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        2000,
        1312
      ],
      "id": "35bc8cf7-8410-430c-b849-173cdb37cd6f",
      "name": "Delete File"
    },
    {
      "parameters": {
        "operation": "text",
        "options": {}
      },
      "id": "a070c276-3194-4b5b-ab26-c224b8da1843",
      "name": "Extract Document Text1",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        1808,
        1072
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "operation": "delete",
        "tableId": "document_metadata",
        "filters": {
          "conditions": [
            {
              "keyName": "id",
              "condition": "eq",
              "keyValue": "={{ $('Set File ID_!').item.json.file_id }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        2208,
        1312
      ],
      "id": "5ebd7727-c880-4587-9992-547c38265c15",
      "name": "Delete Old Data Rows3",
      "alwaysOutputData": true,
      "executeOnce": true
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://www.googleapis.com/drive/v3/files/{{ $('Set File ID_!').item.json.file_id }}/copy",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleDriveOAuth2Api",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "name",
              "value": "={{ $json.title }}"
            },
            {
              "name": "mimeType",
              "value": "application/vnd.google-apps.document"
            }
          ]
        },
        "options": {}
      },
      "id": "d2cd5eb6-1423-4844-93ff-42d97b504a68",
      "name": "Convert to Google Doc",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1824,
        1312
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "21651ed3-cfa6-4ef8-940b-fb734b9e4fc5",
              "leftValue": "={{ $('Set File ID_!').item.json.file_type }}",
              "rightValue": "video/mp4",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        944,
        832
      ],
      "id": "63996c9d-7465-4754-983a-72c314035edd",
      "name": "If1"
    },
    {
      "parameters": {
        "fieldsToAggregate": {
          "fieldToAggregate": [
            {
              "fieldToAggregate": "id"
            },
            {
              "fieldToAggregate": "url"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        512,
        1664
      ],
      "id": "037c25af-80d2-4bb0-81eb-8e11e6fe0091",
      "name": "AggregateDB"
    },
    {
      "parameters": {
        "jsCode": "// L\u1ea5y hai m\u1ea3ng t\u1eeb input trong n8n\nconst array1 = $('AggregateDB').first().json.id || [];\nconsole.log('array1: ',array1);\nconst array2 = $('AggregateGGDRive').first().json.id.map(item => item.id) || [];\nconsole.log('array2: ',array2)\n\nconst uniqueIDs = [\n  ...array1.filter(id => !array2.includes(id)), // ID ch\u1ec9 c\u00f3 trong array1\n  ...array2.filter(id => !array1.includes(id))  // ID ch\u1ec9 c\u00f3 trong array2\n];\nconsole.log('arrFilter1: ',array1.filter(id => !array2.includes(id)))\nconsole.log('arrFilter2: ',array2.filter(id => !array1.includes(id)))\nconsole.log('uniqueIDS: ',uniqueIDs)\n// Chuy\u1ec3n \u0111\u1ed5i k\u1ebft qu\u1ea3 th\u00e0nh \u0111\u1ecbnh d\u1ea1ng mong mu\u1ed1n\nconst result = uniqueIDs.map(id => ({ id }));\n\n// Tr\u1ea3 v\u1ec1 k\u1ebft qu\u1ea3\nreturn result;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1168,
        1664
      ],
      "id": "e38d5810-b20e-43dd-89f3-6f0c00f0d890",
      "name": "Code1",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        32,
        1664
      ],
      "id": "f95ecb36-d678-4b4c-be06-a28c2dfb611c",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "batchSize": "=1",
        "options": {
          "reset": false
        }
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        2128,
        1648
      ],
      "id": "4215ef18-093d-462a-89a5-fa559cc4da13",
      "name": "Loop Over Items2"
    },
    {
      "parameters": {
        "operation": "delete",
        "tableId": "document_rows",
        "filters": {
          "conditions": [
            {
              "keyName": "dataset_id",
              "condition": "eq",
              "keyValue": "={{ $json.id }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        2384,
        1680
      ],
      "id": "d1dc50fd-362d-4e4b-bdbc-408b461849db",
      "name": "Delete Document_Rows",
      "alwaysOutputData": true,
      "executeOnce": true
    },
    {
      "parameters": {
        "operation": "delete",
        "tableId": "document_metadata",
        "filters": {
          "conditions": [
            {
              "keyName": "id",
              "condition": "eq",
              "keyValue": "={{ $('Loop Over Items2').item.json.id }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        2560,
        1680
      ],
      "id": "ce3eff35-9fdc-4982-a77b-95400d5b8cad",
      "name": "Delete Document_Metadata",
      "alwaysOutputData": true,
      "executeOnce": true
    },
    {
      "parameters": {
        "operation": "delete",
        "tableId": "documents",
        "filterType": "string",
        "filterString": "=metadata->>file_id=like.*{{ $('Loop Over Items2').item.json.id }}*"
      },
      "id": "3a0139c0-b2ce-41e9-821a-7824893ccc36",
      "name": "Delete Documents",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        3232,
        1680
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "69a951b3-58cf-4caf-9f9d-70f1bdcb2e78",
              "leftValue": "={{ $json.id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2848,
        1680
      ],
      "id": "61c6d1e9-a251-438c-8a16-5e6c387054d9",
      "name": "If3"
    },
    {
      "parameters": {
        "authentication": "serviceAccount",
        "resource": "fileFolder",
        "returnAll": true,
        "filter": {
          "folderId": {
            "__rl": true,
            "value": "1es_xg6E6Wuluod7Yo-ZBcs38dPQvZuSV",
            "mode": "list",
            "cachedResultName": "05. UD_Doc1",
            "cachedResultUrl": "https://drive.google.com/drive/folders/1es_xg6E6Wuluod7Yo-ZBcs38dPQvZuSV"
          },
          "whatToSearch": "files",
          "includeTrashed": false
        },
        "options": {
          "fields": [
            "id"
          ]
        }
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        720,
        1664
      ],
      "id": "d610b885-de83-4f61-af63-024a3021ab6f",
      "name": "Node_drive",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "destinationFieldName": "id",
        "options": {}
      },
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        944,
        1664
      ],
      "id": "7e85e796-153e-4d19-8228-a1517f836e80",
      "name": "AggregateGGDRive"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "10646eae-ae46-4327-a4dc-9987c2d76173",
              "name": "file_id",
              "value": "={{ $json.id }}",
              "type": "string"
            },
            {
              "id": "f4536df5-d0b1-4392-bf17-b8137fb31a44",
              "name": "file_type",
              "value": "={{ $json.mimeType }}",
              "type": "string"
            },
            {
              "id": "77d782de-169d-4a46-8a8e-a3831c04d90f",
              "name": "file_title",
              "value": "={{ $json.name }}",
              "type": "string"
            },
            {
              "id": "9bde4d7f-e4f3-4ebd-9338-dce1350f9eab",
              "name": "file_url",
              "value": "={{ $json.webViewLink }}",
              "type": "string"
            },
            {
              "id": "b8735aa6-4e47-4197-9f36-6559fa4d4420",
              "name": "id",
              "value": "={{ $json.id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "6cd2e61b-79b9-4b3c-a9c3-b94f3075b3b5",
      "name": "Set File ID_!",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        288,
        608
      ]
    }
  ],
  "connections": {
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Download File": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "File Created": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI1": {
      "ai_embedding": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Postgres Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Delete Old Doc Rows": {
      "main": [
        [
          {
            "node": "Delete Old Data Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "File Updated": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "RAG AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract PDF Text": {
      "main": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Character Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Default Data Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Summarize": {
      "main": [
        [
          {
            "node": "Set Schema",
            "type": "main",
            "index": 0
          },
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "RAG AI Agent": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Extract PDF Text",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from Excel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from Excel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from Excel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from Excel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from CSV",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract Document Text1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Convert to Google Doc",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Convert to Google Doc",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Convert to Google Doc",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from Excel": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          },
          {
            "node": "Insert Table Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Schema": {
      "main": [
        [
          {
            "node": "Update Schema for Document Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from CSV": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          },
          {
            "node": "Insert Table Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List Documents": {
      "ai_tool": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Get File Contents": {
      "ai_tool": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Supabase Vector Store1": {
      "ai_tool": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI2": {
      "ai_embedding": [
        [
          {
            "node": "Supabase Vector Store1",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Set File ID_!",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert into Supabase Vectorstore": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Old Data Rows": {
      "main": [
        [
          {
            "node": "Insert Document Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Document Metadata": {
      "main": [
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Node_postgres": {
      "main": [
        [
          {
            "node": "AggregateDB",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Loop Over Items2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete File": {
      "main": [
        [
          {
            "node": "Delete Old Data Rows3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Document Text1": {
      "main": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Old Data Rows3": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to Google Doc": {
      "main": [
        [
          {
            "node": "Delete File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If1": {
      "main": [
        [
          {
            "node": "Download File",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AggregateDB": {
      "main": [
        [
          {
            "node": "Node_drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Node_postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items2": {
      "main": [
        [],
        [
          {
            "node": "Delete Document_Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Document_Rows": {
      "main": [
        [
          {
            "node": "Delete Document_Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Document_Metadata": {
      "main": [
        [
          {
            "node": "If3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Documents": {
      "main": [
        [
          {
            "node": "Loop Over Items2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If3": {
      "main": [
        [
          {
            "node": "Delete Documents",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Node_drive": {
      "main": [
        [
          {
            "node": "AggregateGGDRive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AggregateGGDRive": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set File ID_!": {
      "main": [
        [
          {
            "node": "Delete Old Doc Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "a61f96b6-456f-4a3c-afa8-78d8f131a553",
  "id": "fARZdcOZBRis7Nbw",
  "tags": []
}