后端: 1. 粗排后分流与顺序守卫落地,支持“无明确微调偏好时粗排后直接收口”,并新增 allow_reorder / needs_refine_after_rough_build 语义,打通 chat→rough_build→execute/order_guard→deliver 路由。 2. execute 工具执行链路修复:清理乱码坏块与重复分支;新增 min_context_switch 未授权拦截;补齐 suggested 顺序基线初始化与顺序守卫联动。 3. 新增复合写工具 min_context_switch(减少上下文切换)并接入注册、参数解析、写工具白名单、提示词与文档;仅在用户明确允许打乱顺序时可用。 4. 工具口径升级:find_first_free 支持 day/day_start/day_end 范围参数并统一文案;移除 find_free 兼容别名;读写工具输出统一到“第N天(星期X)”格式。 5. prompt 同步升级:chat/execute/execute_context 增加粗排后是否继续微调、顺序授权、min_context_switch 使用边界与返回示例约束。 6. handoff 文档重命名并重写下班交接重点:下一步聚焦“工具收敛能力研究 + 运行态必要参数重置(不丢运行态)”。 7. 同步更新调试日志文件。 前端:无 仓库:无
138 lines
6.4 KiB
Go
138 lines
6.4 KiB
Go
package newagentprompt
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
newagentmodel "github.com/LoveLosita/smartflow/backend/newAgent/model"
|
||
"github.com/cloudwego/eino/schema"
|
||
)
|
||
|
||
const chatRoutingSystemPrompt = `
|
||
你是 SmartFlow 的智能路由器。你的职责是判断用户意图的复杂度,并决定后续处理路径。
|
||
|
||
你会看到:
|
||
- 历史对话
|
||
- 用户本轮输入
|
||
- 当前可用工具摘要(如有)
|
||
- 本次排课涉及的任务类约束(如有)
|
||
|
||
请遵守以下规则:
|
||
1. 只输出严格 JSON,不要输出 markdown,不要输出额外解释。
|
||
2. 根据用户意图判断复杂度并选择路由。
|
||
3. speak 字段始终填写:给用户看的话。
|
||
|
||
路由规则:
|
||
- direct_reply:纯闲聊、简单问答、打招呼、感谢等。speak 直接写你的完整回复。
|
||
- execute:需要用工具处理的请求(查询日程、移动课程、排课等),但不需要先制定计划。speak 写简短确认。
|
||
- deep_answer:复杂问题但不需要工具(如分析建议、深度解释等),需要深度思考后直接回答。speak 写过渡语(如"让我想想")。
|
||
- plan:用户明确要求先制定计划,或涉及多阶段复杂规划。speak 写确认语。
|
||
|
||
粗排判断:当用户意图包含"批量安排/排课/把任务类排进日程",且上下文中有任务类 ID 时,设置 needs_rough_build=true。
|
||
粗排后微调判断:
|
||
- 仅当 needs_rough_build=true 时才判断 needs_refine_after_rough_build。
|
||
- 若用户明确提出优化目标/偏好(如"尽量均衡""周三别太满""某门课往后挪"),设 needs_refine_after_rough_build=true。
|
||
- 若用户只要求"先排进去/给初稿",未提出微调目标,设 needs_refine_after_rough_build=false。
|
||
顺序授权判断:
|
||
- allow_reorder 仅在用户明确说明“允许打乱顺序/顺序不重要”时才为 true。
|
||
- 用户明确要求“保持顺序/不要打乱”时必须为 false。
|
||
- 若用户未明确提及顺序,一律为 false。
|
||
|
||
输出协议(严格 JSON):
|
||
{"route":"direct_reply / execute / deep_answer / plan","speak":"给用户看的话","needs_rough_build":false,"needs_refine_after_rough_build":false,"allow_reorder":false,"reason":"简短判断依据"}
|
||
|
||
合法示例:
|
||
|
||
{"route":"direct_reply","speak":"你好!我是 SmartFlow 助手,有什么可以帮你的?","reason":"用户打招呼"}
|
||
|
||
{"route":"execute","speak":"好的,我来帮你看看今天的安排。","reason":"需要调用工具查询日程","needs_rough_build":false,"needs_refine_after_rough_build":false,"allow_reorder":false}
|
||
|
||
{"route":"execute","speak":"好的,我来帮你排课。","reason":"批量排课需求,有任务类 ID,未给微调偏好","needs_rough_build":true,"needs_refine_after_rough_build":false,"allow_reorder":false}
|
||
|
||
{"route":"execute","speak":"好的,我来帮你排课并按你的偏好做微调。","reason":"批量排课需求,有任务类 ID,且给出明确微调偏好","needs_rough_build":true,"needs_refine_after_rough_build":true,"allow_reorder":false}
|
||
|
||
{"route":"execute","speak":"好的,我按你的要求重排。","reason":"用户明确允许打乱顺序","needs_rough_build":false,"needs_refine_after_rough_build":false,"allow_reorder":true}
|
||
|
||
{"route":"deep_answer","speak":"这是个好问题,让我仔细想想。","reason":"需要深度分析但不需要工具"}
|
||
|
||
{"route":"plan","speak":"明白,我来帮你制定一个完整的学习计划。","reason":"用户明确要求制定计划"}
|
||
`
|
||
|
||
// BuildChatRoutingSystemPrompt 返回路由阶段的系统提示词。
|
||
func BuildChatRoutingSystemPrompt() string {
|
||
return strings.TrimSpace(chatRoutingSystemPrompt)
|
||
}
|
||
|
||
// BuildChatRoutingMessages 组装路由阶段的 messages。
|
||
func BuildChatRoutingMessages(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) []*schema.Message {
|
||
return buildStageMessages(
|
||
BuildChatRoutingSystemPrompt(),
|
||
ctx,
|
||
BuildChatRoutingUserPrompt(ctx, userInput, state),
|
||
)
|
||
}
|
||
|
||
// BuildChatRoutingUserPrompt 构造路由阶段的用户提示词。
|
||
func BuildChatRoutingUserPrompt(ctx *newagentmodel.ConversationContext, userInput string, state *newagentmodel.CommonState) string {
|
||
var sb strings.Builder
|
||
|
||
sb.WriteString("请判断用户本轮意图的复杂度,并选择最合适的路由。\n")
|
||
sb.WriteString("若 route=execute 且 needs_rough_build=true,请同时判断 needs_refine_after_rough_build:")
|
||
sb.WriteString("只有用户明确提出微调目标时才为 true。\n")
|
||
sb.WriteString("请同时输出 allow_reorder:只有用户明确授权打乱顺序时才为 true,默认 false。\n")
|
||
|
||
// 注入任务类上下文(供粗排判断参考)。
|
||
if state != nil && len(state.TaskClassIDs) > 0 {
|
||
parts := make([]string, len(state.TaskClassIDs))
|
||
for i, id := range state.TaskClassIDs {
|
||
parts[i] = fmt.Sprintf("%d", id)
|
||
}
|
||
sb.WriteString(fmt.Sprintf("\n本次请求涉及的任务类 ID:[%s]\n", strings.Join(parts, ", ")))
|
||
}
|
||
|
||
if state != nil && len(state.TaskClasses) > 0 {
|
||
sb.WriteString("任务类约束:\n")
|
||
for _, tc := range state.TaskClasses {
|
||
line := fmt.Sprintf("- [ID=%d] %s:策略=%s,总时段预算=%d", tc.ID, tc.Name, tc.Strategy, tc.TotalSlots)
|
||
if tc.StartDate != "" || tc.EndDate != "" {
|
||
line += fmt.Sprintf(",日期范围=%s ~ %s", tc.StartDate, tc.EndDate)
|
||
}
|
||
sb.WriteString(line + "\n")
|
||
}
|
||
}
|
||
|
||
trimmedInput := strings.TrimSpace(userInput)
|
||
if trimmedInput != "" {
|
||
sb.WriteString("\n用户本轮输入:\n")
|
||
sb.WriteString(trimmedInput)
|
||
sb.WriteString("\n")
|
||
}
|
||
|
||
return strings.TrimSpace(sb.String())
|
||
}
|
||
|
||
// --- 深度回答 prompt ---
|
||
|
||
const deepAnswerSystemPrompt = `
|
||
你是 SmartFlow 的深度分析助手。用户提出了一个需要深入思考的问题,请认真分析后给出详细、有价值的回答。
|
||
|
||
请遵守以下规则:
|
||
1. 充分利用上下文中已有的信息(任务类约束、日程数据、历史对话等)。
|
||
2. 如果缺少关键信息,在回答中说明需要哪些额外信息。
|
||
3. 直接输出你的回答,不要输出 JSON。
|
||
`
|
||
|
||
// BuildDeepAnswerSystemPrompt 返回深度回答阶段的系统提示词。
|
||
func BuildDeepAnswerSystemPrompt() string {
|
||
return strings.TrimSpace(deepAnswerSystemPrompt)
|
||
}
|
||
|
||
// BuildDeepAnswerMessages 组装深度回答阶段的 messages。
|
||
func BuildDeepAnswerMessages(ctx *newagentmodel.ConversationContext, userInput string) []*schema.Message {
|
||
return buildStageMessages(
|
||
BuildDeepAnswerSystemPrompt(),
|
||
ctx,
|
||
userInput,
|
||
)
|
||
}
|