模型优化
This commit is contained in:
@@ -42,7 +42,8 @@ const (
|
||||
JSON 格式:{"tools":[{"name":"工具名称","reason":"..."}],"reason":"..."}
|
||||
工具名称必须来自“可用工具”列表。
|
||||
可以选择多个工具,工具会按配置顺序依次执行;后面的工具可以使用前面工具写入的上下文。
|
||||
如果用户问题包含今天、明天、昨天、本周、本月、本年、最近等相对时间,并且还需要查询数据库,请同时选择 time 和 sql。
|
||||
如果用户问题包含今天、今日、明天、昨天、本周、本月、本年、最近等相对时间,且还需要调用 search 或 sql,必须同时选择 time,并让 time 排在这些工具之前。
|
||||
例如“历史上的今天都发生了什么”应选择 time 和 search:先获取今天的绝对日期,再搜索当天历史事件;如果联网无结果,主模型会回退到自身知识库回答并说明来源。
|
||||
例如“本月有什么日程安排”应选择 time 和 sql:先获取本月绝对日期范围,再查询日程表。
|
||||
如果无需工具,返回 {"tools":[],"reason":"..."}。
|
||||
只选择确实必要的工具。`
|
||||
@@ -1104,8 +1105,8 @@ func runSearchTool(ctx context.Context, state *searchagent.State, messages []Cha
|
||||
}
|
||||
if len(results) == 0 {
|
||||
err := errors.New("未搜索到相关网页结果")
|
||||
emit(chatSSEFrame{Type: "trace", Tool: "search", Stage: "results", Status: "error", Message: err.Error()})
|
||||
return prependHiddenContext(messages, searchagent.BuildErrorContext(query, err)), nil
|
||||
emit(chatSSEFrame{Type: "trace", Tool: "search", Stage: "results", Status: "warning", Message: "未搜索到相关网页结果,将使用模型知识库回答"})
|
||||
return prependHiddenContext(messages, searchagent.BuildFallbackContext(profile, query, routeReason, err)), nil
|
||||
}
|
||||
emit(chatSSEFrame{Type: "trace", Tool: "search", Stage: "results", Status: "success", Message: fmt.Sprintf("联网搜索完成,找到 %d 条结果", len(results)), Data: map[string]any{"provider": profile.Provider, "count": len(results)}})
|
||||
return prependHiddenContext(messages, searchagent.BuildResultContext(profile, query, results, routeReason)), nil
|
||||
@@ -1130,6 +1131,7 @@ func enrichMessagesWithRoutedTools(ctx context.Context, chatProfile *OpenAIProfi
|
||||
return messages, err
|
||||
}
|
||||
selected := filterToolSelections(decision, tools, toolRouterState.cfg.Tools)
|
||||
selected = ensureTimeSelectionForRelativeQuery(selected, tools, toolRouterState.cfg.Tools, latestUserQuery(messages))
|
||||
if len(selected) == 0 {
|
||||
emit(chatSSEFrame{Type: "trace", Tool: "tool_router", Stage: "route", Status: "success", Message: "工具路由结果:无需调用工具", Data: map[string]any{"reason": decision.Reason}})
|
||||
return messages, nil
|
||||
@@ -1239,6 +1241,47 @@ func filterToolSelections(decision ToolRoutingDecision, tools map[string]ChatToo
|
||||
selected[item.Name] = item
|
||||
}
|
||||
}
|
||||
return orderToolSelections(selected, order)
|
||||
}
|
||||
|
||||
func ensureTimeSelectionForRelativeQuery(selected []ToolSelection, tools map[string]ChatTool, order []ToolRouteConfig, query string) []ToolSelection {
|
||||
if !containsRelativeTime(query) || hasToolSelection(selected, "time") || (!hasToolSelection(selected, "search") && !hasToolSelection(selected, "sql")) {
|
||||
return selected
|
||||
}
|
||||
if _, ok := tools["time"]; !ok {
|
||||
return selected
|
||||
}
|
||||
withTime := make(map[string]ToolSelection, len(selected)+1)
|
||||
for _, item := range selected {
|
||||
withTime[item.Name] = item
|
||||
}
|
||||
withTime["time"] = ToolSelection{Name: "time", Reason: "问题包含相对日期,需要先获取当前日期"}
|
||||
return orderToolSelections(withTime, order)
|
||||
}
|
||||
|
||||
func containsRelativeTime(query string) bool {
|
||||
query = strings.TrimSpace(query)
|
||||
if query == "" {
|
||||
return false
|
||||
}
|
||||
for _, keyword := range []string{"今天", "今日", "明天", "昨天", "本周", "这周", "本月", "这个月", "本年", "今年", "最近", "历史上的今天"} {
|
||||
if strings.Contains(query, keyword) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func hasToolSelection(selected []ToolSelection, name string) bool {
|
||||
for _, item := range selected {
|
||||
if item.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func orderToolSelections(selected map[string]ToolSelection, order []ToolRouteConfig) []ToolSelection {
|
||||
result := make([]ToolSelection, 0, len(selected))
|
||||
for _, item := range order {
|
||||
if selection, ok := selected[item.Name]; ok {
|
||||
|
||||
Reference in New Issue
Block a user