feat(ai): Day 3 — GetHealthInsightsTool + 配额前置检查 + Token 预算限制
- 新增 GetHealthInsightsTool:聚合档案摘要+化验异常+体征异常,输出 InsightCard - 注册到 Patient/MedicalStaff 沙箱(10 个 Tool 全部就位) - chat_handler 添加 QuotaService 配额前置检查(月度 Token/患者日限额) - AgentRunParams 新增 token_budget 字段,Orchestrator 每轮累计检查超预算强制结束
This commit is contained in:
@@ -12,6 +12,8 @@ pub struct AgentRunParams {
|
||||
pub temperature: f32,
|
||||
pub max_tokens: u32,
|
||||
pub max_iterations: usize,
|
||||
/// 可选:累计 Token 预算(input + output),超出后强制结束
|
||||
pub token_budget: Option<u32>,
|
||||
}
|
||||
|
||||
impl Default for AgentRunParams {
|
||||
@@ -21,6 +23,7 @@ impl Default for AgentRunParams {
|
||||
temperature: 0.7,
|
||||
max_tokens: 2048,
|
||||
max_iterations: 5,
|
||||
token_budget: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,6 +112,27 @@ impl AgentOrchestrator {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Token 预算检查:超出后强制结束
|
||||
if let Some(budget) = params.token_budget {
|
||||
let total = total_input_tokens + total_output_tokens;
|
||||
if total >= budget {
|
||||
tracing::warn!(
|
||||
total_tokens = total,
|
||||
budget = budget,
|
||||
iterations = iterations,
|
||||
"Token budget exhausted, forcing final reply"
|
||||
);
|
||||
messages.push(ChatMessage {
|
||||
role: ChatMessageRole::User,
|
||||
content: "(系统提示:Token 预算已用尽,请立即基于已有信息总结回复用户,不要再调用工具)"
|
||||
.to_string(),
|
||||
tool_calls: None,
|
||||
tool_call_id: None,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// 将 assistant 的 tool_calls 加入消息历史
|
||||
messages.push(ChatMessage {
|
||||
role: ChatMessageRole::Assistant,
|
||||
|
||||
Reference in New Issue
Block a user