{"active":false,"connections":{"Build Context":{"main":[[{"index":0,"node":"Answer from Docs","type":"main"}]]},"Chat Trigger":{"main":[[{"index":0,"node":"Embed Question","type":"main"}]]},"Embed Document":{"main":[[{"index":0,"node":"Upsert to Vector DB","type":"main"}]]},"Embed Question":{"main":[[{"index":0,"node":"Search Vector DB","type":"main"}]]},"Ingest Webhook Trigger":{"main":[[{"index":0,"node":"Prepare Document","type":"main"}]]},"OpenAI Chat Model":{"ai_languageModel":[[{"index":0,"node":"Answer from Docs","type":"ai_languageModel"}]]},"Prepare Document":{"main":[[{"index":0,"node":"Embed Document","type":"main"}]]},"Search Vector DB":{"main":[[{"index":0,"node":"Build Context","type":"main"}]]}},"meta":{"instanceId":"","templateCredsSetupCompleted":false},"name":"RAG Chatbot over Company Documents","nodes":[{"id":"3c4d5e6f-0000-4a5b-8c9d-000000000000","name":"Sticky Note","parameters":{"color":4,"content":"## RAG Chatbot over Company Documents\n\nTwo separate chains in one workflow:\n\n**CHAIN 1 — Chat (answer questions):** Chat Trigger → Embed Question → Search Vector DB → Build Context → AI Agent answers ONLY from the retrieved context.\n**CHAIN 2 — Ingestion (load docs):** POST doc text to the *Ingest Webhook Trigger* → Prepare Document → Embed Document → Upsert to Vector DB.\n\n**Setup:**\n1. Create an **OpenAI** credential and an **HTTP Header Auth** credential named *OpenAI Bearer* (header `Authorization`, value `Bearer YOUR_OPENAI_API_KEY`). Select *OpenAI Bearer* in *Embed Question* and *Embed Document*; select the OpenAI credential in *OpenAI Chat Model*.\n2. Create an **HTTP Header Auth** credential for your vector DB (Qdrant, Pinecone, …), select it in *Search Vector DB* and *Upsert to Vector DB*, and replace both placeholder URLs (`https://your-vector-db.com/...`) with your real endpoints. Create a `docs` collection first.\n3. Ingest: POST `{\"doc_id\": \"handbook-1\", \"doc_text\": \"...\"}` to the *Ingest Webhook Trigger* URL — or replace it with a Manual Trigger for one-off pastes.\n4. Activate, then open the chat from the *Chat Trigger* node and ask a question.\n\n**Note:** the Search/Build nodes expect Qdrant-style results (`results[].payload.text`). Adjust the *Build Context* expression if your provider returns a different shape.","height":480,"width":500},"position":[160,-80],"type":"n8n-nodes-base.stickyNote","typeVersion":1},{"id":"3c4d5e6f-0001-4a5b-8c9d-111111111111","name":"Chat Trigger","parameters":{"options":{}},"position":[240,300],"type":"@n8n/n8n-nodes-langchain.chatTrigger","typeVersion":1.2,"webhookId":"cortexflow-rag-chat"},{"credentials":{"httpHeaderAuth":{"id":"","name":"OpenAI Bearer"}},"id":"3c4d5e6f-0002-4a5b-8c9d-222222222222","name":"Embed Question","parameters":{"authentication":"predefinedCredentialType","jsonBody":"={\n  \"model\": \"text-embedding-3-small\",\n  \"input\": \"{{ $('Chat Trigger').item.json.chatInput }}\"\n}","method":"POST","nodeCredentialType":"httpHeaderAuth","options":{},"sendBody":true,"specifyBody":"json","url":"https://api.openai.com/v1/embeddings"},"position":[480,300],"type":"n8n-nodes-base.httpRequest","typeVersion":4.2},{"credentials":{"httpHeaderAuth":{"id":"","name":"Vector DB API Key"}},"id":"3c4d5e6f-0003-4a5b-8c9d-333333333333","name":"Search Vector DB","parameters":{"authentication":"predefinedCredentialType","jsonBody":"={\n  \"vector\": {{ $json.data[0].embedding }},\n  \"limit\": 5,\n  \"with_payload\": true\n}","method":"POST","nodeCredentialType":"httpHeaderAuth","options":{},"sendBody":true,"specifyBody":"json","url":"https://your-vector-db.com/collections/docs/search"},"position":[720,300],"type":"n8n-nodes-base.httpRequest","typeVersion":4.2},{"id":"3c4d5e6f-0004-4a5b-8c9d-444444444444","name":"Build Context","parameters":{"assignments":{"assignments":[{"id":"3c4d5e6f-a1","name":"question","type":"string","value":"={{ $('Chat Trigger').item.json.chatInput }}"},{"id":"3c4d5e6f-a2","name":"context","type":"string","value":"={{ ($json.results || $json.points || []).map(function(r, i) { var t = (r.payload && r.payload.text) || r.text || JSON.stringify(r); return 'Chunk ' + (i + 1) + ':\\n' + t; }).join('\\n\\n') }}"}]},"options":{}},"position":[960,300],"type":"n8n-nodes-base.set","typeVersion":3.4},{"id":"3c4d5e6f-0005-4a5b-8c9d-555555555555","name":"Answer from Docs","parameters":{"options":{"systemMessage":"You are a company knowledge-base assistant. Answer the user's question using ONLY the provided knowledge base context. If the answer is not in the context, say you do not have that information and offer to connect them with a human. Never invent facts. When you use a chunk, mention which chunk number it came from."},"promptType":"define","text":"=User question:\n{{ $json.question }}\n\nKnowledge base context:\n{{ $json.context }}"},"position":[1200,300],"type":"@n8n/n8n-nodes-langchain.agent","typeVersion":2.2},{"credentials":{"openAiApi":{"id":"","name":"OpenAI API"}},"id":"3c4d5e6f-0006-4a5b-8c9d-666666666666","name":"OpenAI Chat Model","parameters":{"model":{"__rl":true,"cachedResultName":"gpt-4o-mini","mode":"list","value":"gpt-4o-mini"},"options":{}},"position":[1200,500],"type":"@n8n/n8n-nodes-langchain.lmChatOpenAi","typeVersion":1.2},{"id":"3c4d5e6f-0010-4a5b-8c9d-a0a0a0a0a0a0","name":"Ingest Webhook Trigger","parameters":{"httpMethod":"POST","options":{},"path":"ingest-doc"},"position":[240,700],"type":"n8n-nodes-base.webhook","typeVersion":2,"webhookId":"cortexflow-ingest-doc"},{"id":"3c4d5e6f-0011-4a5b-8c9d-b1b1b1b1b1b1","name":"Prepare Document","parameters":{"assignments":{"assignments":[{"id":"3c4d5e6f-b1","name":"doc_id","type":"string","value":"={{ ($json.doc_id || 'doc-' + $now.toMillis()).toString() }}"},{"id":"3c4d5e6f-b2","name":"doc_text","type":"string","value":"={{ ($json.doc_text || '').toString().trim() }}"}]},"options":{}},"position":[480,700],"type":"n8n-nodes-base.set","typeVersion":3.4},{"credentials":{"httpHeaderAuth":{"id":"","name":"OpenAI Bearer"}},"id":"3c4d5e6f-0012-4a5b-8c9d-c2c2c2c2c2c2","name":"Embed Document","parameters":{"authentication":"predefinedCredentialType","jsonBody":"={\n  \"model\": \"text-embedding-3-small\",\n  \"input\": \"{{ $json.doc_text }}\"\n}","method":"POST","nodeCredentialType":"httpHeaderAuth","options":{},"sendBody":true,"specifyBody":"json","url":"https://api.openai.com/v1/embeddings"},"position":[720,700],"type":"n8n-nodes-base.httpRequest","typeVersion":4.2},{"credentials":{"httpHeaderAuth":{"id":"","name":"Vector DB API Key"}},"id":"3c4d5e6f-0013-4a5b-8c9d-d3d3d3d3d3d3","name":"Upsert to Vector DB","parameters":{"authentication":"predefinedCredentialType","jsonBody":"={\n  \"points\": [{\n    \"id\": \"{{ $('Prepare Document').item.json.doc_id }}\",\n    \"vector\": {{ $json.data[0].embedding }},\n    \"payload\": { \"text\": \"{{ $('Prepare Document').item.json.doc_text }}\" }\n  }]\n}","method":"POST","nodeCredentialType":"httpHeaderAuth","options":{},"sendBody":true,"specifyBody":"json","url":"https://your-vector-db.com/collections/docs/points"},"position":[960,700],"type":"n8n-nodes-base.httpRequest","typeVersion":4.2}],"pinData":{},"settings":{"executionOrder":"v1"},"tags":[{"name":"cortexflow-template"}],"versionId":"cortexflow-rag-chatbot-company-documents-v1"}