@@ -10,8 +10,8 @@ import (
10
10
11
11
. "github.com/onsi/ginkgo/v2"
12
12
. "github.com/onsi/gomega"
13
-
14
13
"github.com/sashabaranov/go-openai"
14
+ "github.com/sashabaranov/go-openai/jsonschema"
15
15
)
16
16
17
17
var _ = Describe ("E2E test" , func () {
@@ -42,6 +42,52 @@ var _ = Describe("E2E test", func() {
42
42
})
43
43
})
44
44
45
+ Context ("function calls" , func () {
46
+ It ("correctly invoke" , func () {
47
+ params := jsonschema.Definition {
48
+ Type : jsonschema .Object ,
49
+ Properties : map [string ]jsonschema.Definition {
50
+ "location" : {
51
+ Type : jsonschema .String ,
52
+ Description : "The city and state, e.g. San Francisco, CA" ,
53
+ },
54
+ "unit" : {
55
+ Type : jsonschema .String ,
56
+ Enum : []string {"celsius" , "fahrenheit" },
57
+ },
58
+ },
59
+ Required : []string {"location" },
60
+ }
61
+
62
+ f := openai.FunctionDefinition {
63
+ Name : "get_current_weather" ,
64
+ Description : "Get the current weather in a given location" ,
65
+ Parameters : params ,
66
+ }
67
+ t := openai.Tool {
68
+ Type : openai .ToolTypeFunction ,
69
+ Function : & f ,
70
+ }
71
+
72
+ dialogue := []openai.ChatCompletionMessage {
73
+ {Role : openai .ChatMessageRoleUser , Content : "What is the weather in Boston today?" },
74
+ }
75
+ resp , err := client .CreateChatCompletion (context .TODO (),
76
+ openai.ChatCompletionRequest {
77
+ Model : openai .GPT4TurboPreview ,
78
+ Messages : dialogue ,
79
+ Tools : []openai.Tool {t },
80
+ },
81
+ )
82
+ Expect (err ).ToNot (HaveOccurred ())
83
+ Expect (len (resp .Choices )).To (Equal (1 ), fmt .Sprint (resp ))
84
+
85
+ msg := resp .Choices [0 ].Message
86
+ Expect (len (msg .ToolCalls )).To (Equal (1 ), fmt .Sprint (msg .ToolCalls ))
87
+ Expect (msg .ToolCalls [0 ].Function .Name ).To (Equal ("get_current_weather" ), fmt .Sprint (msg .ToolCalls [0 ].Function .Name ))
88
+ Expect (msg .ToolCalls [0 ].Function .Arguments ).To (ContainSubstring ("Boston" ), fmt .Sprint (msg .ToolCalls [0 ].Function .Arguments ))
89
+ })
90
+ })
45
91
Context ("json" , func () {
46
92
It ("correctly" , func () {
47
93
model := "gpt-4"
0 commit comments