mirror of
https://github.com/mudler/LocalAI.git
synced 2026-09-29 01:25:03 -04:00
* fix(functions): honor function_arguments_key when building the tool grammar
All four call sites of `Functions.ToJSONStructure(name, args string)` pass
`FunctionsConfig.FunctionNameKey` as *both* arguments, so
`FunctionArgumentsKey` never reaches the grammar generator.
`ToJSONStructure` writes the two properties into the same map:
property[nameKey] = FunctionName{Const: function.Name}
property[argsKey] = Argument{...}
When `nameKey == argsKey` the second assignment overwrites the first, so a
model configured with `function_name_key` gets a grammar carrying only the
arguments object -- the `{"const": "<function name>"}` constraint is gone and
the grammar can no longer express which function was called.
With `function_name_key: function`, the generated property set collapses from
{"function": {"const": "get_weather"}, "arguments": {...}}
to
{"function": {"type": "object", "properties": {...}}}
Setting only `function_arguments_key` is equally broken in the other
direction: the grammar keeps emitting `arguments` while `ParseFunctionCall`
(pkg/functions/parse.go) looks up the configured key, so the parsed call comes
back with its arguments empty.
The default configuration is unaffected -- with both keys empty
`ToJSONStructure` falls back to `name`/`arguments` for both parameters, which
is why this went unnoticed.
The existing `ToJSONStructure()` unit test already calls the helper with two
distinct keys, so only the call sites were wrong. Extend that test with a case
that keeps both custom keys distinct and asserts the two properties survive.
Signed-off-by: Anai-Guo <antai12232931@outlook.com>
* test(functions): cover configured grammar keys
Route grammar construction through FunctionsConfig so the regression test
covers the key wiring used by every endpoint.
Assisted-by: Codex:gpt-5
* chore: empty commit to trigger workflow approval
Signed-off-by: Tai An <antai12232931@outlook.com>
---------
Signed-off-by: Anai-Guo <antai12232931@outlook.com>
Signed-off-by: Tai An <antai12232931@outlook.com>
Co-authored-by: localai-org-maint-bot <306269227+localai-org-maint-bot@users.noreply.github.com>
110 lines
2.6 KiB
Go
110 lines
2.6 KiB
Go
package functions
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/mudler/xlog"
|
|
)
|
|
|
|
const (
|
|
defaultFunctionNameKey = "name"
|
|
defaultFunctionArgumentsKey = "arguments"
|
|
)
|
|
|
|
type Function struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Strict bool `json:"strict"`
|
|
Parameters map[string]any `json:"parameters"`
|
|
}
|
|
type Functions []Function
|
|
|
|
type FunctionName struct {
|
|
Const string `json:"const"`
|
|
}
|
|
|
|
type Argument struct {
|
|
Type string `json:"type"`
|
|
Properties map[string]any `json:"properties"`
|
|
}
|
|
|
|
type Tool struct {
|
|
Type string `json:"type"`
|
|
Function Function `json:"function,omitempty"`
|
|
}
|
|
type Tools []Tool
|
|
|
|
// ToJSONStructure converts a list of functions to a JSON structure that can be parsed to a grammar
|
|
// This allows the LLM to return a response of the type: { "name": "function_name", "arguments": { "arg1": "value1", "arg2": "value2" } }
|
|
func (f Functions) ToJSONStructure(name, args string) JSONFunctionStructure {
|
|
nameKey := defaultFunctionNameKey
|
|
argsKey := defaultFunctionArgumentsKey
|
|
if name != "" {
|
|
nameKey = name
|
|
}
|
|
if args != "" {
|
|
argsKey = args
|
|
}
|
|
js := JSONFunctionStructure{}
|
|
for _, function := range f {
|
|
// t := function.Parameters["type"]
|
|
//tt := t.(string)
|
|
|
|
properties := function.Parameters["properties"]
|
|
defs := function.Parameters["$defs"]
|
|
dat, _ := json.Marshal(properties)
|
|
dat2, _ := json.Marshal(defs)
|
|
prop := map[string]any{}
|
|
defsD := map[string]any{}
|
|
|
|
err := json.Unmarshal(dat, &prop)
|
|
if err != nil {
|
|
xlog.Error("error unmarshalling dat", "error", err)
|
|
}
|
|
err = json.Unmarshal(dat2, &defsD)
|
|
if err != nil {
|
|
xlog.Error("error unmarshalling dat2", "error", err)
|
|
}
|
|
if js.Defs == nil {
|
|
js.Defs = defsD
|
|
}
|
|
|
|
property := map[string]any{}
|
|
property[nameKey] = FunctionName{Const: function.Name}
|
|
property[argsKey] = Argument{
|
|
Type: "object",
|
|
Properties: prop,
|
|
}
|
|
js.OneOf = append(js.OneOf, Item{
|
|
Type: "object",
|
|
Properties: property,
|
|
})
|
|
/*
|
|
js.AnyOf = append(js.OneOf, Item{
|
|
Type: "object",
|
|
Properties: property,
|
|
})
|
|
*/
|
|
}
|
|
return js
|
|
}
|
|
|
|
// ToJSONStructure converts functions using the configured property keys.
|
|
func (c FunctionsConfig) ToJSONStructure(functions Functions) JSONFunctionStructure {
|
|
return functions.ToJSONStructure(c.FunctionNameKey, c.FunctionArgumentsKey)
|
|
}
|
|
|
|
// Select returns a list of functions containing the function with the given name
|
|
func (f Functions) Select(name string) Functions {
|
|
var funcs Functions
|
|
|
|
for _, f := range f {
|
|
if f.Name == name {
|
|
funcs = []Function{f}
|
|
break
|
|
}
|
|
}
|
|
|
|
return funcs
|
|
}
|