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

@@ -4,10 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"github.com/google/generative-ai-go/genai"
"github.com/sashabaranov/go-openai/jsonschema"
"google.golang.org/api/option"
)
@@ -25,62 +23,15 @@ func (g google) ModelVersion(modelVersion string) (ChatCompletion, error) {
func (g google) requestToChatHistory(in Request, model *genai.GenerativeModel) (*genai.GenerativeModel, *genai.ChatSession, []genai.Part) {
res := *model
var openAiSchemaToGenAISchema func(in jsonschema.Definition) *genai.Schema
openAiSchemaToGenAISchema = func(in jsonschema.Definition) *genai.Schema {
res := genai.Schema{}
switch in.Type {
case jsonschema.Object:
res.Type = genai.TypeObject
case jsonschema.Array:
res.Type = genai.TypeArray
case jsonschema.String:
res.Type = genai.TypeString
case jsonschema.Integer:
res.Type = genai.TypeInteger
case jsonschema.Number:
res.Type = genai.TypeNumber
case jsonschema.Boolean:
res.Type = genai.TypeBoolean
default:
res.Type = genai.TypeUnspecified
slog.Error("unknown type in go_llm/google.go!requestToChatHistory", "type", in.Type)
}
if in.Items != nil {
res.Items = openAiSchemaToGenAISchema(*in.Items)
}
res.Description = in.Description
res.Enum = in.Enum
res.Required = in.Required
if in.Properties != nil {
res.Properties = map[string]*genai.Schema{}
for k, v := range in.Properties {
res.Properties[k] = openAiSchemaToGenAISchema(v)
}
}
return &res
}
if in.Toolbox != nil {
for _, tool := range in.Toolbox.funcs {
def := tool.Parameters.Definition()
res.Tools = append(res.Tools, &genai.Tool{
FunctionDeclarations: []*genai.FunctionDeclaration{
{
Name: tool.Name,
Description: tool.Description,
Parameters: openAiSchemaToGenAISchema(def),
Parameters: tool.Parameters.GoogleParameters(),
},
},
})