Skip to content

Commit

Permalink
feat(functions): Enable true regex replacement for the regexReplaceme…
Browse files Browse the repository at this point in the history
…nt option (mudler#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>
  • Loading branch information
3 people authored May 18, 2024
1 parent 5f35e85 commit 6b6c8cd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
10 changes: 6 additions & 4 deletions pkg/functions/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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" } }.
Expand All @@ -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)
Expand Down
76 changes: 74 additions & 2 deletions pkg/functions/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
. "github.com/go-skynet/LocalAI/pkg/functions"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gopkg.in/yaml.v2"
)

var _ = Describe("LocalAI function parse tests", func() {
Expand Down Expand Up @@ -50,6 +51,7 @@ var _ = Describe("LocalAI function parse tests", func() {
Expect(results).To(HaveLen(0))
})
})

Context("when parallel calls are enabled", func() {
It("should handle multiple function calls", func() {
input := `[{"function": "add", "arguments": {"x": 5, "y": 3}}, {"function": "subtract", "arguments": {"x": 10, "y": 7}}]`
Expand Down Expand Up @@ -83,7 +85,7 @@ var _ = Describe("LocalAI function parse tests", func() {
Expect(results[0].Arguments).To(Equal(`{"x":5,"y":3}`))
})

It("Should parse the result by matching the JSONRegexMatch", func() {
It("should parse the result by matching the JSONRegexMatch", func() {
input := `
<tool_call>
{"function": "add", "arguments": {"x": 5, "y": 3}}
Expand All @@ -97,7 +99,7 @@ var _ = Describe("LocalAI function parse tests", func() {
Expect(results[0].Arguments).To(Equal(`{"x":5,"y":3}`))
})

It("Should parse the result by matching the JSONRegexMatch", func() {
It("should parse the result by matching the JSONRegexMatch", func() {
input := `
{"function": "add", "arguments": {"x": 5, "y": 3}}
</tool_call>`
Expand All @@ -110,4 +112,74 @@ var _ = Describe("LocalAI function parse tests", func() {
Expect(results[0].Arguments).To(Equal(`{"x":5,"y":3}`))
})
})

Context("when using ReplaceResults to clean up input", func() {
It("should replace text before and after JSON blob", func() {
input := `
Some text before the JSON
{"function": "add", "arguments": {"x": 5, "y": 3}}
Some text after the JSON
`

functionConfig.ReplaceResults = yaml.MapSlice{
{Key: `(?s)^[^{\[]*`, Value: ""},
{Key: `(?s)[^}\]]*$`, Value: ""},
}

results := ParseFunctionCall(input, functionConfig)
Expect(results).To(HaveLen(1))
Expect(results[0].Name).To(Equal("add"))
Expect(results[0].Arguments).To(Equal(`{"x":5,"y":3}`))
})

It("should replace text before and after array JSON blob", func() {
input := `
Some text before the JSON
[{"function": "add", "arguments": {"x": 5, "y": 3}}, {"function": "subtract", "arguments": {"x": 10, "y": 7}}]
Some text after the JSON
`
functionConfig.ReplaceResults = yaml.MapSlice{
{Key: `(?s)^[^{\[]*`, Value: ""},
{Key: `(?s)[^}\]]*$`, Value: ""},
}

results := ParseFunctionCall(input, functionConfig)
Expect(results).To(HaveLen(2))
Expect(results[0].Name).To(Equal("add"))
Expect(results[0].Arguments).To(Equal(`{"x":5,"y":3}`))
Expect(results[1].Name).To(Equal("subtract"))
Expect(results[1].Arguments).To(Equal(`{"x":10,"y":7}`))
})

It("should convert single-quoted key-value pairs to double-quoted and escape double quotes within values", func() {
input := `
Some text before the JSON
{'function': '"add"', 'arguments': {'x': 5, 'z': '"v"', 'y': 'v"value"'}}
Some text after the JSON
`
// Regex to match non-JSON characters before the JSON structure
//reBefore := regexp.MustCompile(`(?s)^.*?(?=\{|\[)`)
// Regex to match non-JSON characters after the JSON structure
//reAfter := regexp.MustCompile(`(?s)(?<=\}|\]).*$`)

functionConfig.ReplaceResults = yaml.MapSlice{
{Key: `(?s)^[^{\[]*`, Value: ""},
{Key: `(?s)[^}\]]*$`, Value: ""},
// Regex pattern to match single quotes around keys and values
// Step 1: Replace single quotes around keys and values with double quotes
{Key: `'([^']*?)'`, Value: `_DQUOTE_${1}_DQUOTE_`},
// Step 2: Replace double quotes inside values with placeholders
{Key: `\\"`, Value: `__TEMP_QUOTE__`},
{Key: `"`, Value: `\"`},
{Key: `\'`, Value: `'`},
{Key: `_DQUOTE_`, Value: `"`},
{Key: `__TEMP_QUOTE__`, Value: `"`},
}

results := ParseFunctionCall(input, functionConfig)
Expect(results).To(HaveLen(1))
Expect(results[0].Name).To(Equal("\"add\""))
Expect(results[0].Arguments).To(Equal(`{"x":5,"y":"v\"value\"","z":"\"v\""}`))
})
})
})

0 comments on commit 6b6c8cd

Please sign in to comment.