{
  "name": "M7 Policy Q&A",
  "nodes": [
    {
      "parameters": {},
      "id": "a8bd6e95-834a-40f8-9383-8e22f931ff1b",
      "name": "Build the index",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "searchMethod": "name",
        "queryString": "",
        "returnAll": true,
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "list",
            "value": "PICK-YOUR-POLICIES-FOLDER"
          },
          "whatToSearch": "files"
        },
        "options": {}
      },
      "id": "f62ffc21-8751-4532-b832-c7c5750224f3",
      "name": "List policies",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "options": {}
      },
      "id": "5b87b44e-e324-4912-9c77-1f33978076c0",
      "name": "Download file",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "mode": "insert",
        "memoryKey": {
          "__rl": true,
          "mode": "list",
          "value": "policies"
        },
        "clearStore": true
      },
      "id": "407efcc7-fcc7-4241-9bac-bd68adde489f",
      "name": "Store passages",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "typeVersion": 1.3,
      "position": [
        680,
        0
      ]
    },
    {
      "parameters": {
        "dataType": "binary",
        "loader": "pdfLoader",
        "textSplittingMode": "custom",
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "source",
                "value": "={{ $('List policies').item.json.name }}"
              }
            ]
          }
        }
      },
      "id": "b5005113-606c-44c6-9351-2ecdb576ed1c",
      "name": "Read the file",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "typeVersion": 1.1,
      "position": [
        640,
        200
      ]
    },
    {
      "parameters": {
        "chunkSize": 1000,
        "chunkOverlap": 100,
        "options": {}
      },
      "id": "914046e9-60c6-4ddf-a6fe-1dee928180d3",
      "name": "Cut into passages",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "typeVersion": 1,
      "position": [
        640,
        380
      ]
    },
    {
      "parameters": {
        "model": "text-embedding-3-small",
        "options": {}
      },
      "id": "24fd3a08-3db1-4163-b2a7-7f8e750faf07",
      "name": "Embeddings (index)",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        920,
        200
      ],
      "credentials": {
        "openAiApi": {
          "id": null,
          "name": "",
          "__aiGatewayManaged": true
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "49cb126d-0388-4163-b094-9b1e9ea532c4",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.4,
      "position": [
        0,
        560
      ],
      "webhookId": "4da43fe6-5fa4-41f7-9e7a-c53f51280105"
    },
    {
      "parameters": {
        "promptType": "auto",
        "text": "={{ $json.chatInput }}",
        "options": {
          "systemPromptTemplate": "You answer staff questions using ONLY the policy passages provided to you.\nIf the passages do not contain the answer, reply exactly: \"Not found in the current policies. Please contact HR.\"\nNever use general knowledge. Never guess dates, amounts or limits.\nEnd every answer with: Source: [document name and section].\nKeep answers under 120 words.\n----------------\nContext: {context}"
        }
      },
      "id": "13cb705e-72e2-4af8-a145-e64870e12e0b",
      "name": "Answer from policies",
      "type": "@n8n/n8n-nodes-langchain.chainRetrievalQa",
      "typeVersion": 1.7,
      "position": [
        240,
        560
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "34ed8e0c-c291-4c8d-ac6b-09e0cb92b5c7",
              "name": "output",
              "value": "={{ $json.response }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "2708e984-d7fc-498e-a491-df30ce70f684",
      "name": "Reply",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        560,
        560
      ]
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.5-flash",
        "options": {
          "temperature": 0
        }
      },
      "id": "8e6ab49b-71a6-4afb-8c37-5025ebf112dd",
      "name": "Gemini (flash)",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        200,
        760
      ],
      "credentials": {
        "googlePalmApi": {
          "id": null,
          "name": "",
          "__aiGatewayManaged": true
        }
      }
    },
    {
      "parameters": {
        "topK": 4
      },
      "id": "5ce80929-d340-4ec7-a2ec-087f32396c2d",
      "name": "Find passages",
      "type": "@n8n/n8n-nodes-langchain.retrieverVectorStore",
      "typeVersion": 1,
      "position": [
        400,
        760
      ]
    },
    {
      "parameters": {
        "mode": "retrieve",
        "memoryKey": {
          "__rl": true,
          "mode": "list",
          "value": "policies"
        }
      },
      "id": "2de8be2c-ec4b-41ab-9e14-1f2a0f6cbb84",
      "name": "Policies index",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "typeVersion": 1.3,
      "position": [
        400,
        940
      ]
    },
    {
      "parameters": {
        "model": "text-embedding-3-small",
        "options": {}
      },
      "id": "f14daa4c-43d1-4f7d-9637-3be9173d3286",
      "name": "Embeddings (question)",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        400,
        1120
      ],
      "credentials": {
        "openAiApi": {
          "id": null,
          "name": "",
          "__aiGatewayManaged": true
        }
      }
    },
    {
      "parameters": {
        "content": "## 1. Build the index (run once)\nDrive folder **Policies** -> download each PDF -> cut into passages -> embeddings -> Simple Vector Store **policies**.\n\nRe-run when a document changes, and after the workspace restarts: the Simple Vector Store lives in memory.",
        "width": 300,
        "height": 260,
        "color": 4
      },
      "id": "7d185135-9d5c-4def-8473-a43d7aa0a109",
      "name": "Note a597",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -340,
        -60
      ]
    },
    {
      "parameters": {
        "content": "## 2. Answer questions\nOpen the chat (bottom of the canvas). The chain finds the 4 most relevant passages and answers ONLY from them, with a source, or says not found.\n\nKeep **{context}** in the system prompt.",
        "width": 300,
        "height": 260,
        "color": 5
      },
      "id": "948723c1-656e-4477-a2a5-126d9c817b84",
      "name": "Note c129",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -340,
        520
      ]
    }
  ],
  "connections": {
    "Build the index": {
      "main": [
        [
          {
            "node": "List policies",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List policies": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download file": {
      "main": [
        [
          {
            "node": "Store passages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read the file": {
      "ai_document": [
        [
          {
            "node": "Store passages",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Cut into passages": {
      "ai_textSplitter": [
        [
          {
            "node": "Read the file",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings (index)": {
      "ai_embedding": [
        [
          {
            "node": "Store passages",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Answer from policies",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Answer from policies": {
      "main": [
        [
          {
            "node": "Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini (flash)": {
      "ai_languageModel": [
        [
          {
            "node": "Answer from policies",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Find passages": {
      "ai_retriever": [
        [
          {
            "node": "Answer from policies",
            "type": "ai_retriever",
            "index": 0
          }
        ]
      ]
    },
    "Policies index": {
      "ai_vectorStore": [
        [
          {
            "node": "Find passages",
            "type": "ai_vectorStore",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings (question)": {
      "ai_embedding": [
        [
          {
            "node": "Policies index",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {}
}