Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(functions): don't use yaml.MapSlice #2354

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/functions/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"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,14 +43,19 @@ type FunctionsConfig struct {
GrammarPrefix string `yaml:"grammar_prefix"`

// ReplaceResults allow to replace strings in the results before parsing them
ReplaceResults yaml.MapSlice `yaml:"replace_results"`
ReplaceResults []ReplaceResult `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" } }.
// This might be useful for certain models trained with the function name as the first token.
FunctionName bool `yaml:"return_name_in_function_response"`
}

type ReplaceResult struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}

type FuncCallResults struct {
Name string
Arguments string
Expand All @@ -61,7 +65,7 @@ func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncC
log.Debug().Msgf("LLM result: %s", llmresult)

for _, item := range functionConfig.ReplaceResults {
k, v := item.Key.(string), item.Value.(string)
k, v := item.Key, item.Value
log.Debug().Msgf("Replacing %s with %s", k, v)
re := regexp.MustCompile(k)
llmresult = re.ReplaceAllString(llmresult, v)
Expand Down
9 changes: 4 additions & 5 deletions pkg/functions/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 @@ -121,7 +120,7 @@ Some text before the JSON
Some text after the JSON
`

functionConfig.ReplaceResults = yaml.MapSlice{
functionConfig.ReplaceResults = []ReplaceResult{
{Key: `(?s)^[^{\[]*`, Value: ""},
{Key: `(?s)[^}\]]*$`, Value: ""},
}
Expand All @@ -138,7 +137,7 @@ 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{
functionConfig.ReplaceResults = []ReplaceResult{
{Key: `(?s)^[^{\[]*`, Value: ""},
{Key: `(?s)[^}\]]*$`, Value: ""},
}
Expand All @@ -164,7 +163,7 @@ Some text after the JSON
// Regex to match non-JSON characters after the JSON structure
//reAfter := regexp.MustCompile(`(?s)(?<=\}|\]).*$`)

functionConfig.ReplaceResults = yaml.MapSlice{
functionConfig.ReplaceResults = []ReplaceResult{
{Key: `(?s)^[^{\[]*`, Value: ""},
{Key: `(?s)[^}\]]*$`, Value: ""},
// Regex pattern to match single quotes around keys and values
Expand Down Expand Up @@ -197,7 +196,7 @@ Some text after the JSON
// Regex to match non-JSON characters after the JSON structure
//reAfter := regexp.MustCompile(`(?s)(?<=\}|\]).*$`)

functionConfig.ReplaceResults = yaml.MapSlice{
functionConfig.ReplaceResults = []ReplaceResult{
{Key: `(?s)^[^{\[]*`, Value: ""},
{Key: `(?s)[^}\]]*$`, Value: ""},
// Regex pattern to match single quotes around keys and values
Expand Down
Loading