{
  "name": "PDF Q&A System with Pinecone RAG",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "pdf-qa",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "f6a7b8c9-6666-4000-8000-000000000001",
      "name": "Question Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "set-query",
              "name": "query",
              "value": "={{ $json.body.question }}",
              "type": "string"
            },
            {
              "id": "set-top-k",
              "name": "topK",
              "value": "={{ $json.body.topK || 5 }}",
              "type": "number"
            }
          ]
        },
        "options": {}
      },
      "id": "f6a7b8c9-6666-4000-8000-000000000002",
      "name": "Parse Query",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "resource": "embedding",
        "operation": "create",
        "model": "text-embedding-3-small",
        "input": "={{ $json.query }}"
      },
      "id": "f6a7b8c9-6666-4000-8000-000000000003",
      "name": "Create Query Embedding",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.6,
      "position": [
        680,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://your-index-xxxxxxx.svc.pinecone.io/query",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ vector: $json.data[0].embedding, topK: $('Parse Query').item.json.topK, includeMetadata: true, namespace: 'pdf-documents' }) }}",
        "options": {}
      },
      "id": "f6a7b8c9-6666-4000-8000-000000000004",
      "name": "Pinecone Vector Search",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        300
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "set-context",
              "name": "context",
              "value": "={{ $json.matches.map(m => m.metadata.text).join('\\n\\n---\\n\\n') }}",
              "type": "string"
            },
            {
              "id": "set-sources",
              "name": "sources",
              "value": "={{ $json.matches.map(m => ({ source: m.metadata.source, page: m.metadata.page, score: m.score })) }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "f6a7b8c9-6666-4000-8000-000000000005",
      "name": "Build Context",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "message",
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are a precise Q&A assistant that answers questions based ONLY on the provided context from PDF documents. If the context doesn't contain enough information to answer, say so. Always cite which document and page number your answer comes from. Be concise and accurate."
            },
            {
              "role": "user",
              "content": "=Context from PDF documents:\n{{ $json.context }}\n\n---\n\nQuestion: {{ $('Parse Query').item.json.query }}"
            }
          ]
        },
        "options": {
          "temperature": 0.2,
          "maxTokens": 1000
        }
      },
      "id": "f6a7b8c9-6666-4000-8000-000000000006",
      "name": "OpenAI Answer",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.6,
      "position": [
        1340,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ answer: $json.message.content, sources: JSON.parse($('Build Context').item.json.sources), query: $('Parse Query').item.json.query }) }}",
        "options": {
          "responseCode": 200
        }
      },
      "id": "f6a7b8c9-6666-4000-8000-000000000007",
      "name": "Respond with Answer",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1560,
        300
      ]
    }
  ],
  "connections": {
    "Question Webhook": {
      "main": [
        [
          {
            "node": "Parse Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Query": {
      "main": [
        [
          {
            "node": "Create Query Embedding",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Query Embedding": {
      "main": [
        [
          {
            "node": "Pinecone Vector Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pinecone Vector Search": {
      "main": [
        [
          {
            "node": "Build Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Context": {
      "main": [
        [
          {
            "node": "OpenAI Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Answer": {
      "main": [
        [
          {
            "node": "Respond with Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null
}