{
  "name": "Question Answering Workflow",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ask",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Extract query from request\nconst query = $input.body.query;\n\nif (!query) {\n  throw new Error('Query is required');\n}\n\nreturn { query };"
      },
      "name": "Extract Query",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$env.PYTHON_SERVICE_URL}}/generate-embedding",
        "method": "POST",
        "sendBody": true,
        "bodyParametersUi": {
          "parameter": [
            {
              "name": "text",
              "value": "={{ $node[\"Extract Query\"].json.query }}"
            }
          ]
        },
        "options": {}
      },
      "name": "Generate Query Embedding",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=SELECT content, metadata, 1 - (embedding <=> '{{$node[\"Generate Query Embedding\"].json.embedding}}') as similarity\nFROM document_vectors\nWHERE 1 - (embedding <=> '{{$node[\"Generate Query Embedding\"].json.embedding}}') > 0.7\nORDER BY similarity DESC\nLIMIT 5;"
      },
      "name": "Similarity Search",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 1,
      "position": [
        850,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "// Prepare for Gemini\nconst query = $input.json.query;\nconst relevantDocs = $node[\"Similarity Search\"].json;\n\n// Create context from relevant documents\nconst context = relevantDocs.map(doc => doc.content).join('\\n\\n');\n\n// Format prompt for Gemini\nconst prompt = `\nYou are a helpful assistant that answers questions based on the following context.\nIf the answer cannot be found in the context, say \"I don't have enough information to answer that question.\"\nDo not make up or hallucinate information not in the context.\n\nCONTEXT:\n${context}\n\nQUESTION:\n${query}\n\nANSWER:\n`;\n\nreturn { prompt, relevantDocs };"
      },
      "name": "Prepare Prompt",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://generativelanguage.googleapis.com/v1/models/gemini-2.0-flash:generateContent",
        "options": {
          "params": {
            "key": "={{$env.GEMINI_API_KEY}}"
          }
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "contents",
              "value": "={{ [{\"parts\":[{\"text\":$node[\"Prepare Prompt\"].json.prompt}]}] }}"
            }
          ]
        }
      },
      "name": "Ask Gemini",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "// Extract the answer from Gemini response\nconst geminiResponse = $input.json;\nconst relevantDocs = $node[\"Prepare Prompt\"].json.relevantDocs;\n\nlet answer = \"Sorry, I couldn't generate an answer.\";\n\nif (geminiResponse && geminiResponse.candidates && geminiResponse.candidates[0] && \n    geminiResponse.candidates[0].content && geminiResponse.candidates[0].content.parts) {\n  answer = geminiResponse.candidates[0].content.parts[0].text;\n}\n\nreturn {\n  answer,\n  sources: relevantDocs.map(doc => ({\n    content: doc.content.substring(0, 200) + '...',\n    metadata: doc.metadata\n  }))\n};"
      },
      "name": "Format Response",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1450,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "=$node[\"Format Response\"].json"
      },
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1650,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Extract Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Query": {
      "main": [
        [
          {
            "node": "Generate Query Embedding",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Query Embedding": {
      "main": [
        [
          {
            "node": "Similarity Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Similarity Search": {
      "main": [
        [
          {
            "node": "Prepare Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Prompt": {
      "main": [
        [
          {
            "node": "Ask Gemini",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ask Gemini": {
      "main": [
        [
          {
            "node": "Format Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Response": {
      "main": [
        [
          {
            "node": "Respond",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}