iomgaa 399e4d4447 新增:实现医疗问诊工作流系统并优化Monitor智能体
- 新增完整的workflow模块,包含MedicalWorkflow、TaskManager、StepExecutor和WorkflowLogger四个核心组件
- 实现分诊、现病史、既往史三阶段任务管理和状态跟踪机制
- 优化Monitor智能体支持针对特定任务的精准评估,解决任务评价针对性问题
- 完善agent_system各模块的__init__.py文件,确保正确的模块导入
- 实现详细的jsonl格式日志记录系统,支持完整workflow执行追踪

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 20:40:33 +08:00

159 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Optional, List, Dict
from agent_system.base import BaseAgent
from agent_system.monitor.prompt import MonitorPrompt
from agent_system.monitor.response_model import MonitorResult
class Monitor(BaseAgent):
"""
Monitor智能体
监控和评估现病史、既往史和主诉的质量,提供完成度评分
"""
def __init__(self, model_type: str = "gpt-oss:latest", llm_config: dict = {}):
super().__init__(
model_type=model_type,
description=MonitorPrompt.description,
instructions=MonitorPrompt.instructions,
response_model=MonitorResult,
llm_config=llm_config,
structured_outputs=True,
markdown=False,
use_cache=False
)
def run(self, hpi_content: str, ph_content: str, chief_complaint: str,
task_name: str = None, task_description: str = None) -> MonitorResult:
"""
监控病史质量
Args:
hpi_content: 现病史内容
ph_content: 既往史内容
chief_complaint: 主诉
task_name: 任务名称(可选,用于针对性评估)
task_description: 任务描述(可选,用于针对性评估)
Returns:
MonitorResult: 包含完成度评分和评分理由
"""
# 根据是否提供任务信息选择不同的构建方式
if task_name and task_description:
prompt = self._build_task_specific_prompt(task_name, task_description,
hpi_content, ph_content, chief_complaint)
else:
prompt = self.build_prompt(hpi_content, ph_content, chief_complaint)
# 调用LLM进行评估
result = super().run(prompt)
# 确保返回正确的类型
if isinstance(result, MonitorResult):
return result
elif isinstance(result, dict):
return MonitorResult(**result)
else:
# 解析失败,返回默认结果
return MonitorResult(
completion_score=0.0,
reason="监控评估失败无法解析LLM响应"
)
def build_prompt(self, hpi_content: str, ph_content: str, chief_complaint: str) -> str:
"""
构建监控评估的提示语
Args:
hpi_content: 现病史内容
ph_content: 既往史内容
chief_complaint: 主诉
Returns:
str: 构建好的提示语
"""
prompt = f"""请对以下病史信息进行质量监控和评估:
**主诉**
{chief_complaint}
**现病史**
{hpi_content}
**既往史**
{ph_content}
**评估要求**
1. 综合评估现病史、既往史和主诉的信息完整性
2. 考虑信息之间的逻辑一致性和相互关联性
3. 基于医学标准评估信息的临床价值
4. **必须先给出详细的评分理由再基于理由给出0.0-1.0范围内的完成度评分**
5. 评分必须与理由保持逻辑一致
**输出格式**
严格按照以下JSON格式输出
{{
"completion_score": 浮点数0.0-1.0
"reason": "详细的评分理由"
}}
**评分指导**
- 0.9-1.0: 信息非常完整,逻辑清晰,临床价值高
- 0.8-0.9: 信息较完整,有少量缺失,整体质量良好
- 0.7-0.8: 信息基本完整,存在一些不足
- 0.6-0.7: 信息不够完整,有明显缺失
- 0.5-0.6: 信息缺失较多,质量有待提高
- 0.0-0.5: 信息严重不足,需要大幅改善
请基于上述标准进行客观评估。"""
return prompt
def _build_task_specific_prompt(self, task_name: str, task_description: str,
hpi_content: str, ph_content: str, chief_complaint: str) -> str:
"""
构建针对特定任务的评估提示语
Args:
task_name: 任务名称
task_description: 任务描述
hpi_content: 现病史内容
ph_content: 既往史内容
chief_complaint: 主诉
Returns:
str: 构建好的任务特定评估提示语
"""
prompt = f"""请针对特定任务对病史信息进行质量监控和评估:
**评估目标任务**
任务名称:{task_name}
任务描述:{task_description}
**当前病史信息**
主诉:{chief_complaint}
现病史:{hpi_content}
既往史:{ph_content}
**评估要求**
1. **专门针对任务"{task_name}"进行评估**
2. 根据任务描述"{task_description}",判断当前病史信息在这个方面的完整性
3. 重点关注与该任务相关的信息是否充分收集
4. 给出该任务的完成度评分0.0-1.0范围)
5. 详细说明评分理由,解释该任务还缺少哪些关键信息
**评分标准**(针对该任务):
- 0.9-1.0: 该任务相关信息非常完整,无需补充
- 0.8-0.9: 该任务相关信息较完整,仅有少量细节缺失
- 0.7-0.8: 该任务相关信息基本齐全,有一些重要细节待补充
- 0.6-0.7: 该任务相关信息不够完整,缺少多项关键信息
- 0.5-0.6: 该任务相关信息缺失较多,需要大量补充
- 0.0-0.5: 该任务相关信息严重不足或完全缺失
**输出格式**
严格按照以下JSON格式输出
{{
"completion_score": 浮点数0.0-1.0
"reason": "针对任务'{task_name}'的详细评分理由,说明该任务完成情况和缺失信息"
}}
请基于上述要求进行针对性评估。"""
return prompt