mirror of
https://github.com/mudler/LocalAI.git
synced 2025-02-11 13:15:20 +00:00
refactor: function argument parsing using named regex (#4708)
Signed-off-by: Maximilian Kenfenheuer <maximilian.kenfenheuer@ksol.it>
This commit is contained in:
parent
91e1ff5a95
commit
b4b67e00bd
@ -331,37 +331,36 @@ func ParseFunctionCall(llmresult string, functionConfig FunctionsConfig) []FuncC
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ParseFunctionCallArgs(functionArguments string, functionConfig FunctionsConfig) string {
|
func ParseFunctionCallArgs(functionArguments string, functionConfig FunctionsConfig) string {
|
||||||
if len(functionConfig.ArgumentRegex) > 0 {
|
if len(functionConfig.ArgumentRegex) == 0 {
|
||||||
// We use named regexes here to extract the function argument key value pairs and convert this to valid json.
|
|
||||||
// TODO: there might be responses where an object as a value is expected/required. This is currently not handled.
|
|
||||||
args := make(map[string]string)
|
|
||||||
|
|
||||||
agrsRegexKeyName := "key"
|
|
||||||
agrsRegexValueName := "value"
|
|
||||||
|
|
||||||
if functionConfig.ArgumentRegexKey != "" {
|
|
||||||
agrsRegexKeyName = functionConfig.ArgumentRegexKey
|
|
||||||
}
|
|
||||||
if functionConfig.ArgumentRegexValue != "" {
|
|
||||||
agrsRegexValueName = functionConfig.ArgumentRegexValue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, r := range functionConfig.ArgumentRegex {
|
|
||||||
var respRegex = regexp.MustCompile(r)
|
|
||||||
var nameRange []string = respRegex.SubexpNames()
|
|
||||||
var keyIndex = slices.Index(nameRange, agrsRegexKeyName)
|
|
||||||
var valueIndex = slices.Index(nameRange, agrsRegexValueName)
|
|
||||||
matches := respRegex.FindAllStringSubmatch(functionArguments, -1)
|
|
||||||
for _, match := range matches {
|
|
||||||
args[match[keyIndex]] = match[valueIndex]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonBytes, _ := json.Marshal(args)
|
|
||||||
jsonString := string(jsonBytes)
|
|
||||||
|
|
||||||
return jsonString
|
|
||||||
} else {
|
|
||||||
return functionArguments
|
return functionArguments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We use named regexes here to extract the function argument key value pairs and convert this to valid json.
|
||||||
|
// TODO: there might be responses where an object as a value is expected/required. This is currently not handled.
|
||||||
|
args := make(map[string]string)
|
||||||
|
|
||||||
|
agrsRegexKeyName := "key"
|
||||||
|
agrsRegexValueName := "value"
|
||||||
|
|
||||||
|
if functionConfig.ArgumentRegexKey != "" {
|
||||||
|
agrsRegexKeyName = functionConfig.ArgumentRegexKey
|
||||||
|
}
|
||||||
|
if functionConfig.ArgumentRegexValue != "" {
|
||||||
|
agrsRegexValueName = functionConfig.ArgumentRegexValue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range functionConfig.ArgumentRegex {
|
||||||
|
var respRegex = regexp.MustCompile(r)
|
||||||
|
var nameRange []string = respRegex.SubexpNames()
|
||||||
|
var keyIndex = slices.Index(nameRange, agrsRegexKeyName)
|
||||||
|
var valueIndex = slices.Index(nameRange, agrsRegexValueName)
|
||||||
|
matches := respRegex.FindAllStringSubmatch(functionArguments, -1)
|
||||||
|
for _, match := range matches {
|
||||||
|
args[match[keyIndex]] = match[valueIndex]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonBytes, _ := json.Marshal(args)
|
||||||
|
|
||||||
|
return string(jsonBytes)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user