{
  "name": "Document Q&A RAG Pipeline",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ask",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a7b8c9d0-7777-4000-8000-000000000001",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "message",
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "Convert the following user question into an optimized search query for a vector database. Return ONLY the search query string, no explanation. Focus on key concepts and remove filler words."
            },
            {
              "role": "user",
              "content": "={{ $json.body.question }}"
            }
          ]
        },
        "options": {
          "temperature": 0.1,
          "maxTokens": 128
        }
      },
      "id": "a7b8c9d0-7777-4000-8000-000000000002",
      "name": "Optimize Query",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.6,
      "position": [
        500,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/embeddings",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ input: $json.text, model: 'text-embedding-3-small' }) }}",
        "options": {}
      },
      "id": "a7b8c9d0-7777-4000-8000-000000000003",
      "name": "Generate Embedding",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        750,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://your-index-name-abc123.svc.pinecone.io/query",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ vector: $json.data[0].embedding, topK: 5, includeMetadata: true, namespace: 'documents' }) }}",
        "options": {
          "timeout": 10000
        }
      },
      "id": "a7b8c9d0-7777-4000-8000-000000000004",
      "name": "Pinecone Vector Search",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1000,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "message",
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are a knowledgeable document assistant. Answer the user's question using ONLY the provided context from our document database. If the context doesn't contain enough information to answer, say so clearly. Always cite which document(s) you're referencing.\n\nRules:\n1. Only use information from the provided context\n2. If uncertain, indicate your confidence level\n3. Quote relevant passages when helpful\n4. If the question cannot be answered from the context, say: \"I don't have enough information in the available documents to answer this question.\""
            },
            {
              "role": "user",
              "content": "=Question: {{ $('Webhook').item.json.body.question }}\n\nRelevant Document Context:\n{{ $json.matches ? $json.matches.map((m, i) => `[Doc ${i+1}] (score: ${m.score.toFixed(3)}) ${m.metadata.source || 'Unknown'}:\\n${m.metadata.text}`).join('\\n\\n') : 'No matching documents found.' }}"
            }
          ]
        },
        "options": {
          "temperature": 0.3,
          "maxTokens": 1024
        }
      },
      "id": "a7b8c9d0-7777-4000-8000-000000000005",
      "name": "OpenAI Answer",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.6,
      "position": [
        1250,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {
          "responseCode": 200
        },
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ answer: $json.text, question: $('Webhook').item.json.body.question, sources: ($('Pinecone Vector Search').item.json.matches || []).map(m => ({ source: m.metadata.source, score: m.score })) }) }}"
      },
      "id": "a7b8c9d0-7777-4000-8000-000000000006",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1500,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Optimize Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Optimize Query": {
      "main": [
        [
          {
            "node": "Generate Embedding",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Embedding": {
      "main": [
        [
          {
            "node": "Pinecone Vector Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pinecone Vector Search": {
      "main": [
        [
          {
            "node": "OpenAI Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Answer": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null
}