2024-04-18 20:43:12 +00:00
|
|
|
package functions_test
|
|
|
|
|
|
|
|
import (
|
2024-06-23 08:24:36 +00:00
|
|
|
. "github.com/mudler/LocalAI/pkg/functions"
|
2024-04-18 20:43:12 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("LocalAI function parse tests", func() {
|
|
|
|
var functionConfig FunctionsConfig
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
// Default configuration setup
|
2024-05-18 07:35:28 +00:00
|
|
|
functionConfig = FunctionsConfig{}
|
2024-04-18 20:43:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("when using grammars and single result expected", func() {
|
|
|
|
It("should parse the function name and arguments correctly", func() {
|
2024-07-18 15:52:22 +00:00
|
|
|
input := `{"name": "add", "arguments": {"x": 5, "y": 3}}`
|
2024-04-18 20:43:12 +00:00
|
|
|
|
|
|
|
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}`))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("when not using grammars and regex is needed", func() {
|
|
|
|
It("should extract function name and arguments from the regex", func() {
|
|
|
|
input := `add({"x":5,"y":3})`
|
2024-07-18 15:52:22 +00:00
|
|
|
functionConfig.ResponseRegex = []string{`(?P<name>\w+)\s*\((?P<arguments>.*)\)`}
|
2024-04-18 20:43:12 +00:00
|
|
|
|
|
|
|
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}`))
|
|
|
|
})
|
2024-07-18 15:52:22 +00:00
|
|
|
It("should extract function name and arguments from the regex", func() {
|
|
|
|
input := `add({"x":5,"y":3})`
|
|
|
|
functionConfig.ResponseRegex = []string{`(?P<function>\w+)\s*\((?P<arguments>.*)\)`}
|
|
|
|
functionConfig.FunctionNameKey = "function"
|
|
|
|
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}`))
|
|
|
|
})
|
2024-04-18 20:43:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Context("when having invalid input", func() {
|
|
|
|
It("returns no results when there is no input", func() {
|
|
|
|
input := ""
|
|
|
|
results := ParseFunctionCall(input, functionConfig)
|
|
|
|
Expect(results).To(HaveLen(0))
|
|
|
|
})
|
|
|
|
It("returns no results when is invalid", func() {
|
|
|
|
input := "invalid input"
|
|
|
|
|
|
|
|
results := ParseFunctionCall(input, functionConfig)
|
|
|
|
Expect(results).To(HaveLen(0))
|
|
|
|
})
|
|
|
|
})
|
2024-05-18 23:29:10 +00:00
|
|
|
|
2024-04-18 20:43:12 +00:00
|
|
|
Context("when parallel calls are enabled", func() {
|
|
|
|
It("should handle multiple function calls", func() {
|
2024-07-18 15:52:22 +00:00
|
|
|
input := `[{"name": "add", "arguments": {"x": 5, "y": 3}}, {"name": "subtract", "arguments": {"x": 10, "y": 7}}]`
|
2024-04-18 20:43:12 +00:00
|
|
|
|
|
|
|
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}`))
|
|
|
|
})
|
|
|
|
})
|
2024-05-13 22:32:32 +00:00
|
|
|
|
|
|
|
Context("without grammars and without regex", func() {
|
|
|
|
It("should parse the function name and arguments correctly with the name key", func() {
|
2024-07-18 15:52:22 +00:00
|
|
|
input := `{"function": "add", "arguments": {"x": 5, "y": 3}}`
|
|
|
|
functionConfig.FunctionNameKey = "function"
|
2024-05-13 22:32:32 +00:00
|
|
|
|
|
|
|
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 parse the function name and arguments correctly with the function key", func() {
|
2024-07-18 15:52:22 +00:00
|
|
|
input := `{"name": "add", "arguments": {"x": 5, "y": 3}}`
|
2024-05-14 07:39:20 +00:00
|
|
|
|
|
|
|
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}`))
|
|
|
|
})
|
|
|
|
|
2024-05-18 23:29:10 +00:00
|
|
|
It("should parse the result by matching the JSONRegexMatch", func() {
|
2024-05-14 07:39:20 +00:00
|
|
|
input := `
|
|
|
|
<tool_call>
|
2024-07-18 15:52:22 +00:00
|
|
|
{"name": "add", "arguments": {"x": 5, "y": 3}}
|
2024-05-14 07:39:20 +00:00
|
|
|
</tool_call>`
|
2024-05-18 07:35:28 +00:00
|
|
|
|
2024-05-19 16:24:49 +00:00
|
|
|
functionConfig.JSONRegexMatch = []string{`(?s)<tool_call>(.*?)</tool_call>`}
|
2024-05-14 07:39:20 +00:00
|
|
|
|
|
|
|
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}`))
|
|
|
|
})
|
|
|
|
|
2024-05-18 23:29:10 +00:00
|
|
|
It("should parse the result by matching the JSONRegexMatch", func() {
|
2024-05-14 07:39:20 +00:00
|
|
|
input := `
|
2024-07-18 15:52:22 +00:00
|
|
|
{"name": "add", "arguments": {"x": 5, "y": 3}}
|
2024-05-14 07:39:20 +00:00
|
|
|
</tool_call>`
|
2024-05-18 07:35:28 +00:00
|
|
|
|
2024-05-19 16:24:49 +00:00
|
|
|
functionConfig.JSONRegexMatch = []string{`(?s)(.*?)</tool_call>`}
|
2024-05-13 22:32:32 +00:00
|
|
|
|
|
|
|
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}`))
|
|
|
|
})
|
2024-07-18 15:52:22 +00:00
|
|
|
|
|
|
|
It("should parse the result even with invalid JSON", func() {
|
|
|
|
input := `{"name": "add", "arguments": {"x": 5, "y": 3}} invalid {"name": "add", "arguments": {"x": 5, "y": 3}}`
|
|
|
|
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}`))
|
|
|
|
})
|
2024-05-13 22:32:32 +00:00
|
|
|
})
|
2024-05-18 23:29:10 +00:00
|
|
|
|
|
|
|
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
|
2024-07-18 15:52:22 +00:00
|
|
|
{"name": "add", "arguments": {"x": 5, "y": 3}}
|
2024-05-18 23:29:10 +00:00
|
|
|
Some text after the JSON
|
|
|
|
`
|
|
|
|
|
2024-05-21 22:14:16 +00:00
|
|
|
functionConfig.ReplaceFunctionResults = []ReplaceResult{
|
2024-05-18 23:29:10 +00:00
|
|
|
{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
|
2024-07-18 15:52:22 +00:00
|
|
|
[{"name": "add", "arguments": {"x": 5, "y": 3}}, {"name": "subtract", "arguments": {"x": 10, "y": 7}}]
|
2024-05-18 23:29:10 +00:00
|
|
|
Some text after the JSON
|
|
|
|
`
|
2024-05-21 22:14:16 +00:00
|
|
|
functionConfig.ReplaceFunctionResults = []ReplaceResult{
|
2024-05-18 23:29:10 +00:00
|
|
|
{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
|
2024-07-18 15:52:22 +00:00
|
|
|
{'name': '"add"', 'arguments': {'x': 5, 'z': '"v"', 'y': 'v"value"'}}
|
2024-05-18 23:29:10 +00:00
|
|
|
Some text after the JSON
|
|
|
|
`
|
2024-05-19 16:24:49 +00:00
|
|
|
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)(?<=\}|\]).*$`)
|
|
|
|
|
2024-05-21 22:14:16 +00:00
|
|
|
functionConfig.ReplaceFunctionResults = []ReplaceResult{
|
2024-05-19 16:24:49 +00:00
|
|
|
{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
|
2024-07-18 15:52:22 +00:00
|
|
|
<tool_call>{'name': '"add"', 'arguments': {'x': 5, 'z': '"v"', 'y': 'v"value"'}}</tool_call>
|
2024-05-19 16:24:49 +00:00
|
|
|
Some text after the JSON
|
|
|
|
`
|
|
|
|
functionConfig.JSONRegexMatch = []string{`(?s)<tool_call>(.*?)</tool_call>`}
|
|
|
|
|
2024-05-18 23:29:10 +00:00
|
|
|
// 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)(?<=\}|\]).*$`)
|
|
|
|
|
2024-05-21 22:14:16 +00:00
|
|
|
functionConfig.ReplaceFunctionResults = []ReplaceResult{
|
2024-05-18 23:29:10 +00:00
|
|
|
{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\""}`))
|
|
|
|
})
|
2024-05-28 19:06:09 +00:00
|
|
|
|
|
|
|
It("should detect multiple functions call where the JSONRegexMatch is repeated", func() {
|
|
|
|
input := `
|
|
|
|
Some text before the JSON
|
2024-07-18 15:52:22 +00:00
|
|
|
<tool_call>{"name": "add", "arguments": {"x": 5, "y": 3}}</tool_call>
|
|
|
|
<tool_call>{"name": "subtract", "arguments": {"x": 10, "y": 7}}</tool_call>
|
2024-05-28 19:06:09 +00:00
|
|
|
Some text after the JSON
|
|
|
|
`
|
|
|
|
functionConfig.JSONRegexMatch = []string{`(?s)<tool_call>(.*?)</tool_call>`}
|
|
|
|
|
|
|
|
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}`))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
Context("ParseTextContent", func() {
|
|
|
|
It("Can extract notes from the LLM result", func() {
|
|
|
|
input := `
|
|
|
|
Some text before the JSON
|
|
|
|
<sketchpad>
|
|
|
|
roses are red
|
|
|
|
</sketchpad>
|
2024-07-18 15:52:22 +00:00
|
|
|
<tool_call>{"name": "subtract", "arguments": {"x": 10, "y": 7}}</tool_call>
|
2024-05-28 19:06:09 +00:00
|
|
|
Some text after the JSON
|
|
|
|
`
|
|
|
|
functionConfig.CaptureLLMResult = []string{`(?s)<sketchpad>(.*?)</sketchpad>`}
|
|
|
|
results := ParseTextContent(input, functionConfig)
|
|
|
|
Expect(results).To(Equal("roses are red"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Defaults to empty if doesn't catch any", func() {
|
|
|
|
input := `
|
|
|
|
Some text before the JSON
|
2024-07-18 15:52:22 +00:00
|
|
|
<tool_call>{"name": "subtract", "arguments": {"x": 10, "y": 7}}</tool_call>
|
2024-05-28 19:06:09 +00:00
|
|
|
Some text after the JSON
|
|
|
|
`
|
|
|
|
functionConfig.CaptureLLMResult = []string{`(?s)<sketchpad>(.*?)</sketchpad>`}
|
|
|
|
results := ParseTextContent(input, functionConfig)
|
|
|
|
Expect(results).To(Equal(""))
|
|
|
|
})
|
2024-05-18 23:29:10 +00:00
|
|
|
})
|
2024-07-18 15:52:22 +00:00
|
|
|
Context("ParseJSON - when given valid JSON strings", func() {
|
|
|
|
It("should parse multiple JSON objects", func() {
|
|
|
|
input := `{"key1": "value1"} {"key2": "value2"}`
|
|
|
|
expected := []map[string]any{
|
|
|
|
{"key1": "value1"},
|
|
|
|
{"key2": "value2"},
|
|
|
|
}
|
|
|
|
result, err := ParseJSON(input)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(expected))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should parse a single JSON object with various types", func() {
|
|
|
|
input := `{"key1": "value1", "key2": 2}`
|
|
|
|
expected := []map[string]any{
|
|
|
|
{"key1": "value1", "key2": float64(2)},
|
|
|
|
}
|
|
|
|
result, err := ParseJSON(input)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(expected))
|
|
|
|
})
|
|
|
|
It("should handle JSON without syntax errors gracefully", func() {
|
|
|
|
input := `{"key1": "value1"}`
|
|
|
|
expected := []map[string]any{
|
|
|
|
{"key1": "value1"},
|
|
|
|
}
|
|
|
|
result, err := ParseJSON(input)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(expected))
|
|
|
|
})
|
|
|
|
It("should handle JSON without syntax errors gracefully", func() {
|
|
|
|
input := `[{"key1": "value1"}]`
|
|
|
|
expected := []map[string]any{
|
|
|
|
{"key1": "value1"},
|
|
|
|
}
|
|
|
|
result, err := ParseJSON(input)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(expected))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("ParseJSON - when given invalid JSON strings", func() {
|
|
|
|
It("should return an error for completely invalid JSON", func() {
|
|
|
|
input := `invalid json`
|
|
|
|
result, err := ParseJSON(input)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(result).To(BeNil())
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should skip invalid JSON parts and parse valid parts", func() {
|
|
|
|
input := `{"key1": "value1"} invalid {"key2": "value2"}`
|
|
|
|
expected := []map[string]any{
|
|
|
|
{"key1": "value1"},
|
|
|
|
{"key2": "value2"},
|
|
|
|
}
|
|
|
|
result, err := ParseJSON(input)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(expected))
|
|
|
|
})
|
|
|
|
|
|
|
|
PIt("should handle JSON with syntax errors gracefully", func() {
|
|
|
|
input := `{"key1": "value1", "key2": }`
|
|
|
|
expected := []map[string]any{
|
|
|
|
{"key1": "value1"},
|
|
|
|
}
|
|
|
|
result, err := ParseJSON(input)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(expected))
|
|
|
|
})
|
|
|
|
})
|
2024-04-18 20:43:12 +00:00
|
|
|
})
|