diff --git a/api_integration_test.go b/api_integration_test.go index 3084268e6..1485e7d70 100644 --- a/api_integration_test.go +++ b/api_integration_test.go @@ -78,6 +78,20 @@ func TestAPI(t *testing.T) { ) checks.NoError(t, err, "CreateChatCompletion (with name) returned error") + functionParams := jsonschema.Definition{ + Type: jsonschema.Object, + Properties: map[string]jsonschema.Definition{ + "location": { + Type: jsonschema.String, + Description: "The city and state, e.g. San Francisco, CA", + }, + "unit": { + Type: jsonschema.String, + Enum: []string{"celsius", "fahrenheit"}, + }, + }, + Required: []string{"location"}, + } _, err = c.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ @@ -89,25 +103,31 @@ func TestAPI(t *testing.T) { }, }, Functions: []openai.FunctionDefinition{{ - Name: "get_current_weather", - Parameters: jsonschema.Definition{ - Type: jsonschema.Object, - Properties: map[string]jsonschema.Definition{ - "location": { - Type: jsonschema.String, - Description: "The city and state, e.g. San Francisco, CA", - }, - "unit": { - Type: jsonschema.String, - Enum: []string{"celsius", "fahrenheit"}, - }, - }, - Required: []string{"location"}, - }, + Name: "get_current_weather", + Parameters: functionParams, }}, }, ) checks.NoError(t, err, "CreateChatCompletion (with functions) returned error") + + _, err = c.CreateChatCompletion( + context.Background(), + openai.ChatCompletionRequest{ + Model: openai.GPT3Dot5Turbo, + Messages: []openai.ChatCompletionMessage{ + { + Role: openai.ChatMessageRoleUser, + Content: "What is the weather like in Boston?", + }, + }, + Functions: []openai.FunctionDefinition{{ + Name: "get_current_weather", + Parameters: functionParams, + Strict: true, + }}, + }, + ) + checks.NoError(t, err, "CreateChatCompletion (with functions, strict output) returned error") } func TestCompletionStream(t *testing.T) { diff --git a/chat.go b/chat.go index 31fa887d6..0c86f40fa 100644 --- a/chat.go +++ b/chat.go @@ -269,7 +269,8 @@ type FunctionDefinition struct { // or you can pass in a struct which serializes to the proper JSON schema. // The jsonschema package is provided for convenience, but you should // consider another specialized library if you require more complex schemas. - Parameters any `json:"parameters"` + Parameters any `json:"parameters"` + Strict bool `json:"strict"` } // Deprecated: use FunctionDefinition instead.