From 2e0f9145c64ebb8e6bd720c0fa5b3c4faf9cd528 Mon Sep 17 00:00:00 2001 From: iomgaa Date: Mon, 11 Aug 2025 16:06:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=B7=BB=E5=8A=A0Pr?= =?UTF-8?q?ompter=E6=99=BA=E8=83=BD=E4=BD=93=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现智能体提示词生成专家功能 - 基于患者病史和具体任务生成定制化子智能体 - 包含专业角色定义和可操作询问指令生成 - 添加完整的测试用例验证功能 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- agent_system/prompter/__init__.py | 5 + agent_system/prompter/agent.py | 165 ++++++++++++++++++++++++ agent_system/prompter/prompt.py | 80 ++++++++++++ agent_system/prompter/response_model.py | 18 +++ test_prompter.py | 49 +++++++ 5 files changed, 317 insertions(+) create mode 100644 agent_system/prompter/__init__.py create mode 100644 agent_system/prompter/agent.py create mode 100644 agent_system/prompter/prompt.py create mode 100644 agent_system/prompter/response_model.py create mode 100644 test_prompter.py diff --git a/agent_system/prompter/__init__.py b/agent_system/prompter/__init__.py new file mode 100644 index 0000000..1a89adb --- /dev/null +++ b/agent_system/prompter/__init__.py @@ -0,0 +1,5 @@ +from .agent import Prompter +from .response_model import PrompterResult +from .prompt import PrompterPrompt + +__all__ = ['Prompter', 'PrompterResult', 'PrompterPrompt'] \ No newline at end of file diff --git a/agent_system/prompter/agent.py b/agent_system/prompter/agent.py new file mode 100644 index 0000000..fcd7ed2 --- /dev/null +++ b/agent_system/prompter/agent.py @@ -0,0 +1,165 @@ +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 \ No newline at end of file diff --git a/agent_system/prompter/prompt.py b/agent_system/prompter/prompt.py new file mode 100644 index 0000000..8d90b05 --- /dev/null +++ b/agent_system/prompter/prompt.py @@ -0,0 +1,80 @@ +from agent_system.base import BasePrompt + + +class PrompterPrompt(BasePrompt): + """ + Prompter智能体的提示词模板 + + 定义了Prompter智能体的角色、任务目标和执行指令, + 用于根据患者病史和当前任务生成专门的子智能体提示内容。 + """ + + # 智能体角色和目标描述 + description = ( + "你是一名专业的智能体提示词生成专家,擅长基于医疗场景和具体任务需求," + "为特定的询问任务创建专门的智能体描述和指令。" + "你的主要任务是根据患者的现病史、既往史、主述以及当前具体任务," + "生成一个针对该任务的专门子智能体的description和instructions," + "该子智能体将负责围绕特定主题向患者进行专业的医疗询问。" + ) + + # 执行指令和注意事项 + instructions = [ + "## 核心生成任务", + "1. **任务理解**: 深入理解当前任务的具体要求和询问重点(如起病情况、患病时间、症状特征等)", + "2. **背景整合**: 结合患者的现病史、既往史和主述,理解患者的整体病情背景", + "3. **角色定位**: 为目标子智能体定义清晰的专业角色和询问职责", + "4. **指令设计**: 制定具体、可操作的询问指令,确保能够有效收集目标信息", + "", + "## 子智能体设计原则", + "- **专业性**: 基于医学专业知识,确保询问的科学性和准确性", + "- **针对性**: 紧密围绕当前任务主题,避免偏离核心询问目标", + "- **个性化**: 结合患者的具体病史背景,提供个性化的询问策略", + "- **系统性**: 确保询问内容全面、有条理,不遗漏重要信息", + "", + "## 输出内容要求", + "1. **description字段**: 清晰描述子智能体的角色、专业领域和主要职责", + "2. **instructions字段**: 详细的执行指令列表,包括询问步骤、注意事项和质量要求", + "3. **医学准确性**: 确保所有医学术语和概念的准确性", + "4. **可操作性**: 指令必须具体明确,便于子智能体执行", + "", + "## 示例输出格式(JSON)", + "{", + " \"description\": \"你是一名专业的起病情况询问医师,专门负责详细了解患者疾病的起病过程和时间特征。基于患者的头痛主述和相关病史,你需要系统性地收集起病相关的关键信息,为后续诊断提供重要依据。\",", + " \"instructions\": [", + " \"## 起病时间询问\",", + " \"1. 询问患者头痛症状的具体开始时间(年月日或具体时间点)\",", + " \"2. 了解从开始到现在的总病程持续时间\",", + " \"3. 确认是否为首次出现此类症状\",", + " \"\",", + " \"## 起病方式询问\",", + " \"1. 详细了解症状是突然出现还是逐渐加重\",", + " \"2. 询问起病时的具体情况和环境背景\",", + " \"3. 了解是否有明确的诱发因素或触发事件\",", + " \"\",", + " \"## 询问注意事项\",", + " \"- 使用通俗易懂的语言,避免过多医学术语\",", + " \"- 耐心引导患者回忆具体细节\",", + " \"- 确保信息的准确性和完整性\"", + " ]", + "}" + ] + + @staticmethod + def get_example_output() -> str: + """ + 获取示例输出格式,用于指导 LLM 生成符合要求的结构化输出 + + Returns: + str: JSON 格式的示例输出 + """ + return """{ + "description": "为特定任务定制的子智能体描述,说明角色、任务和目标", + "instructions": [ + "## 询问重点", + "具体的询问步骤和要点", + "", + "## 注意事项", + "执行过程中的注意事项和要求" + ] +}""" \ No newline at end of file diff --git a/agent_system/prompter/response_model.py b/agent_system/prompter/response_model.py new file mode 100644 index 0000000..b8713ff --- /dev/null +++ b/agent_system/prompter/response_model.py @@ -0,0 +1,18 @@ +from typing import List +from pydantic import Field +from agent_system.base import BaseResponseModel + +class PrompterResult(BaseResponseModel): + """ + Prompter智能体分析结果模型 + + 用于输出为特定任务定制的子智能体的描述和指令内容 + """ + description: str = Field( + ..., + description="为特定任务定制的子智能体描述,说明该智能体的角色、任务和目标" + ) + instructions: List[str] = Field( + default_factory=list, + description="为特定任务定制的子智能体执行指令列表,包含具体的执行步骤和注意事项" + ) \ No newline at end of file diff --git a/test_prompter.py b/test_prompter.py new file mode 100644 index 0000000..6ad5826 --- /dev/null +++ b/test_prompter.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +""" +测试Prompter智能体功能 +""" + +from agent_system.prompter import Prompter + +def test_prompter(): + """测试Prompter智能体的基本功能""" + + # 创建Prompter实例 + prompter = Prompter() + + # 测试数据 + hpi_content = "患者反复头痛3个月,以前额和颞部为主,呈搏动性疼痛,每周发作2-3次,每次持续4-6小时" + ph_content = "既往体健,无高血压、糖尿病等慢性疾病史,无头部外伤史" + chief_complaint = "反复头痛3个月" + current_task = "起病情况和患病时间" + + print("=== 测试Prompter智能体 ===") + print(f"患者主述: {chief_complaint}") + print(f"现病史: {hpi_content}") + print(f"既往史: {ph_content}") + print(f"当前任务: {current_task}") + print() + + try: + # 执行Prompter分析 + result = prompter.run( + hpi_content=hpi_content, + ph_content=ph_content, + chief_complaint=chief_complaint, + current_task=current_task + ) + + print("=== Prompter分析结果 ===") + print(f"子智能体描述:") + print(f"{result.description}") + print() + print(f"子智能体指令:") + for i, instruction in enumerate(result.instructions, 1): + print(f"{i}. {instruction}") + print() + + except Exception as e: + print(f"测试失败: {e}") + +if __name__ == "__main__": + test_prompter() \ No newline at end of file