-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix function calling for openai
- Loading branch information
Showing
18 changed files
with
155 additions
and
132 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
use schemars::schema::{RootSchema, Schema, SchemaObject, SingleOrVec}; | ||
|
||
pub use schemars::{schema_for, JsonSchema}; | ||
|
||
/// Function Calling has strict requirements for JsonSchema, use fix_json_schema to fix it | ||
/// 1. Remove $schema field | ||
/// 2. Remove $format field | ||
/// 3. Object type Schema must set additionalProperties: false | ||
/// 4. required field should include all properties fields, meaning all struct fields are required (no Option) | ||
pub fn fix_json_schema(schema: &mut RootSchema) { | ||
schema.meta_schema = None; // Remove the $schema field | ||
fix_obj_schema(&mut schema.schema); | ||
} | ||
|
||
fn fix_obj_schema(schema: &mut SchemaObject) { | ||
schema.format = None; // Remove the $format field | ||
if let Some(obj) = &mut schema.object { | ||
// https://platform.openai.com/docs/guides/structured-outputs#additionalproperties-false-must-always-be-set-in-objects | ||
obj.additional_properties = Some(Box::new(Schema::Bool(false))); | ||
// if obj.required.len() != obj.properties.len() { | ||
// obj.required = obj.properties.keys().cloned().collect(); | ||
// } | ||
for v in obj.properties.values_mut() { | ||
if let Schema::Object(o) = v { | ||
fix_obj_schema(o); | ||
} | ||
} | ||
} | ||
if let Some(arr) = &mut schema.array { | ||
if let Some(v) = &mut arr.items { | ||
match v { | ||
SingleOrVec::Single(v) => { | ||
if let Schema::Object(o) = v.as_mut() { | ||
fix_obj_schema(o); | ||
} | ||
} | ||
SingleOrVec::Vec(arr) => { | ||
for v in arr { | ||
if let Schema::Object(o) = v { | ||
fix_obj_schema(o); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.