mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-20 10:35:01 +00:00
feat(functions): allow to use JSONRegexMatch unconditionally (#2349)
* feat(functions): allow to use JSONRegexMatch unconditionally Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(functions): make json_regex_match a list Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
parent
8ccd5ab040
commit
73566a2bb2
2 changed files with 50 additions and 13 deletions
|
@ -37,7 +37,7 @@ type FunctionsConfig struct {
|
|||
ResponseRegex string `yaml:"response_regex"`
|
||||
|
||||
// JSONRegexMatch is a regex to extract the JSON object from the response
|
||||
JSONRegexMatch string `yaml:"json_regex_match"`
|
||||
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
|
||||
|
@ -124,6 +124,18 @@ 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
|
||||
var respRegex = regexp.MustCompile(r)
|
||||
match := respRegex.FindStringSubmatch(llmresult)
|
||||
if len(match) >= 2 {
|
||||
llmresult = match[1]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if functionConfig.ResponseRegex != "" {
|
||||
// We use named regexes here to extract the function name and arguments
|
||||
// obviously, this expects the LLM to be stable and return correctly formatted JSON
|
||||
|
@ -143,16 +155,6 @@ func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncC
|
|||
return results
|
||||
}
|
||||
results = append(results, FuncCallResults{Name: result[functionNameKey], Arguments: result["arguments"]})
|
||||
} else if functionConfig.JSONRegexMatch != "" {
|
||||
|
||||
// We use a regex to extract the JSON object from the response
|
||||
var respRegex = regexp.MustCompile(functionConfig.JSONRegexMatch)
|
||||
match := respRegex.FindStringSubmatch(llmresult)
|
||||
if len(match) < 2 {
|
||||
return results
|
||||
}
|
||||
|
||||
results, _ = returnResult(match[1])
|
||||
} else {
|
||||
results, _ = returnResult(llmresult)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue