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

@@ -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 {