Skip to content

Commit d6c202b

Browse files
committed
Updates to tests and tools
1 parent ff3c9bd commit d6c202b

File tree

5 files changed

+60
-36
lines changed

5 files changed

+60
-36
lines changed

pkg/openai/chat.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ import (
2020
// A request for a chat completion
2121
type reqChat struct {
2222
options
23+
Tools []reqChatTools `json:"tools,omitempty"`
2324
Messages []*schema.Message `json:"messages,omitempty"`
2425
}
2526

27+
type reqChatTools struct {
28+
Type string `json:"type"`
29+
Function *schema.Tool `json:"function"`
30+
}
31+
2632
// A chat completion object
2733
type respChat struct {
2834
Id string `json:"id"`
@@ -70,6 +76,14 @@ func (c *Client) Chat(ctx context.Context, messages []*schema.Message, opts ...O
7076
}
7177
}
7278

79+
// Append tools
80+
for _, tool := range request.options.Tools {
81+
request.Tools = append(request.Tools, reqChatTools{
82+
Type: "function",
83+
Function: tool,
84+
})
85+
}
86+
7387
// Set up the request
7488
reqopts := []client.RequestOpt{
7589
client.OptPath("chat/completions"),

pkg/openai/chat_test.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package openai_test
22

33
import (
4+
"context"
45
"encoding/json"
56
"os"
7+
"reflect"
68
"testing"
79

810
// Packages
911
opts "github.com/mutablelogic/go-client"
1012
openai "github.com/mutablelogic/go-client/pkg/openai"
13+
schema "github.com/mutablelogic/go-client/pkg/openai/schema"
1114
assert "github.com/stretchr/testify/assert"
1215
)
1316

@@ -17,8 +20,8 @@ func Test_chat_001(t *testing.T) {
1720
assert.NoError(err)
1821
assert.NotNil(client)
1922

20-
message := openai.NewMessage("user", "What would be the best app to use to get the weather in berlin today?")
21-
response, err := client.Chat([]*openai.Message{message})
23+
message := schema.NewMessage("user", "What would be the best app to use to get the weather in berlin today?")
24+
response, err := client.Chat(context.Background(), []*schema.Message{message})
2225
assert.NoError(err)
2326
assert.NotNil(response)
2427
assert.NotEmpty(response)
@@ -35,23 +38,16 @@ func Test_chat_002(t *testing.T) {
3538
assert.NoError(err)
3639
assert.NotNil(client)
3740

38-
message := openai.NewMessage("user", "What will the weather be like in Berlin tomorrow?")
39-
response, err := client.Chat([]*openai.Message{message}, openai.OptFunction("get_weather", "Get the weather in a specific city and country", openai.ToolParameter{
40-
Name: "city",
41-
Type: "string",
42-
Description: "The city to get the weather for",
43-
Required: true,
44-
}, openai.ToolParameter{
45-
Name: "country",
46-
Type: "string",
47-
Description: "The country to get the weather for",
48-
Required: true,
49-
}, openai.ToolParameter{
50-
Name: "time",
51-
Type: "string",
52-
Description: "When to get the weather for. If not specified, defaults to the current time",
53-
Required: true,
54-
}))
41+
message := schema.NewMessage("user", "What will the weather be like in Berlin tomorrow?")
42+
assert.NotNil(message)
43+
44+
get_weather := schema.NewTool("get_weather", "Get the weather in a specific city and country")
45+
assert.NotNil(get_weather)
46+
assert.NoError(get_weather.Add("city", "The city to get the weather for", true, reflect.TypeOf("string")))
47+
assert.NoError(get_weather.Add("country", "The country to get the weather for", true, reflect.TypeOf("string")))
48+
assert.NoError(get_weather.Add("time", "When to get the weather for. If not specified, defaults to the current time", true, reflect.TypeOf("string")))
49+
50+
response, err := client.Chat(context.Background(), []*schema.Message{message}, openai.OptTool(get_weather))
5551
assert.NoError(err)
5652
assert.NotNil(response)
5753
assert.NotEmpty(response)
@@ -68,14 +64,14 @@ func Test_chat_003(t *testing.T) {
6864
assert.NoError(err)
6965
assert.NotNil(client)
7066

71-
message := openai.NewMessage("user", "What is in this image").AppendImageUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg")
72-
response, err := client.Chat([]*openai.Message{message}, openai.OptModel("gpt-4-vision-preview"))
67+
message := schema.NewMessage("user", "What is in this image")
68+
// .AppendImageUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg")
69+
response, err := client.Chat(context.Background(), []*schema.Message{message}, openai.OptModel("gpt-4-vision-preview"))
7370
assert.NoError(err)
7471
assert.NotNil(response)
7572
assert.NotEmpty(response)
7673

7774
data, err := json.MarshalIndent(response, "", " ")
7875
assert.NoError(err)
7976
t.Log(string(data))
80-
8177
}

pkg/openai/image_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package openai_test
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -17,7 +18,7 @@ func Test_image_001(t *testing.T) {
1718
assert.NoError(err)
1819
assert.NotNil(client)
1920

20-
images, err := client.CreateImages("A painting of a cat", openai.OptCount(1))
21+
images, err := client.CreateImages(context.Background(), "A painting of a cat", openai.OptCount(1))
2122
assert.NoError(err)
2223
assert.NotNil(images)
2324
assert.NotEmpty(images)
@@ -31,7 +32,7 @@ func Test_image_002(t *testing.T) {
3132
assert.NotNil(client)
3233

3334
// Create one image
34-
images, err := client.CreateImages("A painting of a cat in the style of Salvador Dali", openai.OptResponseFormat("b64_json"), openai.OptCount(1))
35+
images, err := client.CreateImages(context.Background(), "A painting of a cat in the style of Salvador Dali", openai.OptResponseFormat("b64_json"), openai.OptCount(1))
3536
assert.NoError(err)
3637
assert.NotNil(images)
3738
assert.NotEmpty(images)
@@ -60,7 +61,7 @@ func Test_image_003(t *testing.T) {
6061
assert.NotNil(client)
6162

6263
// Create one image
63-
images, err := client.CreateImages("A painting of a cat in the style of Van Gogh", openai.OptResponseFormat("url"), openai.OptCount(1))
64+
images, err := client.CreateImages(context.Background(), "A painting of a cat in the style of Van Gogh", openai.OptResponseFormat("url"), openai.OptCount(1))
6465
assert.NoError(err)
6566
assert.NotNil(images)
6667
assert.NotEmpty(images)

pkg/openai/opts.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,28 @@ import (
1414
// TYPES
1515

1616
type options struct {
17-
Model string `json:"model,omitempty"`
17+
// Common options
18+
Count int `json:"n,omitempty"`
19+
MaxTokens int `json:"max_tokens,omitempty"`
20+
Model string `json:"model,omitempty"`
21+
ResponseFormat string `json:"response_format,omitempty"`
22+
Seed int `json:"seed,omitempty"`
23+
Temperature *float32 `json:"temperature,omitempty"`
24+
User string `json:"user,omitempty"`
25+
26+
// Options for chat
1827
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
1928
PresencePenalty float32 `json:"presence_penalty,omitempty"`
20-
MaxTokens int `json:"max_tokens,omitempty"`
21-
Count int `json:"n,omitempty"`
22-
ResponseFormat string `json:"response_format,omitempty"`
23-
Seed int `json:"seed,omitempty"`
29+
Tools []*schema.Tool `json:"-"`
2430
Stop []string `json:"stop,omitempty"`
25-
Temperature *float32 `json:"temperature,omitempty"`
26-
Tools []schema.Tool `json:"tools,omitempty"`
27-
User string `json:"user,omitempty"`
2831
Stream bool `json:"stream,omitempty"`
2932
StreamOptions *streamoptions `json:"stream_options,omitempty"`
3033
StreamCallback Callback `json:"-"`
3134

3235
// Options for audio
33-
Speed *float32 `json:"speed,omitempty"`
34-
Prompt string `json:"prompt,omitempty"`
3536
Language string `json:"language,omitempty"`
37+
Prompt string `json:"prompt,omitempty"`
38+
Speed *float32 `json:"speed,omitempty"`
3639

3740
// Options for images
3841
Quality string `json:"quality,omitempty"`
@@ -158,9 +161,18 @@ func OptTemperature(v float32) Opt {
158161
// A list of tools the model may call. Currently, only functions are supported as a tool.
159162
// Use this to provide a list of functions the model may generate JSON inputs for.
160163
// A max of 128 functions are supported.
161-
func OptTools(value ...schema.Tool) Opt {
164+
func OptTool(value ...*schema.Tool) Opt {
162165
return func(o *options) error {
166+
// Check tools
167+
for _, tool := range value {
168+
if tool == nil {
169+
return ErrBadParameter.With("OptTool")
170+
}
171+
}
172+
// Append tools
163173
o.Tools = append(o.Tools, value...)
174+
175+
// Return success
164176
return nil
165177
}
166178
}

pkg/openai/schema/tool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
type Tool struct {
1818
Name string `json:"name"`
1919
Description string `json:"description"`
20+
Type string `json:"type,omitempty"`
2021
Parameters *toolParameters `json:"input_schema,omitempty"`
2122
}
2223

0 commit comments

Comments
 (0)