@@ -95,6 +95,31 @@ func CompleteToolCalls(ctx *gin.Context, completion pkg.ChatCompletion, callback
95
95
}
96
96
}
97
97
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
+
98
123
message , err := buildTemplate (ctx , completion , agent .ToolCall )
99
124
if err != nil {
100
125
return false , err
@@ -503,14 +528,16 @@ func toolIdWithTools(name string, tools []pkg.Keyv[interface{}]) (value string)
503
528
504
529
for _ , t := range tools {
505
530
fn := t .GetKeyv ("function" )
531
+ if fn .Has ("id" ) && value == fn .GetString ("id" ) {
532
+ return
533
+ }
534
+
506
535
if fn .Has ("name" ) {
507
536
if fn .Has ("id" ) && value == fn .GetString ("name" ) {
508
537
value = fn .GetString ("id" )
509
538
return
510
539
}
511
- }
512
-
513
- if fn .Has ("id" ) && value == fn .GetString ("id" ) {
540
+ value = fn .GetString ("name" )
514
541
return
515
542
}
516
543
}
@@ -543,6 +570,11 @@ func nameWithTools(name string, tools []pkg.Keyv[interface{}]) (value string) {
543
570
}
544
571
545
572
func toolIsEnabled (ctx * gin.Context ) bool {
573
+ completion := common .GetGinCompletion (ctx )
574
+ if completion .ToolChoice != "" && completion .ToolChoice != "auto" {
575
+ return false
576
+ }
577
+
546
578
t := common .GetGinToolValue (ctx )
547
579
return t .Is ("tasks" , true )
548
580
}
0 commit comments