- 实现智能体提示词生成专家功能 - 基于患者病史和具体任务生成定制化子智能体 - 包含专业角色定义和可操作询问指令生成 - 添加完整的测试用例验证功能 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
165 lines
6.4 KiB
Python
165 lines
6.4 KiB
Python
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) -> PrompterResult:
|
||
"""
|
||
执行智能体提示词生成
|
||
|
||
基于患者的现病史、既往史、主述以及当前具体任务,
|
||
生成针对该任务的专门子智能体的description和instructions。
|
||
|
||
Args:
|
||
hpi_content (str): 现病史内容,患者的主要症状描述
|
||
ph_content (str): 既往史内容,患者的历史疾病信息
|
||
chief_complaint (str): 患者主述,患者的主要不适描述
|
||
current_task (str): 当前任务,如"起病情况和患病时间"、"主要症状特征"等
|
||
|
||
Returns:
|
||
PrompterResult: 包含子智能体描述和指令的结构化数据,包括:
|
||
- description: 为特定任务定制的子智能体描述
|
||
- instructions: 为特定任务定制的子智能体执行指令列表
|
||
|
||
Raises:
|
||
Exception: 当LLM调用失败时,返回包含默认信息的PrompterResult
|
||
"""
|
||
try:
|
||
# 构建生成提示词
|
||
prompt = self._build_prompt(hpi_content, ph_content, chief_complaint, current_task)
|
||
|
||
# 调用基类的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) -> str:
|
||
"""
|
||
构建Prompter的提示词模板
|
||
|
||
根据患者病史信息和当前任务,构建用于生成子智能体的提示词。
|
||
|
||
Args:
|
||
hpi_content (str): 现病史内容
|
||
ph_content (str): 既往史内容
|
||
chief_complaint (str): 患者主述
|
||
current_task (str): 当前任务
|
||
|
||
Returns:
|
||
str: 构建的提示词
|
||
"""
|
||
# 确保既往史内容的合理显示
|
||
past_history_display = ph_content.strip() if ph_content.strip() else "暂无既往史信息"
|
||
|
||
# 从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}
|
||
|
||
请基于上述患者信息和当前任务,生成一个专门的子智能体,该智能体将负责围绕"{current_task}"主题向患者进行专业询问。
|
||
|
||
请为该子智能体提供:
|
||
1. description - 描述该智能体的角色、专业领域和主要职责
|
||
2. instructions - 详细的执行指令列表,包括询问步骤、注意事项等
|
||
|
||
输出格式示例:
|
||
{example_output}
|
||
|
||
请严格按照上述JSON格式输出。
|
||
输出内容为:"""
|
||
|
||
return prompt |