{
  "name": "Analyze Student Emotion",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "analyze-emotion",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ],
      "id": "webhook-node",
      "name": "Webhook"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://ollama:11434/api/generate",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"gemma2:2b\",\n  \"prompt\": \"Eres un psic\u00f3logo educativo experto en an\u00e1lisis emocional de estudiantes.\\n\\nAnaliza la siguiente respuesta de un estudiante peruano y determina su estado emocional.\\n\\nRespuesta del estudiante:\\n={{ $json.body.text }}\\n\\nCalificaci\u00f3n obtenida: ={{ $json.body.grade }}/100\\n\\nResponde SOLAMENTE con este formato:\\nEMOCI\u00d3N: [POSITIVO/NEUTRAL/NEGATIVO]\\nENGAGEMENT: [0.0 a 1.0]\\nRAZ\u00d3N: [breve explicaci\u00f3n en una l\u00ednea]\",\n  \"stream\": false,\n  \"options\": {\n    \"num_predict\": 200,\n    \"temperature\": 0.3\n  }\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        208,
        0
      ],
      "id": "http-request-node",
      "name": "HTTP Request"
    },
    {
      "parameters": {
        "jsCode": "// Obtener respuesta de Ollama\nconst ollamaResponse = $input.first().json;\nconst content = ollamaResponse.response;\n\n// Parsear emoci\u00f3n\nlet emotion = 'NEUTRAL';\nconst emotionMatch = content.match(/EMOCI\u00d3N:\\s*(POSITIVO|NEUTRAL|NEGATIVO)/i);\nif (emotionMatch) {\n  emotion = emotionMatch[1].toUpperCase();\n}\n\n// Parsear engagement\nlet engagement = 0.5;\nconst engagementMatch = content.match(/ENGAGEMENT:\\s*([\\d.]+)/i);\nif (engagementMatch) {\n  engagement = Number.parseFloat(engagementMatch[1]);\n  // Asegurar que est\u00e1 entre 0 y 1\n  engagement = Math.max(0, Math.min(1, engagement));\n}\n\n// Extraer raz\u00f3n\nlet reason = '';\nconst reasonMatch = content.match(/RAZ\u00d3N:\\s*(.+)/i);\nif (reasonMatch) {\n  reason = reasonMatch[1].trim();\n}\n\n// Retornar respuesta estructurada\nreturn {\n  json: {\n    emotion: emotion,\n    engagement: engagement,\n    reason: reason,\n    timestamp: new Date().toISOString()\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        416,
        0
      ],
      "id": "code-node",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "position": [
        624,
        0
      ],
      "id": "respond-node",
      "name": "Respond to Webhook"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  }
}