Skip to content

Commit 622f4f0

Browse files
committed
fix: ToolChoice数据类型解析异常
1 parent 53d4eff commit 622f4f0

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

internal/agent/com.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ TOOL_CALL:
2828
你是一个智能机器人,你拥有专注于拆解多个任务的能力。有时候,你可以依赖工具的运行结果,来更准确的回答用户。
2929
3030
请你根据用户请求,拆解出3个以内的子任务。在完成拆解过程中,USER代表用户的输入,TOOL_RESPONSE代表工具运行结果。ASSISTANT 代表你的输出,task为子任务字符串描述。
31+
浏览上面的上下文,避免出现与最新用户请求无关的子任务。
3132
3233
你的每次输出都必须以0,1开头,代表是否需要拆解任务:
3334
0: 无拆解任务。

internal/plugin/toolcall.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ func CompleteToolCalls(ctx *gin.Context, completion pkg.ChatCompletion, callback
9595
}
9696
}
9797

98+
// toolChoice自推荐toolId处理
99+
if completion.ToolChoice != "" && completion.ToolChoice != "auto" {
100+
var (
101+
keyv pkg.Keyv[interface{}]
102+
toolChoice pkg.Keyv[interface{}]
103+
ok = false
104+
)
105+
toolChoice, ok = completion.ToolChoice.(map[string]interface{})
106+
if !ok || !toolChoice.Is("type", "function") {
107+
goto label
108+
}
109+
110+
keyv = toolChoice.GetKeyv("function")
111+
if !keyv.Has("name") {
112+
goto label
113+
}
114+
115+
if toolId := toolIdWithTools(keyv.GetString("name"), completion.Tools); toolId != "-1" {
116+
completion.Messages = append(completion.Messages, pkg.Keyv[interface{}]{
117+
"role": "user", "content": "continue。 工具推荐: toolId = " + toolId,
118+
})
119+
}
120+
label:
121+
}
122+
98123
message, err := buildTemplate(ctx, completion, agent.ToolCall)
99124
if err != nil {
100125
return false, err
@@ -503,14 +528,16 @@ func toolIdWithTools(name string, tools []pkg.Keyv[interface{}]) (value string)
503528

504529
for _, t := range tools {
505530
fn := t.GetKeyv("function")
531+
if fn.Has("id") && value == fn.GetString("id") {
532+
return
533+
}
534+
506535
if fn.Has("name") {
507536
if fn.Has("id") && value == fn.GetString("name") {
508537
value = fn.GetString("id")
509538
return
510539
}
511-
}
512-
513-
if fn.Has("id") && value == fn.GetString("id") {
540+
value = fn.GetString("name")
514541
return
515542
}
516543
}
@@ -543,6 +570,11 @@ func nameWithTools(name string, tools []pkg.Keyv[interface{}]) (value string) {
543570
}
544571

545572
func toolIsEnabled(ctx *gin.Context) bool {
573+
completion := common.GetGinCompletion(ctx)
574+
if completion.ToolChoice != "" && completion.ToolChoice != "auto" {
575+
return false
576+
}
577+
546578
t := common.GetGinToolValue(ctx)
547579
return t.Is("tasks", true)
548580
}

pkg/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type ChatCompletion struct {
1515
TopK int `json:"topK"`
1616
TopP float32 `json:"topP"`
1717
Stream bool `json:"stream"`
18-
ToolChoice string `json:"tool_choice"`
18+
ToolChoice interface{} `json:"tool_choice"`
1919
}
2020

2121
type ChatGeneration struct {

0 commit comments

Comments
 (0)