Skip to content

Commit

Permalink
Allow structured outputs via function calling
Browse files Browse the repository at this point in the history
  • Loading branch information
greysteil committed Aug 16, 2024
1 parent 2c6889e commit 3ba61eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ type ToolFunction struct {
type FunctionDefinition struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Strict bool `json:"strict,omitempty"`
// Parameters is an object describing the function.
// You can pass json.RawMessage to describe the schema,
// or you can pass in a struct which serializes to the proper JSON schema.
Expand Down
26 changes: 26 additions & 0 deletions chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,32 @@ func TestChatCompletionsFunctions(t *testing.T) {
})
checks.NoError(t, err, "CreateChatCompletion with functions error")
})
t.Run("StructuredOutputs", func(t *testing.T) {
type testMessage struct {
Count int `json:"count"`
Words []string `json:"words"`
}
msg := testMessage{
Count: 2,
Words: []string{"hello", "world"},
}
_, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
MaxTokens: 5,
Model: openai.GPT3Dot5Turbo0613,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: "Hello!",
},
},
Functions: []openai.FunctionDefinition{{
Name: "test",
Strict: true,
Parameters: &msg,
}},
})
checks.NoError(t, err, "CreateChatCompletion with functions error")
})
}

func TestAzureChatCompletions(t *testing.T) {
Expand Down

0 comments on commit 3ba61eb

Please sign in to comment.