health_indicator.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """
  2. 健康指标分析 Agent
  3. """
  4. import json
  5. from typing import Dict, Any, List
  6. from agents.base import BaseAgent
  7. class HealthIndicatorAgent(BaseAgent):
  8. def __init__(self, task_id=None, llm=None):
  9. super().__init__(name="HealthIndicatorAgent", task_id=task_id, llm=llm)
  10. async def run(self, input_data: Dict[str, Any]) -> Dict[str, Any]:
  11. await self.validate_input(input_data)
  12. self.set_state("running")
  13. report_text = input_data["report_text"]
  14. prompt = f"""
  15. 你是一名专业的健康分析助手。
  16. 请从以下体检或健康报告中提取关键健康指标,并判断风险。
  17. 报告内容:
  18. {report_text}
  19. 请返回 JSON,严格遵循以下格式:
  20. {{
  21. "indicator_results": {{
  22. "<指标名>": {{
  23. "value": "<原始数值或描述>",
  24. "status": "<normal | borderline | high | low | abnormal>",
  25. "risk_level": "<low | medium | high>",
  26. "analysis": "<简要分析该指标的健康含义>"
  27. }}
  28. }}
  29. }}
  30. 要求:
  31. - 每个指标单独分析,不要给出综合结论
  32. - 不要给出任何健康建议
  33. - 如果报告中未提及明确数值,可用描述性判断
  34. """
  35. response = await self.think(prompt)
  36. indicators: List[Dict[str, Any]] = []
  37. try:
  38. result = json.loads(response)
  39. indicator_dict = result.get("indicator_results", {})
  40. for name, data in indicator_dict.items():
  41. indicators.append({
  42. "name": name,
  43. "value": data.get("value"),
  44. "status": data.get("status"),
  45. "risk_level": data.get("risk_level"),
  46. "analysis": data.get("analysis")
  47. })
  48. except json.JSONDecodeError:
  49. # LLM 输出异常保护
  50. indicators = []
  51. self.set_state("completed")
  52. return {
  53. "indicators": indicators
  54. }
  55. def get_required_fields(self) -> List[str]:
  56. return ["report_text"]