{
  "name": "RAG knowledge-base chatbot",
  "nodes": [
    {
      "parameters": {
        "content": "## RAG knowledge-base chatbot\n\nPortfolio demo only. This workflow is an original example for demonstration. It is not client work and it does not represent a deployment for any customer.\n\nRun **Run demo** to execute the offline rules engine. The live branch is used only when the incoming item sets `demo` to false and the placeholder credentials have been replaced.",
        "height": 220,
        "width": 520,
        "color": 4
      },
      "id": "75c80292-a734-4c41-b65e-43810d06fc51",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -40
      ]
    },
    {
      "parameters": {},
      "id": "8e9817d7-9025-4973-a983-f473289a351f",
      "name": "Run demo",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        40,
        280
      ],
      "notes": "CLI and the n8n editor start here. An empty manual run loads the bundled sample.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "rag-knowledge-base-chatbot",
        "authentication": "none",
        "responseMode": "lastNode",
        "responseData": "firstEntryJson",
        "options": {}
      },
      "id": "e6c73417-8f6a-48ae-8c33-df33c7f5cc5f",
      "name": "Inbound webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        40,
        460
      ],
      "notes": "POST JSON to this path. Include demo:false only after credentials are connected.",
      "notesInFlow": true,
      "webhookId": "c83b5fe9-e673-4894-9919-04aab5a415d1"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const sample = {\n  \"demo\": true,\n  \"question\": \"What is your refund policy?\",\n  \"prompt\": \"What is your refund policy?\",\n  \"documents\": [\n    {\n      \"text\": \"Refunds are available within 14 days.\"\n    }\n  ]\n};\nconst incoming = $input.first()?.json ?? {};\nconst provided = Object.keys(incoming).filter((key) => incoming[key] !== undefined && incoming[key] !== null && incoming[key] !== '');\nconst base = provided.length ? { ...sample, ...incoming } : { ...sample };\nconst flag = incoming.demo;\nbase.demo = !(flag === false || flag === 'false' || flag === 0 || flag === '0');\nreturn [{ json: base }];\n"
      },
      "id": "c7b399de-55fc-4f88-9b29-3041ea159ab8",
      "name": "Normalize input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        340,
        360
      ],
      "notes": "Merges the request over the bundled sample. demo stays true unless the caller explicitly turns it off.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nconst produced = (() => {\n\nconst book = {\n  'refund': 'Refunds are available within 14 days of the original charge.',\n  'shipping': 'Standard shipping takes three business days.',\n  'password': 'Password resets are sent from the login page and expire in 30 minutes.'\n};\nconst prompt = String(item.prompt || item.question || item.text || '').toLowerCase();\nconst keys = Object.keys(book);\nconst hit = keys.find((key) => prompt.includes(key)) || null;\nconst answer = hit ? book[hit] : 'I do not have a grounded answer for that in the demo knowledge set. Ask about: ' + keys.join(', ') + '.';\nconst record = { answer, topic: hit, persona: 'support librarian', sessionId: item.sessionId || 'portfolio-demo' };\nreturn {\n  summary: answer,\n  route: hit ? 'answered' : 'fallback',\n  prompt: item.prompt || item.question || item.text || '',\n  record,\n  slackText: (hit ? 'Answered' : 'Fallback') + ': ' + answer,\n  actions: [{ app: 'agent', operation: 'reply', payload: record }],\n};\n\n})();\nif (!produced || typeof produced.summary !== 'string' || !produced.summary.trim()) {\n  throw new Error('Apply rules must return a summary string');\n}\nreturn [{\n  json: {\n    ...item,\n    ...produced,\n    demo: item.demo !== false,\n  },\n}];\n"
      },
      "id": "89189271-41d7-4f14-a910-79515dbb60ef",
      "name": "Apply rules",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        620,
        360
      ],
      "notes": "Deterministic stand-in for the model or enrichment API. Replace or precede this with a live model node when you turn demo mode off.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "87cebd3e-1901-4585-933c-1060ed157f06",
              "leftValue": "={{ $json.demo }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "f553a8c2-55fe-4a80-9674-5c6fedb3e18c",
      "name": "Demo mode?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        900,
        360
      ],
      "notes": "True: package the offline result. False: run the credentialed apps.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nreturn [{\n  json: {\n    demo: true,\n    status: 'demo_ready',\n    workflow: 'rag-knowledge-base-chatbot',\n    summary: item.summary,\n    route: item.route ?? null,\n    record: item.record ?? {},\n    actions: item.actions ?? [],\n    slackText: item.slackText ?? item.summary,\n    notes: 'Demo mode did not call external apps. Send {\"demo\": false} after placeholder credentials are replaced.',\n  },\n}];\n"
      },
      "id": "b443f7c0-62dc-497b-aae7-3b4b1f0c95a8",
      "name": "Package demo result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1180,
        180
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.question || $json.prompt }}"
      },
      "id": "d83a3b8a-c3cf-445d-b59e-a1699abce7f9",
      "name": "Answer from knowledge base",
      "type": "@n8n/n8n-nodes-langchain.chainRetrievalQa",
      "typeVersion": 1.7,
      "position": [
        1220,
        780
      ],
      "notes": "Live retrieval chain. Demo mode answers from the local keyword matcher in Apply rules.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {
          "temperature": 0.1
        }
      },
      "id": "60d58096-16eb-4d6c-8a83-e3fee5a7b7ce",
      "name": "Groq chat model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "typeVersion": 1,
      "position": [
        980,
        980
      ],
      "credentials": {
        "groqApi": {
          "id": "PLACEHOLDER_GROQAPI",
          "name": "Groq account"
        }
      }
    },
    {
      "parameters": {
        "topK": 4
      },
      "id": "8006496a-a7aa-41bb-b1ac-5f0264f08302",
      "name": "Vector store retriever",
      "type": "@n8n/n8n-nodes-langchain.retrieverVectorStore",
      "typeVersion": 1,
      "position": [
        1220,
        1020
      ]
    },
    {
      "parameters": {
        "mode": "retrieve",
        "memoryKey": {
          "__rl": true,
          "mode": "list",
          "value": "portfolio_kb"
        },
        "topK": 4
      },
      "id": "7e78b2c2-8a97-42c8-9a2d-88cda9faa22a",
      "name": "Knowledge vector store",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "typeVersion": 1.3,
      "position": [
        980,
        1220
      ]
    },
    {
      "parameters": {
        "mode": "insert",
        "memoryKey": {
          "__rl": true,
          "mode": "list",
          "value": "portfolio_kb"
        },
        "clearStore": false
      },
      "id": "2df193ee-ffc1-4f23-8bd3-2721ab333880",
      "name": "Insert into knowledge base",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "typeVersion": 1.3,
      "position": [
        1540,
        1220
      ]
    },
    {
      "parameters": {
        "modelName": "models/gemini-embedding-001"
      },
      "id": "2070bda7-9dff-4ebc-9ac6-f14179d588be",
      "name": "Embeddings",
      "type": "@n8n/n8n-nodes-langchain.embeddingsGoogleGemini",
      "typeVersion": 1,
      "position": [
        760,
        1420
      ],
      "credentials": {
        "googlePalmApi": {
          "id": "PLACEHOLDER_GOOGLEPALMAPI",
          "name": "Google Gemini account"
        }
      }
    },
    {
      "parameters": {
        "dataType": "json",
        "jsonMode": "expressionData",
        "jsonData": "={{ $json.documents }}",
        "textSplittingMode": "custom"
      },
      "id": "af0ac901-f55c-4e58-9ca3-f596cd234bee",
      "name": "Default data loader",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "typeVersion": 1.1,
      "position": [
        1760,
        1420
      ]
    },
    {
      "parameters": {
        "chunkSize": 800,
        "chunkOverlap": 120
      },
      "id": "094ae13a-bacf-4b91-8a74-43be38d7a1e7",
      "name": "Text splitter",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "typeVersion": 1,
      "position": [
        1760,
        1620
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const docs = $input.first().json.documents || [];\nreturn docs.map((doc) => ({ json: { documents: doc } }));"
      },
      "id": "f9a5d892-78b5-44e1-b74d-7d8803cd6342",
      "name": "Document seed",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1540,
        980
      ],
      "notes": "Wire a schedule or a crawl into this node to refresh the store. It is not on the demo execution path.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "content": "## Live path\n\nReplace every `PLACEHOLDER_*` credential, then POST the sample with `\"demo\": false`.\n\nThe nodes on this branch use the `record` and `actions` fields produced by Apply rules.",
        "height": 180,
        "width": 420,
        "color": 6
      },
      "id": "746c7c96-5240-4664-9278-9734caa0be85",
      "name": "Live path",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1120,
        520
      ]
    }
  ],
  "connections": {
    "Run demo": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Inbound webhook": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize input": {
      "main": [
        [
          {
            "node": "Apply rules",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apply rules": {
      "main": [
        [
          {
            "node": "Demo mode?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Demo mode?": {
      "main": [
        [
          {
            "node": "Package demo result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Answer from knowledge base",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Groq chat model": {
      "ai_languageModel": [
        [
          {
            "node": "Answer from knowledge base",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Vector store retriever": {
      "ai_retriever": [
        [
          {
            "node": "Answer from knowledge base",
            "type": "ai_retriever",
            "index": 0
          }
        ]
      ]
    },
    "Knowledge vector store": {
      "ai_vectorStore": [
        [
          {
            "node": "Vector store retriever",
            "type": "ai_vectorStore",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings": {
      "ai_embedding": [
        [
          {
            "node": "Knowledge vector store",
            "type": "ai_embedding",
            "index": 0
          },
          {
            "node": "Insert into knowledge base",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Default data loader": {
      "ai_document": [
        [
          {
            "node": "Insert into knowledge base",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Text splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Default data loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Document seed": {
      "main": [
        [
          {
            "node": "Insert into knowledge base",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "A question is answered from a tiny local knowledge set in demo mode. The live branch is a retrieval chain: Gemini embeddings, an in-memory vector store, a retriever, and a Groq chat model. An insert path is on the canvas for loading documents."
  },
  "active": false
}
