Add support for integers and tool configuration in schema handling

This update introduces support for `jsonschema.Integer` types and updates the logic to handle nested items in schemas. Added a new default error log for unknown types using `slog.Error`. Also, integrated tool configuration with a `FunctionCallingConfig` when `dontRequireTool` is false.
This commit is contained in:
2025-04-06 01:23:10 -04:00
parent ff5e4ca7b0
commit 7c9eb08cb4
13 changed files with 267 additions and 96 deletions

View File

@@ -28,22 +28,27 @@ func getFromType(t reflect.Type, b basic) Type {
switch t.Kind() {
case reflect.String:
b.DataType = jsonschema.String
b.typeName = "string"
return b
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
b.DataType = jsonschema.Integer
b.typeName = "integer"
return b
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
b.DataType = jsonschema.Integer
b.typeName = "integer"
return b
case reflect.Float32, reflect.Float64:
b.DataType = jsonschema.Number
b.typeName = "number"
return b
case reflect.Bool:
b.DataType = jsonschema.Boolean
b.typeName = "boolean"
return b
case reflect.Struct:
@@ -107,7 +112,7 @@ func getObject(t reflect.Type) object {
}
return object{
basic: basic{DataType: jsonschema.Object},
basic: basic{DataType: jsonschema.Object, typeName: "object"},
fields: fields,
}
}
@@ -116,6 +121,7 @@ func getArray(t reflect.Type) array {
res := array{
basic: basic{
DataType: jsonschema.Array,
typeName: "array",
},
}

View File

@@ -2,6 +2,7 @@ package schema
import (
"errors"
"github.com/openai/openai-go"
"reflect"
"github.com/sashabaranov/go-openai/jsonschema"
@@ -18,6 +19,14 @@ func (a array) SchemaType() jsonschema.DataType {
return jsonschema.Array
}
func (a array) FunctionParameters() openai.FunctionParameters {
return openai.FunctionParameters{
"type": "array",
"description": a.Description(),
"items": a.items.FunctionParameters(),
}
}
func (a array) Definition() jsonschema.Definition {
def := a.basic.Definition()
def.Type = jsonschema.Array

View File

@@ -2,6 +2,7 @@ package schema
import (
"errors"
"github.com/openai/openai-go"
"reflect"
"strconv"
@@ -13,6 +14,7 @@ var _ Type = basic{}
type basic struct {
jsonschema.DataType
typeName string
// index is the position of the parameter in the StructField of the function's parameter struct
index int
@@ -29,6 +31,13 @@ func (b basic) SchemaType() jsonschema.DataType {
return b.DataType
}
func (b basic) FunctionParameters() openai.FunctionParameters {
return openai.FunctionParameters{
"type": b.typeName,
"description": b.description,
}
}
func (b basic) Definition() jsonschema.Definition {
return jsonschema.Definition{
Type: b.DataType,

View File

@@ -2,6 +2,7 @@ package schema
import (
"errors"
"github.com/openai/openai-go"
"reflect"
"golang.org/x/exp/slices"
@@ -19,6 +20,14 @@ func (e enum) SchemaType() jsonschema.DataType {
return jsonschema.String
}
func (e enum) FunctionParameters() openai.FunctionParameters {
return openai.FunctionParameters{
"type": "string",
"description": e.Description(),
"enum": e.values,
}
}
func (e enum) Definition() jsonschema.Definition {
def := e.basic.Definition()
def.Enum = e.values

View File

@@ -2,6 +2,7 @@ package schema
import (
"errors"
"github.com/openai/openai-go"
"reflect"
"github.com/sashabaranov/go-openai/jsonschema"
@@ -19,6 +20,19 @@ func (o object) SchemaType() jsonschema.DataType {
return jsonschema.Object
}
func (o object) FunctionParameters() openai.FunctionParameters {
var properties = map[string]openai.FunctionParameters{}
for k, v := range o.fields {
properties[k] = v.FunctionParameters()
}
return openai.FunctionParameters{
"type": "object",
"description": o.Description(),
"properties": properties,
}
}
func (o object) Definition() jsonschema.Definition {
def := o.basic.Definition()
def.Type = jsonschema.Object

View File

@@ -1,12 +1,15 @@
package schema
import (
"github.com/openai/openai-go"
"reflect"
"github.com/sashabaranov/go-openai/jsonschema"
)
type Type interface {
FunctionParameters() openai.FunctionParameters
SchemaType() jsonschema.DataType
Definition() jsonschema.Definition