mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-20 10:35:01 +00:00
feat(functions): relax mixedgrammars (#2365)
* feat(functions): relax mixedgrammars Extend even more the functionalities and when mixed mode is enabled, tolerate also both strings and JSON in the result - in this case we make sure that the JSON can be correctly parsed. This also updates the examples and the gallery model to configure the grammar. The changeset also breaks current function/grammar configuration as it reserves now a stanza in the YAML config. For example: ```yaml function: grammar: # This allows the grammar to also return messages mixed_mode: true # Suffix to add to the grammar # prefix: '<tool_call>\n' # Force parallel calls in the grammar # parallel_calls: true ``` Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactor, add a way to disable mixed json and freestring Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Fix linting issues Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
parent
1542c58466
commit
491e1d752b
10 changed files with 469 additions and 149 deletions
|
@ -8,6 +8,26 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type GrammarConfig struct {
|
||||
// ParallelCalls enables the LLM to return multiple function calls in the same response
|
||||
ParallelCalls bool `yaml:"parallel_calls"`
|
||||
|
||||
// MixedMode enables the LLM to return strings and not only JSON objects
|
||||
// This is useful for models to not constraing returning only JSON and also messages back to the user
|
||||
MixedMode bool `yaml:"mixed_mode"`
|
||||
|
||||
// NoMixedFreeString disables the mixed mode for free strings
|
||||
// In this way if the LLM selects a free string, it won't be mixed necessarly with JSON objects
|
||||
NoMixedFreeString bool `yaml:"no_mixed_free_string"`
|
||||
|
||||
// NoGrammar disables the grammar parsing and parses the responses directly from the LLM
|
||||
NoGrammar bool `yaml:"disable"`
|
||||
|
||||
// Prefix is the suffix to append to the grammar when being generated
|
||||
// This is useful when models prepend a tag before returning JSON
|
||||
Prefix string `yaml:"prefix"`
|
||||
}
|
||||
|
||||
// FunctionsConfig is the configuration for the tool/function call.
|
||||
// It includes setting to map the function name and arguments from the response
|
||||
// and, for instance, also if processing the requests with BNF grammars.
|
||||
|
@ -16,34 +36,26 @@ type FunctionsConfig struct {
|
|||
// By default we inject a tool that does nothing and is used to return an answer from the LLM
|
||||
DisableNoAction bool `yaml:"disable_no_action"`
|
||||
|
||||
// Grammar is the configuration for the grammar
|
||||
GrammarConfig GrammarConfig `yaml:"grammar"`
|
||||
|
||||
// NoActionFunctionName is the name of the function that does nothing. It defaults to "answer"
|
||||
NoActionFunctionName string `yaml:"no_action_function_name"`
|
||||
|
||||
// NoActionDescriptionName is the name of the function that returns the description of the no action function
|
||||
NoActionDescriptionName string `yaml:"no_action_description_name"`
|
||||
|
||||
// ParallelCalls enables the LLM to return multiple function calls in the same response
|
||||
ParallelCalls bool `yaml:"parallel_calls"`
|
||||
|
||||
// GrammarMessage enables the LLM to return strings and not only JSON objects
|
||||
// This is useful for models to not constraing returning only JSON and also messages back to the user
|
||||
GrammarMessage bool `yaml:"grammar_message"`
|
||||
|
||||
// NoGrammar disables the grammar parsing and parses the responses directly from the LLM
|
||||
NoGrammar bool `yaml:"no_grammar"`
|
||||
|
||||
// ResponseRegex is a named regex to extract the function name and arguments from the response
|
||||
ResponseRegex string `yaml:"response_regex"`
|
||||
|
||||
// JSONRegexMatch is a regex to extract the JSON object from the response
|
||||
JSONRegexMatch []string `yaml:"json_regex_match"`
|
||||
|
||||
// GrammarPrefix is the suffix to append to the grammar when being generated
|
||||
// This is useful when models prepend a tag before returning JSON
|
||||
GrammarPrefix string `yaml:"grammar_prefix"`
|
||||
// ReplaceFunctionResults allow to replace strings in the results before parsing them
|
||||
ReplaceFunctionResults []ReplaceResult `yaml:"replace_function_results"`
|
||||
|
||||
// ReplaceResults allow to replace strings in the results before parsing them
|
||||
ReplaceResults []ReplaceResult `yaml:"replace_results"`
|
||||
// ReplaceLLMResult allow to replace strings in the results before parsing them
|
||||
ReplaceLLMResult []ReplaceResult `yaml:"replace_llm_results"`
|
||||
|
||||
// FunctionName enable the LLM to return { "name": "function_name", "arguments": { "arg1": "value1", "arg2": "value2" } }
|
||||
// instead of { "function": "function_name", "arguments": { "arg1": "value1", "arg2": "value2" } }.
|
||||
|
@ -61,18 +73,49 @@ type FuncCallResults struct {
|
|||
Arguments string
|
||||
}
|
||||
|
||||
func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncCallResults {
|
||||
func (g GrammarConfig) Options() []func(o *GrammarOption) {
|
||||
opts := []func(o *GrammarOption){}
|
||||
if g.MixedMode {
|
||||
opts = append(opts, EnableMaybeString)
|
||||
}
|
||||
if g.ParallelCalls {
|
||||
opts = append(opts, EnableMaybeArray)
|
||||
}
|
||||
if g.Prefix != "" {
|
||||
opts = append(opts, SetPrefix(g.Prefix))
|
||||
}
|
||||
if g.NoMixedFreeString {
|
||||
opts = append(opts, NoMixedFreeString)
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
func CleanupLLMResult(llmresult string, functionConfig FunctionsConfig) string {
|
||||
log.Debug().Msgf("LLM result: %s", llmresult)
|
||||
|
||||
for _, item := range functionConfig.ReplaceResults {
|
||||
for _, item := range functionConfig.ReplaceLLMResult {
|
||||
k, v := item.Key, item.Value
|
||||
log.Debug().Msgf("Replacing %s with %s", k, v)
|
||||
re := regexp.MustCompile(k)
|
||||
llmresult = re.ReplaceAllString(llmresult, v)
|
||||
}
|
||||
|
||||
log.Debug().Msgf("LLM result(processed): %s", llmresult)
|
||||
|
||||
return llmresult
|
||||
}
|
||||
|
||||
func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncCallResults {
|
||||
|
||||
log.Debug().Msgf("LLM result: %s", llmresult)
|
||||
|
||||
for _, item := range functionConfig.ReplaceFunctionResults {
|
||||
k, v := item.Key, item.Value
|
||||
log.Debug().Msgf("Replacing %s with %s", k, v)
|
||||
re := regexp.MustCompile(k)
|
||||
llmresult = re.ReplaceAllString(llmresult, v)
|
||||
}
|
||||
log.Debug().Msgf("LLM result(function cleanup): %s", llmresult)
|
||||
|
||||
functionNameKey := "function"
|
||||
if functionConfig.FunctionName {
|
||||
functionNameKey = "name"
|
||||
|
@ -127,7 +170,6 @@ func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncC
|
|||
|
||||
// the response is a string that we have to parse
|
||||
result := make(map[string]string)
|
||||
|
||||
if len(functionConfig.JSONRegexMatch) != 0 {
|
||||
for _, r := range functionConfig.JSONRegexMatch {
|
||||
// We use a regex to extract the JSON object from the response
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue