62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package schema
|
|
|
|
import (
|
|
"errors"
|
|
"reflect"
|
|
"slices"
|
|
|
|
"github.com/google/generative-ai-go/genai"
|
|
"github.com/openai/openai-go"
|
|
)
|
|
|
|
type enum struct {
|
|
basic
|
|
|
|
values []string
|
|
}
|
|
|
|
func (e enum) FunctionParameters() openai.FunctionParameters {
|
|
return openai.FunctionParameters{
|
|
"type": "string",
|
|
"description": e.Description(),
|
|
"enum": e.values,
|
|
}
|
|
}
|
|
|
|
func (e enum) GoogleParameters() *genai.Schema {
|
|
return &genai.Schema{
|
|
Type: genai.TypeString,
|
|
Description: e.Description(),
|
|
Enum: e.values,
|
|
}
|
|
}
|
|
|
|
func (e enum) AnthropicInputSchema() map[string]any {
|
|
return map[string]any{
|
|
"type": "string",
|
|
"description": e.Description(),
|
|
"enum": e.values,
|
|
}
|
|
}
|
|
|
|
func (e enum) FromAny(val any) (reflect.Value, error) {
|
|
v := reflect.ValueOf(val)
|
|
if v.Kind() != reflect.String {
|
|
return reflect.Value{}, errors.New("expected string, got " + v.Kind().String())
|
|
}
|
|
|
|
s := v.String()
|
|
if !slices.Contains(e.values, s) {
|
|
return reflect.Value{}, errors.New("value " + s + " not in enum")
|
|
}
|
|
|
|
return v, nil
|
|
}
|
|
|
|
func (e enum) SetValueOnField(obj reflect.Value, val reflect.Value) {
|
|
if !e.required {
|
|
val = val.Addr()
|
|
}
|
|
obj.Field(e.index).Set(val)
|
|
}
|