{
  "name": "Citizen-support RAG assistant",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "rag/ingest",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "fbd8c4f1-5809-489d-a742-3eea31b93cc2",
      "name": "Upload PDF",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "webhookId": "bdfb728f-3e87-4b26-af8e-fe98725322d2"
    },
    {
      "parameters": {
        "operation": "pdf",
        "options": {}
      },
      "id": "8fb59fef-86b8-4b78-a4a2-24872b491899",
      "name": "Extract text",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "position": [
        512,
        300
      ]
    },
    {
      "parameters": {
        "dataType": "binary",
        "options": {}
      },
      "id": "709ef658-f1e3-46f2-aa07-0d7d336e6110",
      "name": "Load document",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "typeVersion": 1,
      "position": [
        784,
        534
      ]
    },
    {
      "parameters": {
        "chunkSize": 900,
        "chunkOverlap": 120,
        "options": {}
      },
      "id": "3b0db561-9833-4c12-aea2-763ac8550e3c",
      "name": "Split into chunks",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "typeVersion": 1,
      "position": [
        784,
        767
      ]
    },
    {
      "parameters": {
        "model": "nomic-embed-text:latest"
      },
      "id": "61c10686-a781-44e7-a179-1ec67b31fc7f",
      "name": "Embed (local)",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOllama",
      "typeVersion": 1,
      "position": [
        1056,
        534
      ]
    },
    {
      "parameters": {
        "mode": "insert",
        "tableName": "support_chunks",
        "options": {}
      },
      "id": "badea3ce-b9cd-4bcb-a5f2-f47bf0ae760a",
      "name": "Store in pgvector",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePGVector",
      "typeVersion": 1,
      "position": [
        784,
        300
      ],
      "credentials": {
        "postgres": {
          "id": "REPLACE_ME",
          "name": "Postgres (self-hosted)"
        }
      }
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "rag/ask",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "04636c5b-78af-459e-a572-c563d4f62527",
      "name": "Question",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        1059
      ],
      "webhookId": "3a017183-3bfd-442d-aa51-1c3c7f46e2b7"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.body.question }}",
        "options": {
          "systemPromptTemplate": "Answer using only the context. If the context does not contain the answer, say that you do not know.\n\n{context}"
        }
      },
      "id": "1bcb9405-2609-4f9f-a2d3-c613a1661e0c",
      "name": "Answer from sources",
      "type": "@n8n/n8n-nodes-langchain.chainRetrievalQa",
      "typeVersion": 1.4,
      "position": [
        512,
        1059
      ]
    },
    {
      "parameters": {
        "model": "llama3.1:8b",
        "options": {
          "temperature": 0.1
        }
      },
      "id": "67970229-dc57-4c07-a1ff-a5be9d0edaa6",
      "name": "Local chat model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        512,
        1293
      ]
    },
    {
      "parameters": {
        "topK": 5
      },
      "id": "de87c347-8fd3-495d-aaa6-c5aa55d4f7b7",
      "name": "Retriever",
      "type": "@n8n/n8n-nodes-langchain.retrieverVectorStore",
      "typeVersion": 1,
      "position": [
        784,
        1293
      ]
    },
    {
      "parameters": {
        "mode": "retrieve",
        "tableName": "support_chunks",
        "options": {}
      },
      "id": "4affbb43-39da-4b76-aa63-9fba9b3283be",
      "name": "Search pgvector",
      "type": "@n8n/n8n-nodes-langchain.vectorStorePGVector",
      "typeVersion": 1,
      "position": [
        1056,
        1293
      ],
      "credentials": {
        "postgres": {
          "id": "REPLACE_ME",
          "name": "Postgres (self-hosted)"
        }
      }
    },
    {
      "parameters": {
        "model": "nomic-embed-text:latest"
      },
      "id": "40608e83-d4aa-4d56-ac70-b5761dd3d242",
      "name": "Embed query",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOllama",
      "typeVersion": 1,
      "position": [
        1328,
        1293
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "schema": {
          "__rl": true,
          "value": "public",
          "mode": "list"
        },
        "table": {
          "__rl": true,
          "value": "support_audit",
          "mode": "list"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "question": "={{ $(\"Question\").item.json.body.question }}",
            "answer": "={{ $json.response.text }}"
          }
        }
      },
      "id": "011812aa-ffbe-4d14-aadc-7daf19a28c74",
      "name": "Write audit log",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        784,
        1059
      ],
      "credentials": {
        "postgres": {
          "id": "REPLACE_ME",
          "name": "Postgres (self-hosted)"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { answer: $(\"Answer from sources\").item.json.response.text } }}",
        "options": {}
      },
      "id": "5a2eef7c-27f5-45d1-a7cc-d062c9b6fcd8",
      "name": "Reply",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1056,
        1059
      ]
    }
  ],
  "connections": {
    "Upload PDF": {
      "main": [
        [
          {
            "node": "Extract text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract text": {
      "main": [
        [
          {
            "node": "Store in pgvector",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load document": {
      "ai_document": [
        [
          {
            "node": "Store in pgvector",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Split into chunks": {
      "ai_textSplitter": [
        [
          {
            "node": "Load document",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Embed (local)": {
      "ai_embedding": [
        [
          {
            "node": "Store in pgvector",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Question": {
      "main": [
        [
          {
            "node": "Answer from sources",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Answer from sources": {
      "main": [
        [
          {
            "node": "Write audit log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write audit log": {
      "main": [
        [
          {
            "node": "Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Local chat model": {
      "ai_languageModel": [
        [
          {
            "node": "Answer from sources",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Retriever": {
      "ai_retriever": [
        [
          {
            "node": "Answer from sources",
            "type": "ai_retriever",
            "index": 0
          }
        ]
      ]
    },
    "Search pgvector": {
      "ai_vectorStore": [
        [
          {
            "node": "Retriever",
            "type": "ai_vectorStore",
            "index": 0
          }
        ]
      ]
    },
    "Embed query": {
      "ai_embedding": [
        [
          {
            "node": "Search pgvector",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "Answers from your own documents, on your own server."
  },
  "tags": []
}