2024-07-25 06:41:00 +00:00
|
|
|
package functions
|
|
|
|
|
2024-07-26 18:11:29 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/mudler/LocalAI/pkg/functions/grammars"
|
|
|
|
)
|
2024-07-25 06:41:00 +00:00
|
|
|
|
|
|
|
type Item struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Properties map[string]interface{} `json:"properties"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type JSONFunctionStructure struct {
|
|
|
|
OneOf []Item `json:"oneOf,omitempty"`
|
|
|
|
AnyOf []Item `json:"anyOf,omitempty"`
|
|
|
|
Defs map[string]interface{} `json:"$defs,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-07-26 18:11:29 +00:00
|
|
|
func (j JSONFunctionStructure) Grammar(options ...func(*grammars.GrammarOption)) (string, error) {
|
|
|
|
grammarOpts := &grammars.GrammarOption{}
|
2024-07-25 06:41:00 +00:00
|
|
|
grammarOpts.Apply(options...)
|
|
|
|
|
|
|
|
dat, err := json.Marshal(j)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-07-26 18:11:29 +00:00
|
|
|
|
|
|
|
converter := NewSchemaConverter(*grammarOpts)
|
|
|
|
return converter.GrammarFromBytes(dat, options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SchemaConverter interface {
|
|
|
|
GrammarFromBytes([]byte, ...func(*grammars.GrammarOption)) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSchemaConverter(opt grammars.GrammarOption) SchemaConverter {
|
|
|
|
switch {
|
|
|
|
case opt.SchemaType == grammars.LLama31Schema:
|
|
|
|
return grammars.NewLLama31SchemaConverter(opt.FunctionName)
|
|
|
|
}
|
|
|
|
return grammars.NewJSONSchemaConverter(opt.PropOrder)
|
2024-07-25 06:41:00 +00:00
|
|
|
}
|