-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Improve handling of JSON Schema in OpenAI API Response Context #819
Merged
sashabaranov
merged 21 commits into
sashabaranov:master
from
eiixy:feature/jsonschema-validate
Aug 24, 2024
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
47856de
feat: add jsonschema.Validate and jsonschema.Unmarshal
eiixy 0e89407
fix Sanity check
eiixy 545ee4b
remove slices.Contains
eiixy 8bb6204
fix Sanity check
eiixy 051a17f
Merge branch 'sashabaranov:master' into feature/jsonschema-validate
eiixy 8b49a36
add SchemaWrapper
eiixy cdb0075
update api_integration_test.go
eiixy 1bf03a8
update method 'reflectSchema' to support 'omitempty' in JSON tag
eiixy d3fd653
add GenerateSchemaForType
eiixy 162bb6a
update json_test.go
eiixy 35d36ca
update `Warp` to `Wrap`
eiixy a80ea2f
fix Sanity check
eiixy 5018f63
fix Sanity check
eiixy 290bc29
update api_internal_test.go
eiixy 25d8769
update README.md
eiixy e21015f
update README.md
eiixy 9332d59
Merge branch 'sashabaranov:master' into feature/jsonschema-validate
eiixy a4c1156
remove jsonschema.SchemaWrapper
eiixy 4d0750d
remove jsonschema.SchemaWrapper
eiixy 9db4d84
fix Sanity check
eiixy 8680e7b
optimize code formatting
eiixy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package jsonschema | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
) | ||
|
||
func VerifySchemaAndUnmarshal(schema Definition, content []byte, v any) error { | ||
var data any | ||
err := json.Unmarshal(content, &data) | ||
if err != nil { | ||
return err | ||
} | ||
if !Validate(schema, data) { | ||
return errors.New("data validation failed against the provided schema") | ||
} | ||
return json.Unmarshal(content, &v) | ||
} | ||
|
||
func Validate(schema Definition, data any) bool { | ||
switch schema.Type { | ||
case Object: | ||
return validateObject(schema, data) | ||
case Array: | ||
return validateArray(schema, data) | ||
case String: | ||
_, ok := data.(string) | ||
return ok | ||
case Number: // float64 and int | ||
_, ok := data.(float64) | ||
if !ok { | ||
_, ok = data.(int) | ||
} | ||
return ok | ||
case Boolean: | ||
_, ok := data.(bool) | ||
return ok | ||
case Integer: | ||
_, ok := data.(int) | ||
return ok | ||
case Null: | ||
return data == nil | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
func validateObject(schema Definition, data any) bool { | ||
dataMap, ok := data.(map[string]any) | ||
if !ok { | ||
return false | ||
} | ||
for _, field := range schema.Required { | ||
if _, exists := dataMap[field]; !exists { | ||
return false | ||
} | ||
} | ||
for key, valueSchema := range schema.Properties { | ||
value, exists := dataMap[key] | ||
if exists && !Validate(valueSchema, value) { | ||
return false | ||
} else if !exists && contains(schema.Required, key) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func validateArray(schema Definition, data any) bool { | ||
dataArray, ok := data.([]any) | ||
if !ok { | ||
return false | ||
} | ||
for _, item := range dataArray { | ||
if !Validate(*schema.Items, item) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func contains[S ~[]E, E comparable](s S, v E) bool { | ||
for i := range s { | ||
if v == s[i] { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also support float32 here?