mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-20 10:35:01 +00:00
feat(functions): parse broken JSON when we parse the raw results, use dynamic rules for grammar keys (#2912)
* feat(functions): enhance parsing with broken JSON when we parse the raw results Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * breaking: make function name by default Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * feat(grammar): dynamically generate grammars with mutating keys Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * refactor: simplify condition Signed-off-by: Ettore Di Giacinto <mudler@localai.io> * Update docs Signed-off-by: Ettore Di Giacinto <mudler@localai.io> --------- Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
parent
35d55572ac
commit
bf9dd1de7f
9 changed files with 279 additions and 202 deletions
|
@ -6,6 +6,11 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultFunctionNameKey = "name"
|
||||
defaultFunctionArgumentsKey = "arguments"
|
||||
)
|
||||
|
||||
type Function struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
|
@ -19,50 +24,18 @@ type Tool struct {
|
|||
}
|
||||
type Tools []Tool
|
||||
|
||||
// ToJSONFunctionStructure 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: { "function": "function_name", "arguments": { "arg1": "value1", "arg2": "value2" } }
|
||||
func (f Functions) ToJSONFunctionStructure() JSONFunctionStructureFunction {
|
||||
js := JSONFunctionStructureFunction{}
|
||||
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]interface{}{}
|
||||
defsD := map[string]interface{}{}
|
||||
|
||||
err := json.Unmarshal(dat, &prop)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error unmarshalling dat")
|
||||
}
|
||||
err = json.Unmarshal(dat2, &defsD)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error unmarshalling dat2")
|
||||
}
|
||||
if js.Defs == nil {
|
||||
js.Defs = defsD
|
||||
}
|
||||
js.OneOf = append(js.OneOf, ItemFunction{
|
||||
Type: "object",
|
||||
Properties: FunctionProperties{
|
||||
Function: FunctionName{Const: function.Name},
|
||||
Arguments: Argument{
|
||||
Type: "object",
|
||||
Properties: prop,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
return js
|
||||
}
|
||||
|
||||
// ToJSONNameStructure 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) ToJSONNameStructure() JSONFunctionStructureName {
|
||||
js := JSONFunctionStructureName{}
|
||||
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)
|
||||
|
@ -85,15 +58,16 @@ func (f Functions) ToJSONNameStructure() JSONFunctionStructureName {
|
|||
if js.Defs == nil {
|
||||
js.Defs = defsD
|
||||
}
|
||||
js.OneOf = append(js.OneOf, ItemName{
|
||||
Type: "object",
|
||||
Properties: NameProperties{
|
||||
Function: FunctionName{Const: function.Name},
|
||||
Arguments: Argument{
|
||||
Type: "object",
|
||||
Properties: prop,
|
||||
},
|
||||
},
|
||||
|
||||
property := map[string]interface{}{}
|
||||
property[nameKey] = FunctionName{Const: function.Name}
|
||||
property[argsKey] = Argument{
|
||||
Type: "object",
|
||||
Properties: prop,
|
||||
}
|
||||
js.OneOf = append(js.OneOf, Item{
|
||||
Type: "object",
|
||||
Properties: property,
|
||||
})
|
||||
}
|
||||
return js
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue