8
n8n 中文网amn8n.com

AI-GPT-4o-mini驱动的预测性健康监测与警报系统

高级

这是一个Engineering, AI Summarization领域的自动化工作流,包含 26 个节点。主要使用 If, Code, Redis, Slack, Twilio 等节点。 使用GPT-4o-mini的预测性健康监测与警报系统

前置要求
  • Redis 服务器连接信息
  • Slack Bot Token 或 Webhook URL
  • MongoDB 连接字符串
  • HTTP Webhook 端点(n8n 会自动生成)
  • PostgreSQL 数据库连接信息
  • 可能需要目标 API 的认证凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "EmWzr179H1gbzeK6",
  "meta": {
    "instanceId": "b91e510ebae4127f953fd2f5f8d40d58ca1e71c746d4500c12ae86aad04c1502",
    "templateCredsSetupCompleted": true
  },
  "name": "AI-GPT-4o-mini 驱动的预测性健康监测与警报系统",
  "tags": [],
  "nodes": [
    {
      "id": "0c2ded10-be6c-49be-b328-57581a01abf4",
      "name": "Webhook - 健康数据输入",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -272,
        1216
      ],
      "webhookId": "eab1c005-6c8a-4c4e-afc1-6d8c1e320fa5",
      "parameters": {
        "path": "health-data",
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "5d24fe84-e6a7-40bf-a749-6752838cb715",
      "name": "标准化健康数据",
      "type": "n8n-nodes-base.code",
      "position": [
        -80,
        1216
      ],
      "parameters": {
        "mode": "raw"
      },
      "typeVersion": 2
    },
    {
      "id": "a60b0cae-2d85-4c93-8bdc-b819a44d9f77",
      "name": "存储到数据库",
      "type": "n8n-nodes-base.postgres",
      "position": [
        144,
        1024
      ],
      "parameters": {
        "table": "health_records",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "c3d5aa03-7d91-416c-9720-d0ca8c7504cf",
      "name": "获取近期历史记录",
      "type": "n8n-nodes-base.postgres",
      "position": [
        144,
        1408
      ],
      "parameters": {
        "query": "SELECT * FROM health_records ORDER BY timestamp DESC LIMIT 30",
        "options": {},
        "operation": "executeQuery"
      },
      "typeVersion": 2
    },
    {
      "id": "a90be109-549c-4cd8-a668-d0354a5c7022",
      "name": "分析健康指标",
      "type": "n8n-nodes-base.code",
      "position": [
        368,
        1408
      ],
      "parameters": {
        "jsCode": "const currentData = $input.first().json;\nconst historicalData = $input.all().map(item => item.json);\n\n// Calculate averages from last 7 days\nconst last7Days = historicalData.slice(0, 7);\nconst avgHeartRate = last7Days.reduce((sum, d) => sum + parseFloat(d.heart_rate || 0), 0) / last7Days.length;\nconst avgBPSystolic = last7Days.reduce((sum, d) => sum + parseFloat(d.bp_systolic || 0), 0) / last7Days.length;\nconst avgBPDiastolic = last7Days.reduce((sum, d) => sum + parseFloat(d.bp_diastolic || 0), 0) / last7Days.length;\nconst avgSleep = last7Days.reduce((sum, d) => sum + parseFloat(d.sleep_hours || 0), 0) / last7Days.length;\n\n// Health analysis flags\nconst alerts = [];\nlet riskLevel = 'normal';\n\n// Heart rate analysis\nif (currentData.heart_rate > 100 || currentData.heart_rate < 60) {\n  alerts.push(`Heart rate abnormal: ${currentData.heart_rate} bpm`);\n  riskLevel = 'warning';\n}\n\n// Blood pressure analysis\nif (currentData.bp_systolic > 140 || currentData.bp_diastolic > 90) {\n  alerts.push(`High blood pressure: ${currentData.bp_systolic}/${currentData.bp_diastolic}`);\n  riskLevel = 'critical';\n}\nif (currentData.bp_systolic < 90 || currentData.bp_diastolic < 60) {\n  alerts.push(`Low blood pressure: ${currentData.bp_systolic}/${currentData.bp_diastolic}`);\n  riskLevel = 'warning';\n}\n\n// Temperature analysis\nif (currentData.temperature > 38.0) {\n  alerts.push(`Elevated temperature: ${currentData.temperature}°C`);\n  riskLevel = 'critical';\n}\n\n// Sleep analysis\nif (avgSleep < 6) {\n  alerts.push(`Insufficient sleep average: ${avgSleep.toFixed(1)} hours`);\n  if (riskLevel === 'normal') riskLevel = 'warning';\n}\n\n// Symptoms check\nif (currentData.symptoms && currentData.symptoms !== 'none') {\n  alerts.push(`Reported symptoms: ${currentData.symptoms}`);\n  if (riskLevel === 'normal') riskLevel = 'warning';\n}\n\nconst needsDoctorVisit = riskLevel === 'critical' || alerts.length >= 3;\n\nreturn {\n  currentData,\n  analysis: {\n    alerts,\n    riskLevel,\n    needsDoctorVisit,\n    averages: {\n      heartRate: avgHeartRate.toFixed(1),\n      bloodPressure: `${avgBPSystolic.toFixed(0)}/${avgBPDiastolic.toFixed(0)}`,\n      sleep: avgSleep.toFixed(1)\n    },\n    trend: {\n      heartRateChange: ((currentData.heart_rate - avgHeartRate) / avgHeartRate * 100).toFixed(1),\n      sleepChange: ((currentData.sleep_hours - avgSleep) / avgSleep * 100).toFixed(1)\n    }\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "06be0728-a5de-429e-80c4-162f63e9f050",
      "name": "检查是否需要医生就诊",
      "type": "n8n-nodes-base.if",
      "position": [
        1024,
        1216
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "doctor-alert-condition",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              },
              "leftValue": "={{ $json.analysis.needsDoctorVisit }}",
              "rightValue": true
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "0055cd07-1fe3-4482-bd1c-49845434ffa7",
      "name": "生成健康报告",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1232,
        944
      ],
      "parameters": {
        "text": "=You are an advanced medical health advisor AI with access to comprehensive patient data. Generate a detailed, personalized health report.\n\n**PATIENT PROFILE:**\n- Name: {{$json.currentData.patient_name}}\n- Patient ID: {{$json.currentData.patient_id}}\n- Data Confidence: {{$json.currentData.data_confidence}}\n\n**CURRENT VITAL SIGNS:**\n- Health Score: {{$json.healthScore.score}}/100 (Grade: {{$json.healthScore.grade}})\n- Heart Rate: {{$json.currentData.heart_rate}} bpm\n- Blood Pressure: {{$json.currentData.bp_systolic}}/{{$json.currentData.bp_diastolic}} mmHg\n- Temperature: {{$json.currentData.temperature}}°C\n- Blood Oxygen: {{$json.currentData.blood_oxygen}}%\n- Weight: {{$json.currentData.weight}} kg\n\n**LIFESTYLE METRICS:**\n- Sleep: {{$json.currentData.sleep_hours}} hours\n- Steps: {{$json.currentData.steps_count}}\n- Exercise: {{$json.currentData.exercise_minutes}} minutes\n- Hydration: {{$json.currentData.hydration_ml}} ml\n- Stress Level: {{$json.currentData.stress_level}}/10\n- Mood Rating: {{$json.currentData.mood_rating}}/10\n- Medication Adherence: {{$json.currentData.medication_taken ? 'Yes' : 'No'}}\n\n**7-DAY AVERAGES:**\n- Heart Rate: {{$json.analysis.averages.heartRate}} bpm\n- Blood Pressure: {{$json.analysis.averages.bloodPressure}} mmHg\n- Sleep: {{$json.analysis.averages.sleep}} hours\n\n**WEEKLY COMPARISON:**\n{{$json.weeklyComparison.available ? `- Heart Rate Trend: ${$json.weeklyComparison.heartRateChange}%\n- Sleep Trend: ${$json.weeklyComparison.sleepChange}%\n- Overall Trajectory: ${$json.weeklyComparison.trend}\n- Key Insights: ${$json.weeklyComparison.insights.join('; ')}` : 'Baseline week - no comparison data available'}}\n\n**HEALTH SCORE BREAKDOWN:**\n- Cardiovascular: {{$json.healthScore.breakdown.cardiovascular}}/100\n- Rest Quality: {{$json.healthScore.breakdown.rest}}/100\n- Activity Level: {{$json.healthScore.breakdown.activity}}/100\n- Vital Signs: {{$json.healthScore.breakdown.vitals}}/100\n\n**ACTIVE ALERTS ({{$json.analysis.alerts.length}}):**\n{{$json.analysis.alerts.join('\\n')}}\n\n**RISK ASSESSMENT:**\n- Current Risk Level: {{$json.analysis.riskLevel}}\n- Doctor Visit Needed: {{$json.analysis.needsDoctorVisit ? 'YES - URGENT' : 'NO'}}\n- Recommendation: {{$json.healthScore.recommendation}}\n\n**SYMPTOMS REPORTED:**\n{{$json.currentData.symptoms}}\n\n**VALIDATION NOTES:**\n{{$json.currentData.validation_warnings.length > 0 ? $json.currentData.validation_warnings.join('; ') : 'All readings within expected ranges'}}\n\n---\n\n**REQUIRED REPORT SECTIONS:**\n1. **Executive Health Summary** (2-3 sentences)\n2. **Detailed Metrics Analysis** (explain each concerning reading)\n3. **Week-over-Week Progress Assessment**\n4. **Lifestyle Recommendations** (sleep, exercise, nutrition, stress management)\n5. **Medical Action Plan** (urgency level, specific next steps)\n6. **Preventive Health Strategies** (long-term wellness goals)\n7. **Risk Mitigation Plan** (if applicable)\n8. **Mental Health Considerations** (based on stress/mood data)\n9. **Medication Compliance Assessment**\n10. **Follow-up Timeline** (when to reassess)\n\nProvide evidence-based, compassionate, and actionable guidance. Use clear medical terminology with explanations. Prioritize patient safety and wellbeing.",
        "options": {
          "systemMessage": "You are a health analytics AI assistant specialized in interpreting vital signs and health metrics. Provide evidence-based recommendations while emphasizing the importance of professional medical consultation for serious concerns."
        },
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "ef5be657-a1d3-40c4-b54d-19bae5a8509a",
      "name": "OpenAI GPT-4",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1232,
        1184
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "OGYj7DgYv5GFLFZk",
          "name": "OpenAi account 2"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "0426db19-e07c-4bcc-b48d-905edf59e74f",
      "name": "发送健康报告邮件",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        1504,
        1072
      ],
      "webhookId": "d3c68bf3-5cfb-4e08-95da-104fe3d2721b",
      "parameters": {
        "text": "=📊 *COMPREHENSIVE HEALTH REPORT*\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n*Patient:* {{$('Webhook - Health Data Input').item.json.patient_name}}\n*Report Date:* {{DateTime.now().toFormat('MMMM dd, yyyy - HH:mm')}}\n*Health Score:* {{$('Calculate Health Score').item.json.healthScore.score}}/100 ({{$('Calculate Health Score').item.json.healthScore.grade}})\n*Risk Level:* {{$('Calculate Health Score').item.json.analysis.riskLevel.toUpperCase()}}\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n*AI-GENERATED HEALTH ANALYSIS*\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n{{$('Generate Health Report').item.json.output}}\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n*PREDICTIVE HEALTH INSIGHTS*\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n{{$('AI Predictive Health Agent').item.json.output}}\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n*QUICK REFERENCE - CURRENT VITALS*\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n💓 Heart Rate: {{$('Webhook - Health Data Input').item.json.heart_rate}} bpm\n🩺 Blood Pressure: {{$('Webhook - Health Data Input').item.json.bp_systolic}}/{{$('Webhook - Health Data Input').item.json.bp_diastolic}} mmHg\n🌡️ Temperature: {{$('Webhook - Health Data Input').item.json.temperature}}°C\n😴 Sleep: {{$('Webhook - Health Data Input').item.json.sleep_hours}} hours\n🚶 Steps: {{$('Webhook - Health Data Input').item.json.steps_count}}\n💧 Hydration: {{$('Webhook - Health Data Input').item.json.hydration_ml}} ml\n🏋️ Exercise: {{$('Webhook - Health Data Input').item.json.exercise_minutes}} min\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n{{$('Calculate Health Score').item.json.analysis.needsDoctorVisit ? '⚠️ *IMPORTANT:* This report indicates you should schedule a doctor visit. Please take action promptly.' : '✅ Your health metrics are being monitored. Continue your current wellness routine.'}}\n\n*Disclaimer:* This is an automated health analysis tool. Always consult with qualified healthcare professionals for medical decisions.\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\nGenerated by AI Health Monitoring System",
        "options": {},
        "subject": "=🏥 Health Index Update - {{ $('Check If Doctor Visit Needed').item.json.analysis.needsDoctorVisit ? '⚠️ DOCTOR VISIT RECOMMENDED' : '✅ Status Normal' }}",
        "toEmail": "={{ $('Webhook - Health Data Input').item.json.email || 'your-email@example.com' }}",
        "fromEmail": "health-tracker@yourdomain.com"
      },
      "typeVersion": 2
    },
    {
      "id": "1122c278-79e4-40f5-81ca-f0ae59075067",
      "name": "发送紧急医生警报",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        1680,
        1072
      ],
      "webhookId": "e4f02644-3bdc-41b4-987a-b1d73df3fc3a",
      "parameters": {
        "text": "=🚨 *URGENT HEALTH ALERT - DOCTOR VISIT REQUIRED* 🚨\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n*Patient:* {{$('Webhook - Health Data Input').item.json.patient_name}}\n*Alert Time:* {{DateTime.now().toFormat('MMMM dd, yyyy - HH:mm')}}\n*Health Score:* {{$('Calculate Health Score').item.json.healthScore.score}}/100 ({{$('Calculate Health Score').item.json.healthScore.grade}})\n*Risk Level:* {{$('Calculate Health Score').item.json.analysis.riskLevel.toUpperCase()}}\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n*CRITICAL ALERTS DETECTED ({{$('Calculate Health Score').item.json.analysis.alerts.length}})*\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n{{$('Calculate Health Score').item.json.analysis.alerts.map((alert, i) => `${i + 1}. ${alert}`).join('\\n')}}\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n*IMMEDIATE ACTION REQUIRED*\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n✅ NEXT STEPS:\n1. Schedule doctor appointment within {{$('Calculate Health Score').item.json.healthScore.score < 50 ? '24 hours' : '48 hours'}}\n2. Monitor symptoms closely\n3. Keep this report for your physician\n4. If symptoms worsen, seek emergency care immediately\n\n⚕️ RECOMMENDATION:\n{{$('Calculate Health Score').item.json.healthScore.recommendation}}\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n*CURRENT VITAL SIGNS*\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n💓 Heart Rate: {{$('Webhook - Health Data Input').item.json.heart_rate}} bpm (7-day avg: {{$('Calculate Health Score').item.json.analysis.averages.heartRate}})\n🩺 Blood Pressure: {{$('Webhook - Health Data Input').item.json.bp_systolic}}/{{$('Webhook - Health Data Input').item.json.bp_diastolic}} mmHg (7-day avg: {{$('Calculate Health Score').item.json.analysis.averages.bloodPressure}})\n🌡️ Temperature: {{$('Webhook - Health Data Input').item.json.temperature}}°C\n🩸 Blood Oxygen: {{$('Webhook - Health Data Input').item.json.blood_oxygen}}%\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n⚠️ *DO NOT IGNORE THIS ALERT*\n\nYour health monitoring system has detected metrics requiring medical evaluation. This automated alert is designed to help you take timely action.\n\n📞 Emergency Contact: {{$('Webhook - Health Data Input').item.json.emergency_contact_phone || 'Not provided'}}\n\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n*Disclaimer:* This is an automated health monitoring alert. Always use your judgment and seek emergency services (911) if you feel your situation is life-threatening.\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━",
        "options": {},
        "subject": "🚨 URGENT: Doctor Visit Recommended - Health Alert",
        "toEmail": "={{ $('Webhook - Health Data Input').item.json.email || 'your-email@example.com' }}",
        "fromEmail": "health-alerts@yourdomain.com"
      },
      "typeVersion": 2
    },
    {
      "id": "f68c6a66-b9ec-495f-a518-8b6b03055e45",
      "name": "无需操作",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1376,
        1360
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "4a48c63d-7880-4b3e-8eac-6041aab8083b",
      "name": "紧急联系人短信警报",
      "type": "n8n-nodes-base.twilio",
      "position": [
        1024,
        1408
      ],
      "parameters": {
        "to": "={{$json.emergency_contact_phone}}",
        "from": "={{$env.TWILIO_PHONE_NUMBER}}",
        "message": "=🚨 URGENT HEALTH ALERT\n\nPatient: {{$('Webhook - Health Data Input').item.json.patient_name}}\nCritical metrics detected:\n{{$json.analysis.alerts.join('\\n')}}\n\nRisk Level: {{$json.analysis.riskLevel}}\nImmediate medical attention recommended.\n\nView full report: {{$json.reportUrl}}",
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "b68af135-b329-4f4c-9622-85313522c5b0",
      "name": "记录到 MongoDB 健康历史",
      "type": "n8n-nodes-base.mongoDb",
      "position": [
        368,
        1024
      ],
      "parameters": {
        "fields": "patient_id,timestamp,metrics,analysis,ai_report,action_taken",
        "options": {},
        "operation": "insert",
        "collection": "health_analytics"
      },
      "typeVersion": 1
    },
    {
      "id": "282eda5f-b9bb-4694-959c-e49129061e85",
      "name": "计算健康评分",
      "type": "n8n-nodes-base.code",
      "position": [
        512,
        1232
      ],
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst current = data.currentData;\nconst analysis = data.analysis;\n\n// Weighted health scoring algorithm (0-100)\nlet healthScore = 100;\n\n// Heart rate scoring (-20 to 0)\nconst hrDiff = Math.abs(current.heart_rate - 75);\nif (hrDiff > 25) healthScore -= 20;\nelse if (hrDiff > 15) healthScore -= 10;\nelse if (hrDiff > 5) healthScore -= 5;\n\n// Blood pressure scoring (-25 to 0)\nconst bpSystolic = parseFloat(current.bp_systolic);\nconst bpDiastolic = parseFloat(current.bp_diastolic);\nif (bpSystolic > 140 || bpDiastolic > 90) healthScore -= 25;\nelse if (bpSystolic > 130 || bpDiastolic > 85) healthScore -= 15;\nelse if (bpSystolic < 90 || bpDiastolic < 60) healthScore -= 20;\nelse if (bpSystolic > 120 || bpDiastolic > 80) healthScore -= 8;\n\n// Temperature scoring (-15 to 0)\nconst temp = parseFloat(current.temperature);\nif (temp > 38.5) healthScore -= 15;\nelse if (temp > 37.5) healthScore -= 8;\nelse if (temp < 36.0) healthScore -= 10;\n\n// Sleep scoring (-15 to 0)\nconst sleep = parseFloat(current.sleep_hours);\nif (sleep < 5) healthScore -= 15;\nelse if (sleep < 6) healthScore -= 10;\nelse if (sleep < 7) healthScore -= 5;\nelse if (sleep > 10) healthScore -= 5;\n\n// Activity scoring (-15 to 0)\nconst steps = parseInt(current.steps_count);\nif (steps < 3000) healthScore -= 15;\nelse if (steps < 5000) healthScore -= 10;\nelse if (steps < 7000) healthScore -= 5;\n\n// Symptoms scoring (-10 to 0)\nif (current.symptoms && current.symptoms !== 'none') {\n  const symptomSeverity = (current.symptoms.match(/severe|intense|extreme/i)) ? 10 : 5;\n  healthScore -= symptomSeverity;\n}\n\n// Ensure score stays within 0-100\nhealthScore = Math.max(0, Math.min(100, healthScore));\n\n// Determine health grade\nlet grade, recommendation;\nif (healthScore >= 90) {\n  grade = 'A+ Excellent';\n  recommendation = 'Maintain current healthy lifestyle';\n} else if (healthScore >= 80) {\n  grade = 'A Good';\n  recommendation = 'Minor improvements recommended';\n} else if (healthScore >= 70) {\n  grade = 'B Fair';\n  recommendation = 'Focus on sleep and exercise';\n} else if (healthScore >= 60) {\n  grade = 'C Concerning';\n  recommendation = 'Medical consultation advised within 1 week';\n} else if (healthScore >= 50) {\n  grade = 'D Poor';\n  recommendation = 'Schedule doctor appointment within 48 hours';\n} else {\n  grade = 'F Critical';\n  recommendation = 'URGENT: Seek immediate medical attention';\n}\n\nreturn {\n  ...data,\n  healthScore: {\n    score: healthScore,\n    grade: grade,\n    recommendation: recommendation,\n    breakdown: {\n      cardiovascular: Math.max(0, 100 - (hrDiff * 2) - ((bpSystolic > 140) ? 25 : 0)),\n      rest: Math.max(0, 100 - ((sleep < 7) ? (7 - sleep) * 10 : 0)),\n      activity: Math.max(0, 100 - ((steps < 7000) ? ((7000 - steps) / 70) : 0)),\n      vitals: Math.max(0, 100 - ((temp > 37.5) ? (temp - 37.5) * 20 : 0))\n    },\n    timestamp: new Date().toISOString()\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "4dfe63c1-f787-4aae-af1e-85171df75654",
      "name": "与上周比较",
      "type": "n8n-nodes-base.code",
      "position": [
        736,
        896
      ],
      "parameters": {
        "jsCode": "const currentData = $input.first().json;\nconst historicalData = $input.all().map(item => item.json);\n\n// Get data from 7 days ago (assuming daily records)\nconst previousWeek = historicalData.slice(7, 14);\nif (previousWeek.length === 0) {\n  return { ...currentData, weeklyComparison: { available: false } };\n}\n\n// Calculate previous week averages\nconst prevAvgHR = previousWeek.reduce((sum, d) => sum + parseFloat(d.heart_rate || 0), 0) / previousWeek.length;\nconst prevAvgBP = previousWeek.reduce((sum, d) => sum + parseFloat(d.bp_systolic || 0), 0) / previousWeek.length;\nconst prevAvgSleep = previousWeek.reduce((sum, d) => sum + parseFloat(d.sleep_hours || 0), 0) / previousWeek.length;\nconst prevAvgSteps = previousWeek.reduce((sum, d) => sum + parseFloat(d.steps_count || 0), 0) / previousWeek.length;\n\n// Current week averages (already in analysis)\nconst currAvgHR = parseFloat(currentData.analysis.averages.heartRate);\nconst currAvgSleep = parseFloat(currentData.analysis.averages.sleep);\n\n// Calculate week-over-week changes\nconst weeklyComparison = {\n  available: true,\n  heartRateChange: ((currAvgHR - prevAvgHR) / prevAvgHR * 100).toFixed(1),\n  sleepChange: ((currAvgSleep - prevAvgSleep) / prevAvgSleep * 100).toFixed(1),\n  trend: (currAvgHR < prevAvgHR && currAvgSleep > prevAvgSleep) ? 'improving' : 'needs attention',\n  insights: []\n};\n\nif (Math.abs(weeklyComparison.heartRateChange) > 10) {\n  weeklyComparison.insights.push(`Significant heart rate change: ${weeklyComparison.heartRateChange}%`);\n}\nif (Math.abs(weeklyComparison.sleepChange) > 15) {\n  weeklyComparison.insights.push(`Notable sleep pattern change: ${weeklyComparison.sleepChange}%`);\n}\n\nreturn { ...currentData, weeklyComparison };"
      },
      "typeVersion": 2
    },
    {
      "id": "717957d5-3bf5-4b29-b387-6380442db2ca",
      "name": "生成 PDF 报告",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        1008,
        560
      ],
      "parameters": {
        "url": "https://api.pdfmonkey.io/api/v1/documents",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "document[document_template_id]",
              "value": "health_report_template"
            },
            {
              "name": "document[payload][patient_name]",
              "value": "={{$('Webhook - Health Data Input').item.json.patient_name}}"
            },
            {
              "name": "document[payload][health_score]",
              "value": "={{$json.healthScore.score}}"
            },
            {
              "name": "document[payload][analysis]",
              "value": "={{JSON.stringify($json.analysis)}}"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{$env.PDFMONKEY_API_KEY}}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "3de482ad-942f-4d19-9077-a272a7092e72",
      "name": "检查关键阈值",
      "type": "n8n-nodes-base.if",
      "position": [
        752,
        1392
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "critical-score-check",
              "operator": {
                "type": "number",
                "operation": "smaller"
              },
              "leftValue": "={{$json.healthScore.score}}",
              "rightValue": 50
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "f6d440af-7ddf-4b73-85a1-8bc37d20348b",
      "name": "Slack 警报发送给护理团队",
      "type": "n8n-nodes-base.slack",
      "position": [
        1024,
        1600
      ],
      "webhookId": "1241303c-ce10-43ab-8017-3d4497729bda",
      "parameters": {
        "text": "=🚨 *CRITICAL HEALTH ALERT*\n\n*Patient:* {{$('Webhook - Health Data Input').item.json.patient_name}}\n*Health Score:* {{$json.healthScore.score}}/100 ({{$json.healthScore.grade}})\n*Risk Level:* {{$json.analysis.riskLevel}}\n\n*Critical Metrics:*\n{{$json.analysis.alerts.map(a => '• ' + a).join('\\n')}}\n\n*Recommendation:* {{$json.healthScore.recommendation}}\n\n*Action Required:* Immediate follow-up needed",
        "otherOptions": {}
      },
      "typeVersion": 2.2
    },
    {
      "id": "b3823d85-0472-42da-bc44-be434f4245d1",
      "name": "安排随访日历事件",
      "type": "n8n-nodes-base.googleCalendar",
      "position": [
        1024,
        1760
      ],
      "parameters": {
        "end": "={{DateTime.now().plus({days: 1, hours: 1}).toISO()}}",
        "start": "={{DateTime.now().plus({days: 1}).toISO()}}",
        "calendar": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "additionalFields": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "740ab316-db84-44eb-ac4e-0fb5b0f72bf2",
      "name": "OpenAI 健康趋势分析",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1040,
        976
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "OGYj7DgYv5GFLFZk",
          "name": "OpenAi account 2"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "de97eb7e-0a9c-4648-9d73-83edab294d5e",
      "name": "AI 预测性健康代理",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        960,
        752
      ],
      "parameters": {
        "text": "=You are an advanced predictive health analytics AI. Analyze the following comprehensive health data and provide:\n\n**Current Health Snapshot:**\n- Health Score: {{$json.healthScore.score}}/100 ({{$json.healthScore.grade}})\n- Heart Rate: {{$json.currentData.heart_rate}} bpm\n- Blood Pressure: {{$json.currentData.bp_systolic}}/{{$json.currentData.bp_diastolic}} mmHg\n- Temperature: {{$json.currentData.temperature}}°C\n- Sleep: {{$json.currentData.sleep_hours}} hours\n- Steps: {{$json.currentData.steps_count}}\n\n**Weekly Trends:**\n{{$json.weeklyComparison.available ? `- Heart Rate Change: ${$json.weeklyComparison.heartRateChange}%\n- Sleep Change: ${$json.weeklyComparison.sleepChange}%\n- Overall Trend: ${$json.weeklyComparison.trend}` : 'Insufficient historical data'}}\n\n**Current Alerts:**\n{{$json.analysis.alerts.join('\\n')}}\n\n**Task:**\n1. Provide a comprehensive health trajectory forecast for the next 30 days\n2. Identify potential health risks based on current trends\n3. Suggest specific lifestyle interventions with expected impact percentages\n4. Recommend optimal timing for preventive medical check-ups\n5. Provide personalized nutrition and exercise recommendations\n6. Assess stress indicators and mental health considerations\n\nBe data-driven, actionable, and include confidence levels for predictions.",
        "options": {
          "systemMessage": "You are a medical AI assistant specializing in predictive health analytics. Use evidence-based reasoning and provide confidence intervals for predictions. Always emphasize that your analysis supplements but does not replace professional medical advice."
        },
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "4d01f55c-c6ca-4ec7-ae13-311f6df60c6a",
      "name": "在 Redis 缓存中存储健康评分",
      "type": "n8n-nodes-base.redis",
      "position": [
        752,
        1568
      ],
      "parameters": {
        "key": "=health_score:{{$('Webhook - Health Data Input').item.json.patient_id || 'default'}}:latest",
        "ttl": 604800,
        "value": "={{JSON.stringify({score: $json.healthScore.score, grade: $json.healthScore.grade, timestamp: $json.healthScore.timestamp})}}",
        "expire": true,
        "operation": "set"
      },
      "typeVersion": 1
    },
    {
      "id": "7927ddba-2f68-4db9-b6bf-4c10fbc161cc",
      "name": "触发可穿戴设备同步",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        144,
        1216
      ],
      "parameters": {
        "url": "=https://api.fitbit.com/1/user/{{$('Webhook - Health Data Input').item.json.patient_id}}/activities/sync.json",
        "method": "POST",
        "options": {},
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "fitbitOAuth2Api"
      },
      "typeVersion": 4.2
    },
    {
      "id": "ea7ffeed-3d45-424a-8a0d-698b690428e8",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -240,
        496
      ],
      "parameters": {
        "width": 624,
        "height": 480,
        "content": "## 工作原理"
      },
      "typeVersion": 1
    },
    {
      "id": "9127a23c-4896-4aad-bff6-7f0cd66e8902",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1296,
        496
      ],
      "parameters": {
        "color": 3,
        "width": 432,
        "height": 416,
        "content": "## 先决条件"
      },
      "typeVersion": 1
    },
    {
      "id": "a42f6415-8235-4a23-89f4-7cab111ef510",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        416,
        496
      ],
      "parameters": {
        "color": 6,
        "width": 480,
        "height": 240,
        "content": "## 工作流步骤"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "a7553a5c-8ba9-4b5f-aab2-7a866946fb1f",
  "connections": {
    "OpenAI GPT-4": {
      "ai_languageModel": [
        [
          {
            "node": "Generate Health Report",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Store in Database": {
      "main": [
        [
          {
            "node": "Log to MongoDB Health History",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Recent History": {
      "main": [
        [
          {
            "node": "Analyze Health Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Health Data": {
      "main": [
        [
          {
            "node": "Store in Database",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Recent History",
            "type": "main",
            "index": 0
          },
          {
            "node": "Trigger Wearable Device Sync",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze Health Metrics": {
      "main": [
        [
          {
            "node": "Check If Doctor Visit Needed",
            "type": "main",
            "index": 0
          },
          {
            "node": "Calculate Health Score",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Health Score": {
      "main": [
        [
          {
            "node": "Compare with Previous Week",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check Critical Threshold",
            "type": "main",
            "index": 0
          },
          {
            "node": "Store Health Score in Redis Cache",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Health Report": {
      "main": [
        [
          {
            "node": "Send Health Report Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Critical Threshold": {
      "main": [
        [
          {
            "node": "Emergency Contact SMS Alert",
            "type": "main",
            "index": 0
          },
          {
            "node": "Slack Alert to Care Team",
            "type": "main",
            "index": 0
          },
          {
            "node": "Schedule Follow-up Calendar Event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Health Report Email": {
      "main": [
        [
          {
            "node": "Send Urgent Doctor Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compare with Previous Week": {
      "main": [
        [
          {
            "node": "Check If Doctor Visit Needed",
            "type": "main",
            "index": 0
          },
          {
            "node": "AI Predictive Health Agent",
            "type": "main",
            "index": 0
          },
          {
            "node": "Generate PDF Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - Health Data Input": {
      "main": [
        [
          {
            "node": "Normalize Health Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If Doctor Visit Needed": {
      "main": [
        [
          {
            "node": "Generate Health Report",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Action Needed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Health Trends Analysis": {
      "ai_languageModel": [
        [
          {
            "node": "AI Predictive Health Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 工程, AI 摘要总结

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量26
分类2
节点类型15
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

作者
Cheng Siong Chin

Cheng Siong Chin

@cschin

Dr. Cheng Siong CHIN serves as a Professor in Intelligent Systems Modelling and Simulation in Newcastle University, Singapore. His academic credentials include an M.Sc. in Advanced Control and Systems Engineering from The University of Manchester and a Ph.D. in Robotics from Nanyang Technological University.

外部链接
在 n8n.io 查看

分享此工作流