{
  "name": "Chatbot-Query-Qdrant",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "chatbot-rag",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=http://ollama:11434/api/embeddings",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "nomic-embed-text"
            },
            {
              "name": "prompt",
              "value": "={{ $json.body.pregunta || $json.body.question }}"
            }
          ]
        },
        "options": {}
      },
      "id": "query-embedding",
      "name": "Query Embedding",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://qdrant:6333/collections/reglamento_taekwondo/points/search",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"vector\": {{ JSON.stringify($json.embedding) }},\n  \"limit\": 3,\n  \"with_payload\": true\n}",
        "options": {}
      },
      "id": "qdrant-search",
      "name": "Qdrant Search",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "context-val",
              "name": "context",
              "value": "={{ $json.result.map(r => r.payload.text).join('\\n---\\n') }}",
              "type": "string"
            },
            {
              "id": "question-val",
              "name": "question",
              "value": "={{ $('Webhook').item.json.body.pregunta || $('Webhook').item.json.body.question }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "format-context",
      "name": "Format Context",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.2,
      "position": [
        950,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://ollama:11434/api/generate",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "smollm2:1.7b"
            },
            {
              "name": "prompt",
              "value": "=Eres un asistente experto en el reglamento de Taekwondo. Responde a la pregunta bas\u00e1ndote \u00fanicamente en el contexto proporcionado.\n\nContexto:\n{{ $json.context }}\n\nPregunta: {{ $json.question }}\n\nRespuesta corta y precisa:"
            },
            {
              "name": "stream",
              "value": "false"
            }
          ]
        },
        "options": {}
      },
      "id": "ollama-generate",
      "name": "Ollama Generate",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1200,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "=INSERT INTO consultas_rag (pregunta, respuesta, documentos_usados) VALUES ('{{ $('Webhook').item.json.body.pregunta || $('Webhook').item.json.body.question }}', '{{ $json.response }}', ARRAY['reglamento_taekwondo']);",
        "options": {}
      },
      "id": "postgres-save",
      "name": "PostgreSQL Save",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.2,
      "position": [
        1450,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "respond-to-webhook",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1700,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Query Embedding",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Query Embedding": {
      "main": [
        [
          {
            "node": "Qdrant Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Qdrant Search": {
      "main": [
        [
          {
            "node": "Format Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Context": {
      "main": [
        [
          {
            "node": "Ollama Generate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Generate": {
      "main": [
        [
          {
            "node": "PostgreSQL Save",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "PostgreSQL Save": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  }
}