{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "d4e5b3fc-0ab5-4900-b551-0bdac8f6b3e7",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -1340,
        1020
      ],
      "parameters": {
        "options": {
          "temperature": 0.2
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7cacd167-0172-4032-bf93-6184fb4edca9",
      "name": "Default Data Loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        1220,
        1760
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "=file_id",
                "value": "={{ $('Loop Over Items').item.json.file_id}}"
              },
              {
                "name": "file_name",
                "value": "={{ $('Loop Over Items').item.json.file_name}}"
              }
            ]
          }
        },
        "jsonData": "={{ $json.data || $json.text || $json.appended_data }}",
        "jsonMode": "expressionData"
      },
      "typeVersion": 1
    },
    {
      "id": "f3ce8818-db70-45ce-8ca4-9407ccc684e6",
      "name": "Embeddings OpenAI1",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        1060,
        1820
      ],
      "parameters": {
        "model": "text-embedding-3-small",
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7e10f8fb-8d93-46c9-9c02-f78f026b112e",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -740,
        940
      ],
      "parameters": {
        "color": 4,
        "width": 343,
        "height": 489,
        "content": "## Agent Tools for RAG"
      },
      "typeVersion": 1
    },
    {
      "id": "ccc7b130-0fb3-4a4e-8858-34afcaf566a9",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1960,
        1520
      ],
      "parameters": {
        "color": 5,
        "width": 533,
        "height": 507,
        "content": "## Tool to Add a Google Drive File to Vector DB"
      },
      "typeVersion": 1
    },
    {
      "id": "aaabdc32-0009-4076-8e90-f94dea0be81e",
      "name": "Download File",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -780,
        1720
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Loop Over Items').item.json.file_id }}"
        },
        "options": {
          "googleFileConversion": {
            "conversion": {
              "docsToFormat": "text/plain",
              "sheetsToFormat": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            }
          }
        },
        "operation": "download"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "executeOnce": true,
      "typeVersion": 3
    },
    {
      "id": "6a57ed28-c1b6-4f8c-9d60-d8004dd0717f",
      "name": "File Created",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        -1880,
        1620
      ],
      "parameters": {
        "event": "fileCreated",
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "mode": "list",
          "value": "16kHzSvQlrYZdpy2FcvInOAGMpWZGj1ai",
          "cachedResultUrl": "https://drive.google.com/drive/folders/16kHzSvQlrYZdpy2FcvInOAGMpWZGj1ai",
          "cachedResultName": "product-car-lease-docs"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "9db93b4a-870e-46f9-a08f-921e72c548a5",
      "name": "File Updated",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        -1880,
        1840
      ],
      "parameters": {
        "event": "fileUpdated",
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "triggerOn": "specificFolder",
        "folderToWatch": {
          "__rl": true,
          "mode": "list",
          "value": "16kHzSvQlrYZdpy2FcvInOAGMpWZGj1ai",
          "cachedResultUrl": "https://drive.google.com/drive/folders/16kHzSvQlrYZdpy2FcvInOAGMpWZGj1ai",
          "cachedResultName": "product-car-lease-docs"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "ad0b03de-8305-4393-a920-4bcaa082c681",
      "name": "Extract Document Text",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        120,
        1980
      ],
      "parameters": {
        "options": {},
        "operation": "text"
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "9d5fee3d-b6af-4e3f-9e7f-257a87292c88",
      "name": "Embeddings OpenAI",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        -700,
        1260
      ],
      "parameters": {
        "model": "text-embedding-3-small",
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "75af40d5-62b0-4faf-b1fd-37dab8feb143",
      "name": "Delete Old Doc Rows",
      "type": "n8n-nodes-base.supabase",
      "onError": "continueRegularOutput",
      "position": [
        -940,
        1800
      ],
      "parameters": {
        "tableId": "documents",
        "operation": "delete",
        "filterType": "string",
        "filterString": "=metadata->>file_id=like.*{{ $json.file_id }}*"
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "20d282f6-ea7b-48a6-a0aa-b864a4d85396",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1800,
        680
      ],
      "parameters": {
        "width": 1036,
        "height": 485,
        "content": "## RAG AI Agent with Chat Interface"
      },
      "typeVersion": 1
    },
    {
      "id": "35e28ac3-4fd8-4179-9526-b6d15c87eaa6",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        -940,
        720
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "7261cb7a-d3a6-4d20-8e5c-d4176e0b1dd7",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        -1460,
        800
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "9a9a245e-f1a1-4282-bb02-a81ffe629f0f",
              "name": "chatInput",
              "type": "string",
              "value": "={{ $json?.chatInput || $json.body.chatInput || $json.body.query }} "
            },
            {
              "id": "b80831d8-c653-4203-8706-adedfdb98f77",
              "name": "sessionId",
              "type": "string",
              "value": "={{ $json?.sessionId || $json.body.sessionId}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "a4dae2ba-f596-499a-a2cf-040501efaa7d",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -1720,
        800
      ],
      "parameters": {
        "public": true,
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "c695b449-4624-468e-9cd7-605da16b7915",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -1720,
        1000
      ],
      "parameters": {
        "path": "9ba11544-5c4e-4f91-818a-08a4ecb596c5",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "d300d76a-47e0-49df-9a21-5d334e68162c",
      "name": "Extract PDF Text",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        60,
        1200
      ],
      "parameters": {
        "options": {},
        "operation": "pdf"
      },
      "typeVersion": 1
    },
    {
      "id": "1f625839-c8f9-4eca-8616-eeaa0c3bd96c",
      "name": "Character Text Splitter",
      "type": "@n8n/n8n-nodes-langchain.textSplitterCharacterTextSplitter",
      "position": [
        1220,
        1980
      ],
      "parameters": {
        "chunkSize": 2000,
        "chunkOverlap": 50
      },
      "typeVersion": 1
    },
    {
      "id": "6064bfd7-eda9-4ab5-9c5c-59c197e5e3cf",
      "name": "Summarize",
      "type": "n8n-nodes-base.summarize",
      "position": [
        140,
        1600
      ],
      "parameters": {
        "options": {
          "outputFormat": "singleItem",
          "disableDotNotation": true
        },
        "fieldsToSummarize": {
          "values": [
            {
              "field": "data",
              "aggregation": "append",
              "includeEmpty": true
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "438dd4b2-81af-4ddc-a7b7-0d8509c57fc3",
      "name": "RAG AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -1240,
        800
      ],
      "parameters": {
        "text": "=user: {{ $json.chatInput }}\n\nAlways use ' Vector Store' tool  to get correct iinformation.\nAlways ask questions and only provide limited information which exactly matches user's needs.  \nAsk for each detail first, each need and requirement of the user.",
        "options": {
          "systemMessage": "=You are a helpful assistant."
        },
        "promptType": "define"
      },
      "typeVersion": 1.6
    },
    {
      "id": "bda5d65a-608b-41a5-8884-d85558020a27",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        -580,
        1720
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "fa3c1e8c-3e54-423e-849d-390cf842870d",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $('Loop Over Items').item.json.file_type}}",
                    "rightValue": "application/pdf"
                  }
                ]
              }
            },
            {
              "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": "={{ $('Loop Over Items').item.json.file_type}}",
                    "rightValue": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                  }
                ]
              }
            },
            {
              "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": "={{ $('Loop Over Items').item.json.file_type}}",
                    "rightValue": "application/vnd.google-apps.document"
                  }
                ]
              }
            }
          ]
        },
        "options": {
          "fallbackOutput": 2
        }
      },
      "typeVersion": 3
    },
    {
      "id": "de305c39-ce48-4e04-9167-db00a78d8e6d",
      "name": "Insert into Supabase Vectorstore",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "position": [
        1080,
        1540
      ],
      "parameters": {
        "mode": "insert",
        "options": {
          "queryName": "match_documents"
        },
        "tableName": {
          "__rl": true,
          "mode": "list",
          "value": "documents",
          "cachedResultName": "documents"
        }
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "69e46f85-77c3-45f6-96bc-4fff02b733d5",
      "name": "Extract from Excel",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        -220,
        1600
      ],
      "parameters": {
        "options": {
          "headerRow": true
        },
        "operation": "xlsx"
      },
      "typeVersion": 1
    },
    {
      "id": "049cfd79-66a0-4def-9034-fcb2f054b480",
      "name": "Calculator",
      "type": "@n8n/n8n-nodes-langchain.toolCalculator",
      "position": [
        -1040,
        1040
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "dbe03d9d-e526-40ea-8737-85e11bb44359",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -1140,
        1640
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "d94daf22-e6e6-423d-8def-4c4f4301229b",
      "name": "FinishNode",
      "type": "n8n-nodes-base.set",
      "position": [
        -820,
        1540
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "78ef64a9-7023-40c3-9a4b-f6a33fc33d55",
              "name": "response",
              "type": "string",
              "value": "done"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "6dc86b42-0b0b-44c1-9978-d558c3ef890b",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "position": [
        1660,
        1980
      ],
      "parameters": {
        "amount": 1
      },
      "typeVersion": 1.1
    },
    {
      "id": "736594be-6e78-43d8-826f-ed024f85c9fb",
      "name": "Set File ID1",
      "type": "n8n-nodes-base.set",
      "position": [
        -1580,
        1760
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "10646eae-ae46-4327-a4dc-9987c2d76173",
              "name": "file_id",
              "type": "string",
              "value": "={{ $json.id }}"
            },
            {
              "id": "f4536df5-d0b1-4392-bf17-b8137fb31a44",
              "name": "file_type",
              "type": "string",
              "value": "={{ $json.mimeType }}"
            },
            {
              "id": "ee8b4d04-665b-40a7-8542-981a2b224c9c",
              "name": "file_name",
              "type": "string",
              "value": "={{ $json.name }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "9eebfa64-24ba-4df4-911a-722827c0c7f3",
      "name": "Split Out1",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        -1380,
        1640
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "file_id, file_type, file_name"
      },
      "typeVersion": 1
    },
    {
      "id": "9c02b8fd-ca9a-49d5-99d0-e26d0dc685b7",
      "name": "Code1",
      "type": "n8n-nodes-base.code",
      "position": [
        -20,
        1600
      ],
      "parameters": {
        "jsCode": "// This function processes each Excel row individually\nfor (const item of items) {\n  // Create a formatted string representation of the complete row\n  const rowData = {};\n  \n  // Copy all fields from the Excel row\n  for (const key in item.json) {\n    rowData[key] = item.json[key];\n  }\n  \n  // Create a single JSON object for the entire row\n  item.json = {\n    data: JSON.stringify(rowData),\n    rowData: rowData  // Keep the original data structure too\n  };\n}\n\nreturn items;"
      },
      "typeVersion": 2
    },
    {
      "id": "b67017fe-f12e-4456-b578-46ea97a5ec6c",
      "name": "Split Out",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        300,
        1600
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "appended_data"
      },
      "typeVersion": 1
    },
    {
      "id": "6b194d3c-d184-4426-862d-9f6df8a9cf70",
      "name": "Loop Over Items1",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        500,
        1620
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "88707cf6-10f6-4154-bd12-3d519afc8584",
      "name": "FinishNode1",
      "type": "n8n-nodes-base.set",
      "position": [
        820,
        1400
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "78ef64a9-7023-40c3-9a4b-f6a33fc33d55",
              "name": "response",
              "type": "string",
              "value": "done"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "b2643480-0ac3-42b2-960c-03d8d5dcb4f6",
      "name": "Cars Data Vector Store",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "position": [
        -720,
        1100
      ],
      "parameters": {
        "mode": "retrieve-as-tool",
        "topK": 10,
        "options": {},
        "toolName": "cars_data",
        "tableName": {
          "__rl": true,
          "mode": "list",
          "value": "documents",
          "cachedResultName": "documents"
        },
        "toolDescription": "Use this tool to Get the cars related data to answer the query."
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "45e7cd38-667b-4080-9da7-f1f07e3913aa",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -1160,
        1020
      ],
      "parameters": {},
      "typeVersion": 1.3
    }
  ],
  "connections": {
    "Wait": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          },
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Extract PDF Text",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract from Excel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract Document Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculator": {
      "ai_tool": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Split Out1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "RAG AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "File Created": {
      "main": [
        [
          {
            "node": "Set File ID1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "File Updated": {
      "main": [
        [
          {
            "node": "Set File ID1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "RAG AI Agent": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set File ID1": {
      "main": [
        [
          {
            "node": "Split Out1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download File": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "FinishNode",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Delete Old Doc Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract PDF Text": {
      "main": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items1": {
      "main": [
        [
          {
            "node": "FinishNode1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI": {
      "ai_embedding": [
        [
          {
            "node": "Cars Data Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI1": {
      "ai_embedding": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Extract from Excel": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Default Data Loader": {
      "ai_document": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Delete Old Doc Rows": {
      "main": [
        [
          {
            "node": "Download File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Document Text": {
      "main": [
        [
          {
            "node": "Insert into Supabase Vectorstore",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cars Data Vector Store": {
      "ai_tool": [
        [
          {
            "node": "RAG AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Character Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Default Data Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert into Supabase Vectorstore": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}