添加终止工具调用信号,优化效率
This commit is contained in:
BIN
Binary file not shown.
@@ -22,6 +22,21 @@ type FunctionToolRuntime struct {
|
|||||||
UserInfo *CurrentUserInfo
|
UserInfo *CurrentUserInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// terminalFunctionTools 标记一些"调用即结束"的工具:
|
||||||
|
// 这类工具的返回结果已经包含了用户问题所需的全部数据,调用之后无需再让模型
|
||||||
|
// 决定是否继续调用其他工具,直接进入最终回答生成阶段,可以省掉一轮模型请求。
|
||||||
|
var terminalFunctionTools = map[string]bool{
|
||||||
|
"ops_ai_assistant": true,
|
||||||
|
"ops_ai_assistant_schedule_query": true,
|
||||||
|
"ops_ai_assistant_purchase_query": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsTerminalFunctionTool 判断给定工具名是否为终止工具。命名匹配采用与
|
||||||
|
// FunctionToolSchemas / ExecuteFunctionTool 一致的"小写并裁剪空格"规则。
|
||||||
|
func IsTerminalFunctionTool(name string) bool {
|
||||||
|
return terminalFunctionTools[strings.ToLower(strings.TrimSpace(name))]
|
||||||
|
}
|
||||||
|
|
||||||
func FunctionToolSchemas(configs []ToolConfig) []FunctionToolSchema {
|
func FunctionToolSchemas(configs []ToolConfig) []FunctionToolSchema {
|
||||||
tools := make([]FunctionToolSchema, 0)
|
tools := make([]FunctionToolSchema, 0)
|
||||||
for _, config := range configs {
|
for _, config := range configs {
|
||||||
|
|||||||
@@ -947,6 +947,7 @@ func runOpenAIToolLoop(ctx context.Context, profile models.ConfigsAIChatOpenAI_,
|
|||||||
|
|
||||||
toolExecuted = true
|
toolExecuted = true
|
||||||
messages = append(messages, openaiMessage{Role: "assistant", Content: message.Content, ToolCalls: message.ToolCalls})
|
messages = append(messages, openaiMessage{Role: "assistant", Content: message.Content, ToolCalls: message.ToolCalls})
|
||||||
|
terminalToolName := ""
|
||||||
for _, toolCall := range message.ToolCalls {
|
for _, toolCall := range message.ToolCalls {
|
||||||
toolName := strings.TrimSpace(toolCall.Function.Name)
|
toolName := strings.TrimSpace(toolCall.Function.Name)
|
||||||
parsedArgs := parseJSONTraceValue(toolCall.Function.Arguments)
|
parsedArgs := parseJSONTraceValue(toolCall.Function.Arguments)
|
||||||
@@ -974,6 +975,19 @@ func runOpenAIToolLoop(ctx context.Context, profile models.ConfigsAIChatOpenAI_,
|
|||||||
trace(toolName, "execute", status, "工具执行完成:"+toolName, data)
|
trace(toolName, "execute", status, "工具执行完成:"+toolName, data)
|
||||||
}
|
}
|
||||||
messages = append(messages, openaiMessage{Role: "tool", ToolCallID: toolCall.ID, Name: toolName, Content: string(resultJSON)})
|
messages = append(messages, openaiMessage{Role: "tool", ToolCallID: toolCall.ID, Name: toolName, Content: string(resultJSON)})
|
||||||
|
// 终止工具:仅在工具自身执行成功时短路。失败时仍交给模型重试或换工具。
|
||||||
|
if status == "success" && terminalToolName == "" && agents.IsTerminalFunctionTool(toolName) {
|
||||||
|
terminalToolName = toolName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if terminalToolName != "" {
|
||||||
|
if trace != nil {
|
||||||
|
trace(terminalToolName, "tool_call", "success", "命中终止工具,跳过下一轮工具决策,直接生成最终回答", map[string]interface{}{
|
||||||
|
"tool": terminalToolName,
|
||||||
|
"round": round + 1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return messages, toolExecuted, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return messages, toolExecuted, fmt.Errorf("工具调用超过最大轮数")
|
return messages, toolExecuted, fmt.Errorf("工具调用超过最大轮数")
|
||||||
|
|||||||
Reference in New Issue
Block a user