{
  "name": "Chatbot con Memoria",
  "nodes": [
    {
      "parameters": {
        "path": "/chat",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "get",
        "url": "http://qdrant:6333/collections/chat_history/points/search",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api-key",
              "value": "={{$env.N8N_QDRANT_API_KEY}}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "vector": "={{$node.Webhook.json.question}}",
          "limit": 10,
          "with_payload": true
        },
        "options": {}
      },
      "name": "Buscar Historial",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const historial = $input.all()[0].json.result || [];\nlet contexto = 'Historial de la conversaci\u00f3n:\\n';\nhistorial.forEach(m => { contexto += `${m.payload?.role || 'usuario'}: ${m.payload?.content || ''}\\n`; });\ncontexto += `\\nPregunta actual: ${$input.all()[1].json.question}`;\nreturn { contexto };\n"
      },
      "name": "Preparar Contexto",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://ollama:11434/api/generate",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "gemma4:latest"
            },
            {
              "name": "prompt",
              "value": "={{ $node['Preparar Contexto'].json.contexto }}"
            },
            {
              "name": "stream",
              "value": false
            }
          ]
        },
        "options": {}
      },
      "name": "Generar Respuesta",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "http://qdrant:6333/collections/chat_history/points",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api-key",
              "value": "={{$env.N8N_QDRANT_API_KEY}}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "points": [
            {
              "id": "={{ $now + '-user' }}",
              "payload": {
                "sesion_id": "={{$node.Webhook.json.session_id}}",
                "rol": "usuario",
                "contenido": "={{$node.Webhook.json.question}}"
              }
            },
            {
              "id": "={{ $now + '-assistant' }}",
              "payload": {
                "sesion_id": "={{$node.Webhook.json.session_id}}",
                "rol": "asistente",
                "contenido": "={{$node['Generar Respuesta'].json.response}}"
              }
            }
          ]
        },
        "options": {}
      },
      "name": "Guardar Historial",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": {
          "respuesta": "={{$node['Generar Respuesta'].json.response}}",
          "sesion_id": "={{$node.Webhook.json.session_id}}"
        }
      },
      "name": "Responder",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1250,
        300
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Buscar Historial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Buscar Historial": {
      "main": [
        [
          {
            "node": "Preparar Contexto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Preparar Contexto": {
      "main": [
        [
          {
            "node": "Generar Respuesta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generar Respuesta": {
      "main": [
        [
          {
            "node": "Guardar Historial",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Guardar Historial": {
      "main": [
        [
          {
            "node": "Responder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "saveDataErrorExecution": "all",
    "saveDataSuccessExecution": "all",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "tags": [
    "ia",
    "chatbot",
    "memoria"
  ],
  "triggerCount": 1
}