Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add strict parameter to function definition #827

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions api_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading