Files
AIclinicalresearch/docs/03-业务模块/AIA-AI智能问答/02-技术设计/Protocol_Agent_Backend_Config_Design.md
HaHafeng 96290d2f76 feat(aia): Implement Protocol Agent MVP with reusable Agent framework
Sprint 1-3 Completed (Backend + Frontend):

Backend (Sprint 1-2):
- Implement 5-layer Agent framework (Query->Planner->Executor->Tools->Reflection)
- Create agent_schema with 6 tables (agent_definitions, stages, prompts, sessions, traces, reflexion_rules)
- Create protocol_schema with 2 tables (protocol_contexts, protocol_generations)
- Implement Protocol Agent core services (Orchestrator, ContextService, PromptBuilder)
- Integrate LLM service adapter (DeepSeek/Qwen/GPT-5/Claude)
- 6 API endpoints with full authentication
- 10/10 API tests passed

Frontend (Sprint 3):
- Add Protocol Agent entry in AgentHub (indigo theme card)
- Implement ProtocolAgentPage with 3-column layout
- Collapsible sidebar (Gemini style, 48px <-> 280px)
- StatePanel with 5 stage cards (scientific_question, pico, study_design, sample_size, endpoints)
- ChatArea with sync button and action cards integration
- 100% prototype design restoration (608 lines CSS)
- Detailed endpoints structure: baseline, exposure, outcomes, confounders

Features:
- 5-stage dialogue flow for research protocol design
- Conversation-driven interaction with sync-to-protocol button
- Real-time context state management
- One-click protocol generation button (UI ready, backend pending)

Database:
- agent_schema: 6 tables for reusable Agent framework
- protocol_schema: 2 tables for Protocol Agent
- Seed data: 1 agent + 5 stages + 9 prompts + 4 reflexion rules

Code Stats:
- Backend: 13 files, 4338 lines
- Frontend: 14 files, 2071 lines
- Total: 27 files, 6409 lines

Status: MVP core functionality completed, pending frontend-backend integration testing

Next: Sprint 4 - One-click protocol generation + Word export
2026-01-24 17:29:24 +08:00

5.6 KiB
Raw Blame History

Protocol Agent 后端配置架构设计 (V3.0 核心思路)

设计目标: 将硬编码的业务逻辑转化为可运营的配置数据。
核心范式: 全局继承 + 阶段重写 + 组件化逻辑

1. 配置层级概览 (The Configuration Hierarchy)

我们不再把 Prompt 看作一个长字符串,而是一个 “动态组装对象”

graph TD
Global[全局配置 (Global Config)] --> Stage[阶段配置 (Stage Config)]

subgraph "Level 1: 全局设定"  
Global \--\> BasePrompt\[人设: 资深医学专家\]  
Global \--\> ContextDef\[记忆结构: PICO/N值\]  
Global \--\> GlobalTools\[通用工具: EKB检索\]  
end  
  
subgraph "Level 2: 阶段设定 (覆盖 7 个模块)"  
Stage \--\> TaskPrompt\[任务指令: 算样本量\]  
Stage \--\> CoT\[思维链 SOP\]  
Stage \--\> Reflexion\[反思卫士规则\]  
Stage \--\> StageTools\[专用工具: 计算器\]  
end

2. 详细配置清单 (后端都有哪些需要配置的?)

2.1 Level 1: 全局配置 (Global Configuration)

这是 Agent 的“底色”,在所有阶段都会生效。

配置项 说明 示例值
Agent Name 智能体名称 研究方案制定助手 (Protocol Agent)
Base System Prompt 总体 Prompt "你是一个严谨的临床研究方法学专家。你的核心目标是辅助用户产出高质量的 Protocol。你必须依据循证医学证据说话..."
Tone of Voice 语气风格 专业、客观、循循善诱(不要过于热情,保持学术严谨性)
Global Knowledge 全局知识库挂载 关联 ekb_schema (自建知识库),允许检索所有 Guideline 类型文档。
Memory Structure 记忆结构定义 定义 ProtocolContext 的 JSON Schema告诉 AI 它能记住哪些字段,如 PICO

2.2 Level 2: 阶段配置 (Stage Configuration)

这是 7 个具体模块的配置区。用户处于哪个阶段Orchestrator 就加载哪份配置覆盖进 System Prompt。

配置项 说明 示例值 (以样本量计算为例)
Stage ID 阶段标识 SAMPLE_SIZE_CALC
Stage Trigger 进入条件 (Router) 当用户 PICO 完整 且 研究类型为 RCT 时。
Stage Instruction 阶段任务指令 "当前任务是计算样本量。请根据用户提供的 Alpha/Power 参数,推荐合适的计算公式。"
Specific Tools 专用工具绑定 绑定 tools/st/sample-size (Action Card)。
Output Schema 结构化提取目标 定义需要从对话中提取哪些字段存入 DB如 n_value, alpha

2.3 Level 3: 逻辑与风控配置 (Logic & Reflexion)

这是您特别关心的 “思维链”“反思卫士”

A. 思维链 (CoT) 配置

我们不再把思考过程写在 Prompt 文本里,而是配置为 SOP 步骤列表

  • 配置方式:一个有序数组。
  • 配置内容
    1. Step 1 [Check]: 检查必要参数Alpha, Power是否齐全。
    2. Step 2 [Match]: 匹配研究设计(是两样本均数比较,还是率的比较?)。
    3. Step 3 [Recall]: 回忆/检索类似研究的 Effect Size 作为参考。
    4. Step 4 [Action]: 决定是追问用户,还是生成 Action Card。

B. 反思卫士 (Reflexion Guard) 配置

这是 V3 的核心升级。我们需要配置 “什么样的情况是错误的”

可以分为两类配置:

  1. Prompt-based Reflexion (软校验):
    • 配置一段 Prompt 让 AI 自查:“请检查用户计算出的 N 值是否过小(<20如果是请给出伦理预警。”
  2. Rule-based Reflexion (硬校验 - 高级):
    • 配置 JSON 规则(后端解析执行):
      {
      "rules": [
      { "field": "sample_size.n", "operator": "<", "value": 10, "msg": "样本量过小" },
      { "field": "sample_size.power", "operator": "<", "value": 0.8, "msg": "效能不足" }
      ]
      }

3. 系统运行时的组装逻辑

当用户发来一条消息时,后端 Orchestrator 是这样工作的:

  1. 加载全局:取 Global.BasePrompt。
  2. 定位阶段:根据当前 Context.stage (比如样本量),取 Stage.TaskPrompt。
  3. 注入 SOP:取 Stage.CoT编译成 XML 格式的思考指令。
    • Step 1: <check>...</check>
    • Step 2: <match>...</match>
  4. 挂载反思:取 Stage.Reflexion如果是 Prompt 类型的反思,追加到最后。
  5. 最终合成
    [Global Persona] ...
    [Context Data] ...
    [Stage Instruction] ...
    [CoT Steps (SOP)] ...
    [Reflexion Instructions] ...

4. 总结:设计建议

您现在的思路非常清晰:

  1. 全局控制 (Overall Prompt) 是必须的,它是 Agent 的“灵魂”保证了无论在哪个阶段AI 说话的方式都是一致的。
  2. 分阶段配置 是“肉体”,负责具体的干活技能。
  3. 思维链 & 反思 是“神经系统”,负责控制思考的深度和质量。

下一步建议:
我将在下一个回复中,为您设计 "后端配置中心" 的 UI 原型。
这个原型将不再关注调试对话框,而是关注 "如何像搭积木一样配置上述的三层结构"。
它应该包含:

  • 全局设置页:配置 Agent 人设。
  • 流程编排页一个左侧导航7个阶段右侧配置Prompt + CoT + Reflexion的布局。
  • SOP 编辑器:专门用来拖拽/编辑思维链步骤的区域。