后端: 1. chat 路由新增“二次粗排硬闸门”,避免粗排完成后的微调请求误触发再次 rough_build - 更新 node/chat.go:当上下文已存在 rough_build_done 且用户未明确要求“重新粗排/从头重排”时,强制关闭 needs_rough_build / needs_refine_after_rough_build;补充路由调试日志维度(needs_rough_build、allow_reorder、has_rough_build_done 等) - 更新 prompt/chat.go:补齐二次粗排强约束,明确“移动/微调/优化/均匀化/调顺序”默认走 refine,不再次触发 rough build 2. execute 历史分层与工具调用写回链路增强 - 更新 node/execute.go:next_plan 推进后写入 execute_step_advanced marker,供 prompt 按步骤边界归档 loop;新增统一 appendToolCallResultHistory,标准化 assistant tool_call + tool observation 配对写回 - 更新 node/execute.go:confirm accept 路径补齐 min_context_switch 顺序护栏,避免通过确认链路绕过“未授权打乱顺序”限制 - 更新 prompt/execute_context.go:ReAct 边界识别从 loop_closed 扩展到 loop_closed/step_advanced;执行态文案收敛为“existing 仅作事实参考不作为可移动目标”,并新增参数纪律提示 - 更新 service/agentsvc/agent_newagent.go:冷恢复重置时仅在 completed 场景补写 execute_loop_closed marker,保证下一轮上下文归档一致 3. 工具参数严格校验落地(禁止自造字段) - 新建 tools/arg_guard.go:新增 validateToolArgsStrict 白名单校验,未知字段直接报错(含 day_from/day_to -> day_start/day_end 提示) - 更新 tools/read_filter_tools.go:query_available_slots / query_target_tasks 接入参数白名单校验 - 更新 tools/compound_tools.go:spread_even 接入参数白名单校验 - 更新 prompt/execute.go:系统提示补齐“参数必须严格使用 schema 字段”强约束与非法别名示例 4. execute 范围护栏辅助能力预埋 - 更新 node/execute.go:新增步骤范围解析与日历参数解析辅助(周/天/周几提取、候选 day 估算、batch_move new_day 提取等),为后续步骤级范围拦截提供基础能力 5. 记忆模块方案文档升级(吸收 Mem0 机制) - 更新 memory/记忆模块实施计划.md:补充 Mem0 借鉴与取舍,新增 ADD/UPDATE/DELETE/NONE 决策状态机、UUID 映射防幻觉、JSON 容错链、threshold->reranker->fallback、三维隔离过滤与对应指标/测试项 6. 同步更新调试日志文件 - 更新 newAgent/Log.txt 前端:无 仓库:无
141 lines
6.6 KiB
Go
141 lines
6.6 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。
|
||
二次粗排约束(强约束):
|
||
- 若上下文已出现 rough_build_done,且用户未明确要求“重新粗排/从头重排”,必须设置 needs_rough_build=false。
|
||
- “移动/微调/优化/均匀化/调顺序”等请求默认视为 refine,不得再次触发 rough build。
|
||
粗排后微调判断:
|
||
- 仅当 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,
|
||
)
|
||
}
|