instead of having an openai => google translation layer, just add sister functions to the types that construct the google request just like openai's

This commit is contained in:
2025-04-07 01:57:02 -04:00
parent 58552ee226
commit 5ba0d5df7e
11 changed files with 175 additions and 232 deletions

View File

@@ -3,8 +3,6 @@ package schema
import (
"reflect"
"strings"
"github.com/sashabaranov/go-openai/jsonschema"
)
// GetType will, given an interface{} that is a struct (NOT a pointer to a struct), return the Type of the struct that
@@ -27,27 +25,27 @@ func getFromType(t reflect.Type, b basic) Type {
switch t.Kind() {
case reflect.String:
b.DataType = jsonschema.String
b.DataType = String
b.typeName = "string"
return b
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
b.DataType = jsonschema.Integer
b.DataType = Integer
b.typeName = "integer"
return b
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
b.DataType = jsonschema.Integer
b.DataType = Integer
b.typeName = "integer"
return b
case reflect.Float32, reflect.Float64:
b.DataType = jsonschema.Number
b.DataType = Number
b.typeName = "number"
return b
case reflect.Bool:
b.DataType = jsonschema.Boolean
b.DataType = Boolean
b.typeName = "boolean"
return b
@@ -94,7 +92,7 @@ func getField(f reflect.StructField, index int) Type {
}
}
b.DataType = jsonschema.String
b.DataType = String
b.typeName = "string"
return enum{
basic: b,
@@ -114,7 +112,7 @@ func getObject(t reflect.Type) object {
}
return object{
basic: basic{DataType: jsonschema.Object, typeName: "object"},
basic: basic{DataType: Object, typeName: "object"},
fields: fields,
}
}
@@ -122,7 +120,7 @@ func getObject(t reflect.Type) object {
func getArray(t reflect.Type) array {
res := array{
basic: basic{
DataType: jsonschema.Array,
DataType: Array,
typeName: "array",
},
}

View File

@@ -2,10 +2,10 @@ package schema
import (
"errors"
"github.com/openai/openai-go"
"reflect"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/google/generative-ai-go/genai"
"github.com/openai/openai-go"
)
type array struct {
@@ -15,25 +15,20 @@ type array struct {
items Type
}
func (a array) SchemaType() jsonschema.DataType {
return jsonschema.Array
}
func (a array) FunctionParameters() openai.FunctionParameters {
func (a array) OpenAIParameters() openai.FunctionParameters {
return openai.FunctionParameters{
"type": "array",
"description": a.Description(),
"items": a.items.FunctionParameters(),
"items": a.items.OpenAIParameters(),
}
}
func (a array) Definition() jsonschema.Definition {
def := a.basic.Definition()
def.Type = jsonschema.Array
i := a.items.Definition()
def.Items = &i
def.AdditionalProperties = false
return def
func (a array) GoogleParameters() *genai.Schema {
return &genai.Schema{
Type: genai.TypeArray,
Description: a.Description(),
Items: a.items.GoogleParameters(),
}
}
func (a array) FromAny(val any) (reflect.Value, error) {

View File

@@ -2,18 +2,29 @@ package schema
import (
"errors"
"github.com/openai/openai-go"
"reflect"
"strconv"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/google/generative-ai-go/genai"
"github.com/openai/openai-go"
)
// just enforcing that basic implements Type
var _ Type = basic{}
type DataType string
const (
String DataType = "string"
Integer DataType = "integer"
Number DataType = "number"
Boolean DataType = "boolean"
Object DataType = "object"
Array DataType = "array"
)
type basic struct {
jsonschema.DataType
DataType
typeName string
// index is the position of the parameter in the StructField of the function's parameter struct
@@ -27,20 +38,34 @@ type basic struct {
description string
}
func (b basic) SchemaType() jsonschema.DataType {
return b.DataType
}
func (b basic) FunctionParameters() openai.FunctionParameters {
func (b basic) OpenAIParameters() openai.FunctionParameters {
return openai.FunctionParameters{
"type": b.typeName,
"description": b.description,
}
}
func (b basic) Definition() jsonschema.Definition {
return jsonschema.Definition{
Type: b.DataType,
func (b basic) GoogleParameters() *genai.Schema {
var t = genai.TypeUnspecified
switch b.DataType {
case String:
t = genai.TypeString
case Integer:
t = genai.TypeInteger
case Number:
t = genai.TypeNumber
case Boolean:
t = genai.TypeBoolean
case Object:
t = genai.TypeObject
case Array:
t = genai.TypeArray
default:
t = genai.TypeUnspecified
}
return &genai.Schema{
Type: t,
Description: b.description,
}
}
@@ -57,12 +82,12 @@ func (b basic) FromAny(val any) (reflect.Value, error) {
v := reflect.ValueOf(val)
switch b.DataType {
case jsonschema.String:
case String:
var val = v.String()
return reflect.ValueOf(val), nil
case jsonschema.Integer:
case Integer:
if v.Kind() == reflect.Float64 {
return v.Convert(reflect.TypeOf(int(0))), nil
} else if v.Kind() != reflect.Int {
@@ -71,7 +96,7 @@ func (b basic) FromAny(val any) (reflect.Value, error) {
return v, nil
}
case jsonschema.Number:
case Number:
if v.Kind() == reflect.Float64 {
return v.Convert(reflect.TypeOf(float64(0))), nil
} else if v.Kind() != reflect.Float64 {
@@ -80,7 +105,7 @@ func (b basic) FromAny(val any) (reflect.Value, error) {
return v, nil
}
case jsonschema.Boolean:
case Boolean:
if v.Kind() == reflect.Bool {
return v, nil
} else if v.Kind() == reflect.String {

View File

@@ -2,12 +2,11 @@ package schema
import (
"errors"
"github.com/openai/openai-go"
"reflect"
"slices"
"golang.org/x/exp/slices"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/google/generative-ai-go/genai"
"github.com/openai/openai-go"
)
type enum struct {
@@ -16,10 +15,6 @@ type enum struct {
values []string
}
func (e enum) SchemaType() jsonschema.DataType {
return jsonschema.String
}
func (e enum) FunctionParameters() openai.FunctionParameters {
return openai.FunctionParameters{
"type": "string",
@@ -28,10 +23,12 @@ func (e enum) FunctionParameters() openai.FunctionParameters {
}
}
func (e enum) Definition() jsonschema.Definition {
def := e.basic.Definition()
def.Enum = e.values
return def
func (e enum) GoogleParameters() *genai.Schema {
return &genai.Schema{
Type: genai.TypeString,
Description: e.Description(),
Enum: e.values,
}
}
func (e enum) FromAny(val any) (reflect.Value, error) {

View File

@@ -2,10 +2,10 @@ package schema
import (
"errors"
"github.com/openai/openai-go"
"reflect"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/google/generative-ai-go/genai"
"github.com/openai/openai-go"
)
type object struct {
@@ -16,14 +16,10 @@ type object struct {
fields map[string]Type
}
func (o object) SchemaType() jsonschema.DataType {
return jsonschema.Object
}
func (o object) FunctionParameters() openai.FunctionParameters {
func (o object) OpenAIParameters() openai.FunctionParameters {
var properties = map[string]openai.FunctionParameters{}
for k, v := range o.fields {
properties[k] = v.FunctionParameters()
properties[k] = v.OpenAIParameters()
}
return openai.FunctionParameters{
@@ -33,16 +29,17 @@ func (o object) FunctionParameters() openai.FunctionParameters {
}
}
func (o object) Definition() jsonschema.Definition {
def := o.basic.Definition()
def.Type = jsonschema.Object
def.Properties = make(map[string]jsonschema.Definition)
func (o object) GoogleParameters() *genai.Schema {
var properties = map[string]*genai.Schema{}
for k, v := range o.fields {
def.Properties[k] = v.Definition()
properties[k] = v.GoogleParameters()
}
def.AdditionalProperties = false
return def
return &genai.Schema{
Type: genai.TypeObject,
Description: o.Description(),
Properties: properties,
}
}
func (o object) FromAny(val any) (reflect.Value, error) {

View File

@@ -1,17 +1,18 @@
package schema
import (
"github.com/openai/openai-go"
"reflect"
"github.com/sashabaranov/go-openai/jsonschema"
"github.com/google/generative-ai-go/genai"
"github.com/openai/openai-go"
)
type Type interface {
FunctionParameters() openai.FunctionParameters
OpenAIParameters() openai.FunctionParameters
GoogleParameters() *genai.Schema
SchemaType() jsonschema.DataType
Definition() jsonschema.Definition
//SchemaType() jsonschema.DataType
//Definition() jsonschema.Definition
Required() bool
Description() string