feat(functions): Enable true regex replacement for the regexReplacement option (#2341)

* Adding regex capabilities to ParseFunctionCall replacement

Signed-off-by: Lenaxia <github@47north.lat>

* Adding tests for the regex replace in ParseFunctionCall

Signed-off-by: Lenaxia <github@47north.lat>

* Fixing tests and adding a test case to validate double quote replacement works

Signed-off-by: Lenaxia <github@47north.lat>

* Make Regex replacement stable, drop lookaheads

Signed-off-by: mudler <mudler@localai.io>

---------

Signed-off-by: Lenaxia <github@47north.lat>
Signed-off-by: mudler <mudler@localai.io>
Co-authored-by: Lenaxia <github@47north.lat>
Co-authored-by: mudler <mudler@localai.io>
This commit is contained in:
lenaxia 2024-05-18 16:29:10 -07:00 committed by GitHub
parent 5f35e85e86
commit 6b6c8cdd5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 80 additions and 6 deletions

View file

@ -3,10 +3,10 @@ package functions
import (
"encoding/json"
"regexp"
"strings"
"github.com/go-skynet/LocalAI/pkg/utils"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v2"
)
// FunctionsConfig is the configuration for the tool/function call.
@ -44,7 +44,7 @@ type FunctionsConfig struct {
GrammarPrefix string `yaml:"grammar_prefix"`
// ReplaceResults allow to replace strings in the results before parsing them
ReplaceResults map[string]string `yaml:"replace_results"`
ReplaceResults yaml.MapSlice `yaml:"replace_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" } }.
@ -60,9 +60,11 @@ type FuncCallResults struct {
func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncCallResults {
log.Debug().Msgf("LLM result: %s", llmresult)
for k, v := range functionConfig.ReplaceResults {
for _, item := range functionConfig.ReplaceResults {
k, v := item.Key.(string), item.Value.(string)
log.Debug().Msgf("Replacing %s with %s", k, v)
llmresult = strings.ReplaceAll(llmresult, k, v)
re := regexp.MustCompile(k)
llmresult = re.ReplaceAllString(llmresult, v)
}
log.Debug().Msgf("LLM result(processed): %s", llmresult)