Add support for required fields in parameter generation
Previously, required fields were not handled in OpenAI and Google parameter generation. This update adds logic to include a "required" list for both, ensuring mandatory fields are accurately captured in the schema outputs.
This commit is contained in:
parent
5ba0d5df7e
commit
2ae583e9f3
@ -18,28 +18,48 @@ type object struct {
|
|||||||
|
|
||||||
func (o object) OpenAIParameters() openai.FunctionParameters {
|
func (o object) OpenAIParameters() openai.FunctionParameters {
|
||||||
var properties = map[string]openai.FunctionParameters{}
|
var properties = map[string]openai.FunctionParameters{}
|
||||||
|
var required []string
|
||||||
for k, v := range o.fields {
|
for k, v := range o.fields {
|
||||||
properties[k] = v.OpenAIParameters()
|
properties[k] = v.OpenAIParameters()
|
||||||
|
if v.Required() {
|
||||||
|
required = append(required, k)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return openai.FunctionParameters{
|
var res = openai.FunctionParameters{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"description": o.Description(),
|
"description": o.Description(),
|
||||||
"properties": properties,
|
"properties": properties,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(required) > 0 {
|
||||||
|
res["required"] = required
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o object) GoogleParameters() *genai.Schema {
|
func (o object) GoogleParameters() *genai.Schema {
|
||||||
var properties = map[string]*genai.Schema{}
|
var properties = map[string]*genai.Schema{}
|
||||||
|
var required []string
|
||||||
for k, v := range o.fields {
|
for k, v := range o.fields {
|
||||||
properties[k] = v.GoogleParameters()
|
properties[k] = v.GoogleParameters()
|
||||||
|
if v.Required() {
|
||||||
|
required = append(required, k)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &genai.Schema{
|
var res = &genai.Schema{
|
||||||
Type: genai.TypeObject,
|
Type: genai.TypeObject,
|
||||||
Description: o.Description(),
|
Description: o.Description(),
|
||||||
Properties: properties,
|
Properties: properties,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(required) > 0 {
|
||||||
|
res.Required = required
|
||||||
|
}
|
||||||
|
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o object) FromAny(val any) (reflect.Value, error) {
|
func (o object) FromAny(val any) (reflect.Value, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user