Skip to content

Commit 570fd90

Browse files
authored
[#genai] Added Schema option (#117)
1 parent db729d4 commit 570fd90

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

data/schema.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package data
2+
3+
// Schema used to define structure of the data
4+
// This is a subset of OpenAPI 3.0 schema
5+
type Schema struct {
6+
Id string `json:"id,omitempty" yaml:"id,omitempty"`
7+
Ref string `json:"$ref,omitempty" yaml:"$ref,omitempty"`
8+
Schema string `json:"-" yaml:"-"`
9+
Description string `json:"description,omitempty" yaml:"description,omitempty"`
10+
Type string `json:"type,omitempty" yaml:"type,omitempty"`
11+
Nullable bool `json:"nullable,omitempty" yaml:"nullable,omitempty"`
12+
Format *string `json:"format,omitempty" yaml:"format,omitempty"`
13+
Title string `json:"title,omitempty" yaml:"title,omitempty"`
14+
Default any `json:"default,omitempty" yaml:"default,omitempty"`
15+
Maximum *float64 `json:"maximum,omitempty" yaml:"maximum,omitempty"`
16+
ExclusiveMaximum *float64 `json:"exclusiveMaximum,omitempty" yaml:"exclusiveMaximum,omitempty"`
17+
Minimum *float64 `json:"minimum,omitempty" yaml:"minimum,omitempty"`
18+
ExclusiveMinimum *float64 `json:"exclusiveMinimum,omitempty" yaml:"exclusiveMinimum,omitempty"`
19+
MaxLength *int `json:"maxLength,omitempty" yaml:"maxLength,omitempty"`
20+
MinLength *int `json:"minLength,omitempty" yaml:"minLength,omitempty"`
21+
Pattern *string `json:"pattern,omitempty" yaml:"pattern,omitempty"`
22+
MaxItems *int `json:"maxItems,omitempty" yaml:"maxItems,omitempty"`
23+
MinItems *int `json:"minItems,omitempty" yaml:"minItems,omitempty"`
24+
UniqueItems bool `json:"uniqueItems,omitempty" yaml:"uniqueItems,omitempty"`
25+
MultipleOf *float64 `json:"multipleOf,omitempty" yaml:"multipleOf,omitempty"`
26+
Enum []any `json:"enum,omitempty" yaml:"enum,omitempty"`
27+
MaxProperties *int `json:"maxProperties,omitempty" yaml:"maxProperties,omitempty"`
28+
MinProperties *int `json:"minProperties,omitempty" yaml:"minProperties,omitempty"`
29+
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
30+
Items *Schema `json:"items,omitempty" yaml:"items,omitempty"`
31+
AllOf []*Schema `json:"allOf,omitempty" yaml:"allOf,omitempty"`
32+
OneOf []*Schema `json:"oneOf,omitempty" yaml:"oneOf,omitempty"`
33+
AnyOf []*Schema `json:"anyOf,omitempty" yaml:"anyOf,omitempty"`
34+
Not *Schema `json:"not,omitempty" yaml:"not,omitempty"`
35+
Properties map[string]*Schema `json:"properties,omitempty" yaml:"properties,omitempty"`
36+
AdditionalItems *Schema `json:"additionalItems,omitempty" yaml:"additionalItems,omitempty"`
37+
Xml *Xml `json:"xml,omitempty" yaml:"xml,omitempty"`
38+
ReadOnly bool `json:"readOnly,omitempty" yaml:"readOnly,omitempty"`
39+
WriteOnly bool `json:"writeOnly,omitempty" yaml:"writeOnly,omitempty"`
40+
Example any `json:"example,omitempty" yaml:"example,omitempty"`
41+
Examples []any `json:"examples,omitempty" yaml:"examples,omitempty"`
42+
Deprecated bool `json:"deprecated,omitempty" yaml:"deprecated,omitempty"`
43+
}
44+
45+
type Xml struct {
46+
Name *string `json:"name,omitempty" yaml:"name,omitempty"`
47+
Namespace *string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
48+
Prefix *string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
49+
Attribute *bool `json:"attribute,omitempty" yaml:"attribute,omitempty"`
50+
Wrapped *bool `json:"wrapped,omitempty" yaml:"wrapped,omitempty"`
51+
}

genai/provider.go

+39-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package genai
22

33
import (
4+
"oss.nandlabs.io/golly/data"
45
"oss.nandlabs.io/golly/errutils"
56
"oss.nandlabs.io/golly/managers"
67
)
@@ -22,7 +23,8 @@ const (
2223
OptionBestOf = "best_of"
2324
OptionLogProbs = "log_probs"
2425
OptionEcho = "echo"
25-
OptionOutputMimes = "output_mimes"
26+
OptionOutputMime = "output_mime"
27+
OptionSchema = "schema"
2628
)
2729

2830
var GetUnsupportedModelErr = errutils.NewCustomError("unsupported model %s")
@@ -395,12 +397,24 @@ func (o *Options) GetEcho(defaultValue bool) bool {
395397
// GetOutputMimes retrieves the "output_mimes" option from the Options.
396398
// Returns the value as a slice of strings, or the provided default value if the option does not exist.
397399
func (o *Options) GetOutputMimes(defaultValue []string) []string {
398-
if o.Has(OptionOutputMimes) {
399-
return o.GetStrings(OptionOutputMimes)
400+
if o.Has(OptionOutputMime) {
401+
return o.GetStrings(OptionOutputMime)
400402
}
401403
return defaultValue
402404
}
403405

406+
// GetSchema retrieves the "schema" option from the Options.
407+
// Returns the value as a *data.Schema, or nil if the option does not exist.
408+
func (o *Options) GetSchema() (schema *data.Schema) {
409+
if o.Has(OptionSchema) {
410+
val, ok := o.Get(OptionSchema).(*data.Schema)
411+
if ok {
412+
schema = val
413+
}
414+
}
415+
return
416+
}
417+
404418
// OptionsBuilder is a builder for the Options struct
405419

406420
type OptionsBuilder struct {
@@ -691,12 +705,31 @@ func (o *OptionsBuilder) SetEcho(echo bool) *OptionsBuilder {
691705
//
692706
// Parameters:
693707
//
694-
// outputMimes: A variable number of strings representing the desired output MIME types.
708+
// outputMimes: A string representing the desired output MIME types.
695709
//
696710
// Returns:
697711
//
698712
// *OptionsBuilder: A pointer to the updated OptionsBuilder instance.
699-
func (o *OptionsBuilder) SetOutputMimes(outputMimes ...string) *OptionsBuilder {
700-
o.options.values[OptionOutputMimes] = outputMimes
713+
func (o *OptionsBuilder) SetOutputMimes(outputMimes string) *OptionsBuilder {
714+
o.options.values[OptionOutputMime] = outputMimes
715+
return o
716+
}
717+
718+
// SetSchema sets the schema option in the OptionsBuilder.
719+
// Example usage:
720+
//
721+
// builder := &OptionsBuilder{}
722+
// schema := data.NewSchema()
723+
// builder.SetSchema(schema)
724+
//
725+
// Parameters:
726+
//
727+
// schema (data.Schema): The schema to set in the OptionsBuilder.
728+
//
729+
// Returns:
730+
//
731+
// *OptionsBuilder: The updated OptionsBuilder instance.
732+
func (o *OptionsBuilder) SetSchema(schema *data.Schema) *OptionsBuilder {
733+
o.options.values[OptionSchema] = schema
701734
return o
702735
}

0 commit comments

Comments
 (0)