Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(functions): mixed JSON BNF grammars #2328

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/http/endpoints/openai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,15 @@ func ChatEndpoint(cl *config.BackendConfigLoader, ml *model.ModelLoader, startup
// Handle if we should return "name" instead of "functions"
if config.FunctionsConfig.FunctionName {
jsStruct := funcs.ToJSONNameStructure()
config.Grammar = jsStruct.Grammar("", config.FunctionsConfig.ParallelCalls)
config.Grammar = jsStruct.Grammar(config.FunctionsConfig.GrammarPrefix, "", config.FunctionsConfig.ParallelCalls, config.FunctionsConfig.GrammarMessage)
} else {
jsStruct := funcs.ToJSONFunctionStructure()
config.Grammar = jsStruct.Grammar("", config.FunctionsConfig.ParallelCalls)
config.Grammar = jsStruct.Grammar(config.FunctionsConfig.GrammarPrefix, "", config.FunctionsConfig.ParallelCalls, config.FunctionsConfig.GrammarMessage)
}
case input.JSONFunctionGrammarObject != nil:
config.Grammar = input.JSONFunctionGrammarObject.Grammar("", config.FunctionsConfig.ParallelCalls)
config.Grammar = input.JSONFunctionGrammarObject.Grammar(config.FunctionsConfig.GrammarPrefix, "", config.FunctionsConfig.ParallelCalls, config.FunctionsConfig.GrammarMessage)
case input.JSONFunctionGrammarObjectName != nil:
config.Grammar = input.JSONFunctionGrammarObjectName.Grammar("", config.FunctionsConfig.ParallelCalls)
config.Grammar = input.JSONFunctionGrammarObjectName.Grammar(config.FunctionsConfig.GrammarPrefix, "", config.FunctionsConfig.ParallelCalls, config.FunctionsConfig.GrammarMessage)
default:
// Force picking one of the functions by the request
if config.FunctionToCall() != "" {
Expand Down
63 changes: 51 additions & 12 deletions pkg/functions/grammar_json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"regexp"
"sort"
"strings"

"github.com/go-skynet/LocalAI/pkg/utils"
)

const (
Expand Down Expand Up @@ -48,6 +50,10 @@ var (
[^"\\] |
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
)* "\"" space`,
"freestring": `(
[^"\\] |
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
)* space`,
"null": `"null" space`,
}

Expand Down Expand Up @@ -111,22 +117,54 @@ const array = `arr ::=
(",\n" realvalue)*
)? "]"`

func (sc *JSONSchemaConverter) finalizeGrammar(maybeArray bool) string {
func (sc *JSONSchemaConverter) finalizeGrammar(suffix string, maybeArray, maybeString bool) string {
var lines []string

swapRoot := maybeArray || maybeString || suffix != ""

// write down the computed rules.
// if maybeArray is true, we need to add the array rule and slightly tweak the root rule
for name, rule := range sc.rules {
if maybeArray && name == "root" {
if swapRoot && name == "root" {
name = "realvalue"
}
lines = append(lines, fmt.Sprintf("%s ::= %s", name, rule))
}

if !swapRoot {
return strings.Join(lines, "\n")
}

newRoot := "realvalue"
if maybeArray {
lines = append(lines, fmt.Sprintf("%s ::= %s", "root", "arr | realvalue"))
lines = append(lines, array)
newRoot = "arr | realvalue"
}

if suffix != "" {
// quote newlines in suffix
suffix = utils.EscapeNewLines(suffix)

if maybeArray && maybeString {
newRoot = "(" + newRoot + ")"
}

if maybeString {
//newRoot = "( (\"" + suffix + "\" " + newRoot + ") | freestring ) "
newRoot = "( \"" + suffix + "\" " + newRoot + " | freestring ) "
} else {
newRoot = "\"" + suffix + "\" " + "" + newRoot + ""
}
} else if maybeString {
if maybeArray {
// newRoot = "(" + newRoot + ")"
}

newRoot = "freestring | " + newRoot
}

lines = append(lines, fmt.Sprintf("%s ::= %s", "root", newRoot))
lines = append(lines, array)

return strings.Join(lines, "\n")
}

Expand Down Expand Up @@ -251,15 +289,16 @@ func (sc *JSONSchemaConverter) resolveReference(ref string, rootSchema map[strin

return def
}
func (sc *JSONSchemaConverter) Grammar(schema map[string]interface{}, maybeArray bool) string {
func (sc *JSONSchemaConverter) Grammar(suffix string, schema map[string]interface{}, maybeArray, maybeString bool) string {
sc.addRule("freestring", PRIMITIVE_RULES["freestring"])
sc.visit(schema, "", schema)
return sc.finalizeGrammar(maybeArray)
return sc.finalizeGrammar(suffix, maybeArray, maybeString)
}

func (sc *JSONSchemaConverter) GrammarFromBytes(b []byte, maybeArray bool) string {
func (sc *JSONSchemaConverter) GrammarFromBytes(suffix string, b []byte, maybeArray, maybeString bool) string {
var schema map[string]interface{}
_ = json.Unmarshal(b, &schema)
return sc.Grammar(schema, maybeArray)
return sc.Grammar(suffix, schema, maybeArray, maybeString)
}

func jsonString(v interface{}) string {
Expand Down Expand Up @@ -302,9 +341,9 @@ type JSONFunctionStructureName struct {
Defs map[string]interface{} `json:"$defs,omitempty"`
}

func (j JSONFunctionStructureName) Grammar(propOrder string, maybeArray bool) string {
func (j JSONFunctionStructureName) Grammar(suffix string, propOrder string, maybeArray, maybeString bool) string {
dat, _ := json.Marshal(j)
return NewJSONSchemaConverter(propOrder).GrammarFromBytes(dat, maybeArray)
return NewJSONSchemaConverter(propOrder).GrammarFromBytes(suffix, dat, maybeArray, maybeString)
}

type JSONFunctionStructureFunction struct {
Expand All @@ -313,7 +352,7 @@ type JSONFunctionStructureFunction struct {
Defs map[string]interface{} `json:"$defs,omitempty"`
}

func (j JSONFunctionStructureFunction) Grammar(propOrder string, maybeArray bool) string {
func (j JSONFunctionStructureFunction) Grammar(suffix string, propOrder string, maybeArray, maybeString bool) string {
dat, _ := json.Marshal(j)
return NewJSONSchemaConverter(propOrder).GrammarFromBytes(dat, maybeArray)
return NewJSONSchemaConverter(propOrder).GrammarFromBytes(suffix, dat, maybeArray, maybeString)
}
Loading