195 lines
8.0 KiB
Python
Raw Normal View History

from typing import Any
from agent_system.base import BaseAgent
from agent_system.prompter.prompt import PrompterPrompt
from agent_system.prompter.response_model import PrompterResult
class Prompter(BaseAgent):
"""
预问诊询问智能体生成专家
基于患者的现病史既往史主述以及当前具体任务
生成针对该任务的专门询问子智能体的description和instructions
该子智能体将负责围绕特定主题向患者进行专业的预问诊询问
核心功能:
1. 理解当前任务的具体要求和询问重点
2. 整合患者的病史背景信息
3. 为目标询问子智能体定义清晰的专业角色
4. 制定仅限于可询问内容的具体指令
Attributes:
model_type (str): 使用的大语言模型类型默认为 gpt-oss:latest
llm_config (dict): LLM模型配置参数
"""
def __init__(self, model_type: str = "gpt-oss:latest", llm_config: dict = None):
"""
初始化Prompter智能体
Args:
model_type (str): 大语言模型类型默认使用 gpt-oss:latest
llm_config (dict): LLM模型的配置参数如果为None则使用默认配置
"""
super().__init__(
model_type=model_type,
description="基于患者情况和任务需求生成专门的预问诊询问智能体指导",
instructions=PrompterPrompt.instructions,
response_model=PrompterResult,
llm_config=llm_config or {},
structured_outputs=True,
markdown=False,
use_cache=False
)
def run(self, hpi_content: str, ph_content: str, chief_complaint: str, current_task: str, specific_guidance: str = "") -> PrompterResult:
"""
执行预问诊询问智能体生成
基于患者的现病史既往史主述当前具体任务以及Controller提供的询问指导建议
生成针对该任务的专门询问子智能体的description和instructions
Args:
hpi_content (str): 现病史内容患者的主要症状描述
ph_content (str): 既往史内容患者的历史疾病信息
chief_complaint (str): 患者主述患者的主要不适描述
current_task (str): 当前任务"起病情况和患病时间""主要症状特征"
specific_guidance (str): Controller提供的针对当前任务的询问指导建议用于优化询问子智能体生成
Returns:
PrompterResult: 包含询问子智能体描述和指令的结构化数据包括
- description: 为特定询问任务定制的子智能体描述
- instructions: 为特定询问任务定制的子智能体执行指令列表
Raises:
Exception: 当LLM调用失败时返回包含默认信息的PrompterResult
"""
try:
# 构建生成提示词
prompt = self._build_prompt(hpi_content, ph_content, chief_complaint, current_task, specific_guidance)
# 调用基类的run方法执行LLM推理
result = super().run(prompt)
# 确保返回正确的类型并进行类型转换
return self._ensure_result_type(result)
except Exception as e:
# 当生成失败时记录错误并返回默认结果
print(f"预问诊询问子智能体生成失败: {str(e)}")
return self._get_fallback_result(current_task)
def _ensure_result_type(self, result: Any) -> PrompterResult:
"""
确保返回结果为正确的类型
Args:
result (Any): LLM返回的原始结果
Returns:
PrompterResult: 转换后的结构化结果
"""
if isinstance(result, PrompterResult):
return result
elif isinstance(result, dict):
return PrompterResult(**result)
else:
# 如果类型不匹配,返回默认结果
return self._get_fallback_result("未知任务")
def _get_fallback_result(self, task_name: str) -> PrompterResult:
"""
生成失败时的默认结果
Args:
task_name (str): 任务名称
Returns:
PrompterResult: 包含默认内容的结果
"""
return PrompterResult(
description=f"你是一名专业的预问诊询问医生,负责针对'{task_name}'进行详细的询问信息收集。你需要以专业、耐心的态度与患者交流,通过询问获取准确完整的相关信息。",
instructions=[
"## 询问重点",
f"1. 围绕'{task_name}'主题进行系统性询问",
"2. 使用通俗易懂的语言与患者交流",
"3. 确保询问信息的准确性和完整性",
"",
"## 注意事项",
"- 保持专业和耐心的态度",
"- 避免使用过于复杂的医学术语",
"- 仅通过询问获取信息,不进行任何检查或检验",
"- 引导患者提供具体详细的信息"
]
)
def _build_prompt(self, hpi_content: str, ph_content: str, chief_complaint: str, current_task: str, specific_guidance: str = "") -> str:
"""
构建Prompter的提示词模板
根据患者病史信息当前任务和Controller的具体指导建议构建用于生成子智能体的提示词
Args:
hpi_content (str): 现病史内容
ph_content (str): 既往史内容
chief_complaint (str): 患者主述
current_task (str): 当前任务
specific_guidance (str): Controller提供的具体指导建议
Returns:
str: 构建的提示词
"""
# 确保既往史内容的合理显示
past_history_display = ph_content.strip() if ph_content.strip() else "暂无既往史信息"
# 处理具体指导建议
guidance_section = ""
if specific_guidance.strip():
guidance_section = f"""
Controller指导建议: {specific_guidance}
"""
# 从prompt类获取示例输出格式
from agent_system.prompter.prompt import PrompterPrompt
example_output = PrompterPrompt.get_example_output()
prompt = f"""患者基本信息:
患者主述: {chief_complaint}
现病史: {hpi_content}
既往史: {past_history_display}
当前任务: {current_task}{guidance_section}
请按照以下步骤生成一个专门的预问诊询问子智能体该智能体将负责围绕"{current_task}"主题向患者进行专业询问
## 步骤1: 分析任务特点
- 深入理解"{current_task}"的核心要求和关键询问点
- 结合患者的现病史和主述识别与该任务相关的重要信息
- 如果有Controller指导建议重点考虑其中的专业建议和注意事项
## 步骤2: 设计智能体角色
- 为子智能体定义专业的医疗角色和身份
- 明确该智能体在"{current_task}"方面的专业能力和职责范围
- 确保角色设计与患者的具体病情背景相匹配
## 步骤3: 制定询问策略
- 基于任务特点和患者信息设计系统性的询问流程
- 将复杂的医疗询问分解为患者易于理解和回答的具体问题
- 确保询问内容全面有序针对性强
## 步骤4: 完善执行指令
- 详细说明子智能体应如何执行询问任务
- 包含具体的询问技巧注意事项和质量要求
- 确保指令具有可操作性和实用性
请为该子智能体提供
1. description - 描述该智能体的角色专业领域和主要职责
2. instructions - 详细的执行指令列表包括询问步骤注意事项等
输出格式示例
{example_output}
请严格按照上述JSON格式输出
输出内容为:"""
return prompt