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
|
@ -91,7 +91,7 @@ var _ = Describe("LocalAI function parse tests", func() {
|
|||
{"function": "add", "arguments": {"x": 5, "y": 3}}
|
||||
</tool_call>`
|
||||
|
||||
functionConfig.JSONRegexMatch = `(?s)<tool_call>(.*?)</tool_call>`
|
||||
functionConfig.JSONRegexMatch = []string{`(?s)<tool_call>(.*?)</tool_call>`}
|
||||
|
||||
results := ParseFunctionCall(input, functionConfig)
|
||||
Expect(results).To(HaveLen(1))
|
||||
|
@ -104,7 +104,7 @@ var _ = Describe("LocalAI function parse tests", func() {
|
|||
{"function": "add", "arguments": {"x": 5, "y": 3}}
|
||||
</tool_call>`
|
||||
|
||||
functionConfig.JSONRegexMatch = `(?s)(.*?)</tool_call>`
|
||||
functionConfig.JSONRegexMatch = []string{`(?s)(.*?)</tool_call>`}
|
||||
|
||||
results := ParseFunctionCall(input, functionConfig)
|
||||
Expect(results).To(HaveLen(1))
|
||||
|
@ -157,6 +157,41 @@ Some text before the JSON
|
|||
{'function': '"add"', 'arguments': {'x': 5, 'z': '"v"', 'y': 'v"value"'}}
|
||||
Some text after the JSON
|
||||
`
|
||||
functionConfig.JSONRegexMatch = []string{`(?s)<tool_call>(.*?)</tool_call>`}
|
||||
|
||||
// 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\""}`))
|
||||
})
|
||||
|
||||
It("should convert single-quoted key-value pairs to double-quoted and escape double quotes within values", func() {
|
||||
input := `
|
||||
Some text before the JSON
|
||||
<tool_call>{'function': '"add"', 'arguments': {'x': 5, 'z': '"v"', 'y': 'v"value"'}}</tool_call>
|
||||
Some text after the JSON
|
||||
`
|
||||
functionConfig.JSONRegexMatch = []string{`(?s)<tool_call>(.*?)</tool_call>`}
|
||||
|
||||
// Regex to match non-JSON characters before the JSON structure
|
||||
//reBefore := regexp.MustCompile(`(?s)^.*?(?=\{|\[)`)
|
||||
// Regex to match non-JSON characters after the JSON structure
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue