iomgaa a9144062bf 新增:添加Controller智能体模块并优化Prompter智能体
- 新增Controller智能体模块,负责任务选择和指导建议生成
  - 实现任务信息模型和决策响应模型
  - 支持基于患者病史的智能任务选择
  - 提供针对选定任务的专业指导建议

- 优化Prompter智能体,支持Controller指导建议整合
  - 更新run函数支持specific_guidance参数
  - 添加系统化的4步子智能体生成流程
  - 增强prompt指令,提升生成质量和专业性
  - 保持向后兼容性

- 完善测试验证,确保功能正常运行

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-11 18:17:23 +08:00

194 lines
7.8 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 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