Improve handling of JSON Schema in OpenAI API Response Context (#819)
* feat: add jsonschema.Validate and jsonschema.Unmarshal * fix Sanity check * remove slices.Contains * fix Sanity check * add SchemaWrapper * update api_integration_test.go * update method 'reflectSchema' to support 'omitempty' in JSON tag * add GenerateSchemaForType * update json_test.go * update `Warp` to `Wrap` * fix Sanity check * fix Sanity check * update api_internal_test.go * update README.md * update README.md * remove jsonschema.SchemaWrapper * remove jsonschema.SchemaWrapper * fix Sanity check * optimize code formatting
This commit is contained in:
@@ -4,7 +4,6 @@ package openai_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
@@ -190,6 +189,17 @@ func TestChatCompletionResponseFormat_JSONSchema(t *testing.T) {
|
||||
c := openai.NewClient(apiToken)
|
||||
ctx := context.Background()
|
||||
|
||||
type MyStructuredResponse struct {
|
||||
PascalCase string `json:"pascal_case" required:"true" description:"PascalCase"`
|
||||
CamelCase string `json:"camel_case" required:"true" description:"CamelCase"`
|
||||
KebabCase string `json:"kebab_case" required:"true" description:"KebabCase"`
|
||||
SnakeCase string `json:"snake_case" required:"true" description:"SnakeCase"`
|
||||
}
|
||||
var result MyStructuredResponse
|
||||
schema, err := jsonschema.GenerateSchemaForType(result)
|
||||
if err != nil {
|
||||
t.Fatal("CreateChatCompletion (use json_schema response) GenerateSchemaForType error")
|
||||
}
|
||||
resp, err := c.CreateChatCompletion(
|
||||
ctx,
|
||||
openai.ChatCompletionRequest{
|
||||
@@ -212,31 +222,17 @@ func TestChatCompletionResponseFormat_JSONSchema(t *testing.T) {
|
||||
ResponseFormat: &openai.ChatCompletionResponseFormat{
|
||||
Type: openai.ChatCompletionResponseFormatTypeJSONSchema,
|
||||
JSONSchema: &openai.ChatCompletionResponseFormatJSONSchema{
|
||||
Name: "cases",
|
||||
Schema: jsonschema.Definition{
|
||||
Type: jsonschema.Object,
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
"PascalCase": jsonschema.Definition{Type: jsonschema.String},
|
||||
"CamelCase": jsonschema.Definition{Type: jsonschema.String},
|
||||
"KebabCase": jsonschema.Definition{Type: jsonschema.String},
|
||||
"SnakeCase": jsonschema.Definition{Type: jsonschema.String},
|
||||
},
|
||||
Required: []string{"PascalCase", "CamelCase", "KebabCase", "SnakeCase"},
|
||||
AdditionalProperties: false,
|
||||
},
|
||||
Name: "cases",
|
||||
Schema: schema,
|
||||
Strict: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
checks.NoError(t, err, "CreateChatCompletion (use json_schema response) returned error")
|
||||
var result = make(map[string]string)
|
||||
err = json.Unmarshal([]byte(resp.Choices[0].Message.Content), &result)
|
||||
checks.NoError(t, err, "CreateChatCompletion (use json_schema response) unmarshal error")
|
||||
for _, key := range []string{"PascalCase", "CamelCase", "KebabCase", "SnakeCase"} {
|
||||
if _, ok := result[key]; !ok {
|
||||
t.Errorf("key:%s does not exist.", key)
|
||||
}
|
||||
if err == nil {
|
||||
err = schema.Unmarshal(resp.Choices[0].Message.Content, &result)
|
||||
checks.NoError(t, err, "CreateChatCompletion (use json_schema response) unmarshal error")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user