{
  "name": "RAG Google Drive Supabase",
  "nodes": [
    {
      "parameters": {
        "folderId": "root",
        "event": "fileUpdated"
      },
      "name": "Google Drive Trigger",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ],
      "credentials": {
        "googleDriveOAuth2Api": "<your credential>"
      }
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": "={{$json[\"id\"]}}",
        "binaryPropertyName": "data"
      },
      "name": "Download File",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 1,
      "position": [
        500,
        300
      ],
      "credentials": {
        "googleDriveOAuth2Api": "<your credential>"
      }
    },
    {
      "parameters": {
        "functionCode": "const fileData = $binary.data.data;\nconst text = Buffer.from(fileData,'base64').toString('utf-8');\nreturn [{json:{fileName:$json[\"name\"],content:text}}];"
      },
      "name": "Extract Content",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "fieldToSplit": "content",
        "chunkSize": 500,
        "overlap": 50
      },
      "name": "Split In Chunks",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "resource": "embeddings",
        "operation": "create",
        "model": "text-embedding-3-small",
        "input": "={{$json[\"chunk\"]}}"
      },
      "name": "Create Embeddings",
      "type": "n8n-nodes-base.openAI",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ],
      "credentials": {
        "openAIApi": "<your credential>"
      }
    },
    {
      "parameters": {
        "functionCode": "return $input.all().map(item=>({json:{id:`${item.json.fileName}-${Math.random().toString(36).substr(2,7)}`,fileName:item.json.fileName,embedding:item.json.embedding,metadata:{fileName:item.json.fileName}}}));"
      },
      "name": "Prepare Vectors",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1500,
        300
      ]
    },
    {
      "parameters": {
        "operation": "delete",
        "table": "vectors",
        "rowFilter": "={{`fileName=\\'${$json[\"fileName\"]}\\'`}}"
      },
      "name": "Delete Old Vectors",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        1750,
        300
      ],
      "credentials": {
        "supabaseApi": "<your credential>"
      }
    },
    {
      "parameters": {
        "operation": "insert",
        "table": "vectors",
        "columns": "id,fileName,embedding,metadata",
        "values": "={{[{ json: $json }][0]}}"
      },
      "name": "Insert Updated Vectors",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        2000,
        300
      ],
      "credentials": {
        "supabaseApi": "<your credential>"
      }
    }
  ],
  "connections": {
    "Google Drive Trigger": {
      "main": [
        [
          {
            "node": "Download File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download File": {
      "main": [
        [
          {
            "node": "Extract Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Content": {
      "main": [
        [
          {
            "node": "Split In Chunks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split In Chunks": {
      "main": [
        [
          {
            "node": "Create Embeddings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Embeddings": {
      "main": [
        [
          {
            "node": "Prepare Vectors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Vectors": {
      "main": [
        [
          {
            "node": "Delete Old Vectors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Old Vectors": {
      "main": [
        [
          {
            "node": "Insert Updated Vectors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "1"
}