@@ -9,24 +9,24 @@ import (
9
9
log "github.com/sirupsen/logrus"
10
10
)
11
11
12
- type functionCallType string
12
+ type toolChoiceType string
13
13
14
14
const (
15
- fnCallAuto functionCallType = "auto"
16
- fnCallNone functionCallType = "none"
15
+ toolChoiceAuto toolChoiceType = "auto"
16
+ toolChoiceNone toolChoiceType = "none"
17
17
)
18
18
19
19
func (c * oaiClients ) openaiGptChatCompletion (ctx context.Context , prompt * strings.Builder , temp float32 ) (string , error ) {
20
20
var (
21
- resp openai.ChatCompletionResponse
22
- req openai.ChatCompletionRequest
23
- funcName * openai.FunctionCall
24
- content string
25
- err error
21
+ resp openai.ChatCompletionResponse
22
+ req openai.ChatCompletionRequest
23
+ content string
24
+ err error
26
25
)
27
26
28
27
// if we are using the k8s API, we need to call the functions
29
- fnCallType := fnCallAuto
28
+ toolChoiseType := toolChoiceAuto
29
+
30
30
for {
31
31
prompt .WriteString (content )
32
32
log .Debugf ("prompt: %s" , prompt .String ())
@@ -44,30 +44,36 @@ func (c *oaiClients) openaiGptChatCompletion(ctx context.Context, prompt *string
44
44
}
45
45
46
46
if * usek8sAPI {
47
- // TODO: migrate to tools api
48
- req .Functions = []openai.FunctionDefinition { // nolint:staticcheck
49
- findSchemaNames ,
50
- getSchema ,
47
+ req .Tools = []openai.Tool {
48
+ {
49
+ Type : "function" ,
50
+ Function : & findSchemaNames ,
51
+ },
52
+ {
53
+ Type : "function" ,
54
+ Function : & getSchema ,
55
+ },
51
56
}
52
- req .FunctionCall = fnCallType // nolint:staticcheck
57
+ req .ToolChoice = toolChoiseType
53
58
}
54
59
55
60
resp , err = c .openAIClient .CreateChatCompletion (ctx , req )
56
61
if err != nil {
57
62
return "" , err
58
63
}
59
64
60
- funcName = resp .Choices [0 ].Message .FunctionCall
61
- // if there is no function call, we are done
62
- if funcName == nil {
65
+ if len (resp .Choices [0 ].Message .ToolCalls ) == 0 {
63
66
break
64
67
}
65
- log .Debugf ("calling function: %s" , funcName .Name )
66
68
67
- // if there is a function call, we need to call it and get the result
68
- content , err = funcCall (funcName )
69
- if err != nil {
70
- return "" , err
69
+ for _ , tool := range resp .Choices [0 ].Message .ToolCalls {
70
+ log .Debugf ("calling tool: %s" , tool .Function .Name )
71
+
72
+ // if there is a tool call, we need to call it and get the result
73
+ content , err = callTool (tool )
74
+ if err != nil {
75
+ return "" , err
76
+ }
71
77
}
72
78
}
73
79
0 commit comments