{
  "id": "nmiExXyH4MBBlHYd",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Rag",
  "tags": [],
  "nodes": [
    {
      "id": "4536c1b7-c3c1-4d6c-b237-cc0b66b634a3",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -544,
        -352
      ],
      "parameters": {
        "color": 4,
        "width": 1072,
        "height": 544,
        "content": "## CHAT/RAG API \n"
      },
      "typeVersion": 1
    },
    {
      "id": "0eba02b3-4de3-43b1-931f-6ab34382a5c0",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        720,
        -208
      ],
      "parameters": {
        "color": 7,
        "width": 704,
        "height": 688,
        "content": "## DOCUMENT INGESTION\n"
      },
      "typeVersion": 1
    },
    {
      "id": "06d4ad1b-dbee-4a8b-8b06-84b4962dcfe5",
      "name": "Chat Request Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -496,
        -256
      ],
      "parameters": {
        "path": "0cad49b7-84e3-434c-8973-500ce6736c2c",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2.1
    },
    {
      "id": "e2ee255d-9a56-4770-8b80-09f603c7ef56",
      "name": "RAG Document Assistant",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -160,
        -256
      ],
      "parameters": {
        "text": "={{ $json.body.message }}",
        "options": {
          "systemMessage": "You are a document-based AI assistant.\n\nAnswer questions using ONLY the retrieved document context.\n\nDo not add external knowledge, assumptions, or general facts unless explicitly stated in the retrieved documents.\n\nIf the answer is not found in the retrieved context, say:\n\"I could not find this information in the uploaded documents.\"\n\nKeep answers concise and grounded."
        },
        "promptType": "define"
      },
      "typeVersion": 3.1
    },
    {
      "id": "9d1dda23-88d7-4f7f-b39c-0b4f49be1cd8",
      "name": "OpenRouter LLM",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        -304,
        -48
      ],
      "parameters": {
        "model": "deepseek/deepseek-chat-v3-0324",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e5d09792-9bb0-4904-8b83-48d2d6b860bb",
      "name": "Supabase Vector Retriever",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "position": [
        48,
        0
      ],
      "parameters": {
        "mode": "retrieve-as-tool",
        "topK": 5,
        "options": {
          "queryName": "match_documents"
        },
        "tableName": {
          "__rl": true,
          "mode": "list",
          "value": "documents",
          "cachedResultName": "documents"
        },
        "toolDescription": "Search and retrieve relevant information from indexed documents to answer user questions accurately."
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "ae0bd350-f60b-487a-80b9-8e80432818cd",
      "name": "Return AI Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        256,
        -256
      ],
      "parameters": {
        "options": {},
        "respondWith": "json",
        "responseBody": "={{ { \"answer\": $json.output } }}"
      },
      "typeVersion": 1.5
    },
    {
      "id": "a730cbe9-5911-4c2e-b606-07857c7b278a",
      "name": "Google Drive PDF Downloader",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        784,
        -96
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "list",
          "value": "16oWzN5VwqKi29cV0tSto2TwecG-_V3m7Ht4DRM7s56w",
          "cachedResultUrl": "https://docs.google.com/document/d/16oWzN5VwqKi29cV0tSto2TwecG-_V3m7Ht4DRM7s56w/edit?usp=drivesdk",
          "cachedResultName": "Why Modern Agriculture Is Right for You"
        },
        "options": {
          "googleFileConversion": {
            "conversion": {
              "docsToFormat": "application/pdf"
            }
          }
        },
        "operation": "download"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "2acfa6de-09d6-449d-bee3-8a160b92823c",
      "name": "Supabase Vector Store",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "position": [
        1056,
        -96
      ],
      "parameters": {
        "mode": "insert",
        "options": {},
        "tableName": {
          "__rl": true,
          "mode": "list",
          "value": "documents",
          "cachedResultName": "documents"
        }
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "af7b7c98-a40a-4ddb-9a37-7eac210a32f8",
      "name": "Document Loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        1136,
        112
      ],
      "parameters": {
        "options": {},
        "dataType": "binary",
        "textSplittingMode": "custom"
      },
      "typeVersion": 1.1
    },
    {
      "id": "b5c0aa38-d4a4-4213-b6a9-7dcfba240b65",
      "name": "Gemini Embedding Model",
      "type": "@n8n/n8n-nodes-langchain.embeddingsGoogleGemini",
      "position": [
        480,
        416
      ],
      "parameters": {
        "modelName": "models/gemini-embedding-2-preview"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "14a33f15-994a-4715-830e-1e950cfa30ce",
      "name": "Recursive Text Splitter",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        1136,
        304
      ],
      "parameters": {
        "options": {},
        "chunkSize": 500,
        "chunkOverlap": 100
      },
      "typeVersion": 1
    },
    {
      "id": "c970fc1e-074b-405a-bc4c-ccc80c7a7813",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1040,
        -352
      ],
      "parameters": {
        "color": 2,
        "width": 464,
        "height": 736,
        "content": "## RAG Chat API\n\nThis workflow handles the document question-answering pipeline.\n\n### Flow\n1. Receives user questions through webhook\n2. Searches Supabase vector database\n3. Retrieves relevant document chunks\n4. Sends retrieved context to the AI model\n5. Returns grounded AI responses\n\n### Components\n- Webhook API\n- OpenRouter LLM\n- Supabase Vector Retrieval\n- AI Agent orchestration\n\n### Frontend Integration\nThis endpoint can be connected to:\n- Lovable\n- Next.js\n- React\n- Webflow\n- Custom frontend applications"
      },
      "typeVersion": 1
    },
    {
      "id": "6b8c2526-4fff-4bc2-917e-6c4aab9e4178",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1472,
        -208
      ],
      "parameters": {
        "color": 6,
        "width": 448,
        "height": 464,
        "content": "## Document Ingestion Pipeline\n\nThis workflow converts uploaded documents into searchable vector embeddings.\n\n### Flow\n1. Downloads PDF documents from Google Drive\n2. Extracts raw document text\n3. Splits content into smaller chunks\n4. Generates vector embeddings\n5. Stores embeddings in Supabase pgvector\n\n### Purpose\nThe stored embeddings are later used for semantic retrieval during RAG conversations."
      },
      "typeVersion": 1
    },
    {
      "id": "a7bd1c68-31c3-4a49-b787-dd83fe9fa97a",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1616,
        -352
      ],
      "parameters": {
        "color": "#52265E",
        "width": 384,
        "height": 928,
        "content": "## Build a RAG document chat assistant using Supabase and AI in n8n\n\nThis template demonstrates how to build a Retrieval-Augmented Generation (RAG) workflow inside n8n using Supabase Vector Store and AI models.\n\nThe workflow includes:\n- Document ingestion\n- Recursive chunking\n- Embedding generation\n- Vector search\n- Semantic retrieval\n- AI-powered question answering\n- Webhook API integration\n\n### Stack\n- n8n\n- Supabase pgvector\n- OpenRouter\n- Gemini Embeddings\n- Google Drive\n\n### Recommended Use Cases\n- Internal knowledge assistants\n- AI document chat\n- PDF search systems\n- Knowledge base retrieval\n- AI support assistants\n\n### Setup Requirements\n- Supabase account\n- OpenRouter API key\n- Google Drive credentials\n- pgvector enabled in Supabase"
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "741b8ce1-2b40-45d5-b93c-46f3afdb8e99",
  "connections": {
    "OpenRouter LLM": {
      "ai_languageModel": [
        [
          {
            "node": "RAG Document Assistant",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Document Loader": {
      "ai_document": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Chat Request Webhook": {
      "main": [
        [
          {
            "node": "RAG Document Assistant",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini Embedding Model": {
      "ai_embedding": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "ai_embedding",
            "index": 0
          },
          {
            "node": "Supabase Vector Retriever",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "RAG Document Assistant": {
      "main": [
        [
          {
            "node": "Return AI Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Document Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Supabase Vector Retriever": {
      "ai_tool": [
        [
          {
            "node": "RAG Document Assistant",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive PDF Downloader": {
      "main": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}