Skip to content

Commit 54491bd

Browse files
committed
tests(aio): add functioncall test
1 parent bb792dd commit 54491bd

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

tests/e2e-aio/e2e_test.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
. "github.com/onsi/ginkgo/v2"
1212
. "github.com/onsi/gomega"
13-
1413
"github.com/sashabaranov/go-openai"
14+
"github.com/sashabaranov/go-openai/jsonschema"
1515
)
1616

1717
var _ = Describe("E2E test", func() {
@@ -42,6 +42,52 @@ var _ = Describe("E2E test", func() {
4242
})
4343
})
4444

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+
})
4591
Context("json", func() {
4692
It("correctly", func() {
4793
model := "gpt-4"

0 commit comments

Comments
 (0)