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" } }
(name, args string)
| 37 | // ToJSONStructure converts a list of functions to a JSON structure that can be parsed to a grammar |
| 38 | // This allows the LLM to return a response of the type: { "name": "function_name", "arguments": { "arg1": "value1", "arg2": "value2" } } |
| 39 | func (f Functions) ToJSONStructure(name, args string) JSONFunctionStructure { |
| 40 | nameKey := defaultFunctionNameKey |
| 41 | argsKey := defaultFunctionArgumentsKey |
| 42 | if name != "" { |
| 43 | nameKey = name |
| 44 | } |
| 45 | if args != "" { |
| 46 | argsKey = args |
| 47 | } |
| 48 | js := JSONFunctionStructure{} |
| 49 | for _, function := range f { |
| 50 | // t := function.Parameters["type"] |
| 51 | //tt := t.(string) |
| 52 | |
| 53 | properties := function.Parameters["properties"] |
| 54 | defs := function.Parameters["$defs"] |
| 55 | dat, _ := json.Marshal(properties) |
| 56 | dat2, _ := json.Marshal(defs) |
| 57 | prop := map[string]any{} |
| 58 | defsD := map[string]any{} |
| 59 | |
| 60 | err := json.Unmarshal(dat, &prop) |
| 61 | if err != nil { |
| 62 | xlog.Error("error unmarshalling dat", "error", err) |
| 63 | } |
| 64 | err = json.Unmarshal(dat2, &defsD) |
| 65 | if err != nil { |
| 66 | xlog.Error("error unmarshalling dat2", "error", err) |
| 67 | } |
| 68 | if js.Defs == nil { |
| 69 | js.Defs = defsD |
| 70 | } |
| 71 | |
| 72 | property := map[string]any{} |
| 73 | property[nameKey] = FunctionName{Const: function.Name} |
| 74 | property[argsKey] = Argument{ |
| 75 | Type: "object", |
| 76 | Properties: prop, |
| 77 | } |
| 78 | js.OneOf = append(js.OneOf, Item{ |
| 79 | Type: "object", |
| 80 | Properties: property, |
| 81 | }) |
| 82 | /* |
| 83 | js.AnyOf = append(js.OneOf, Item{ |
| 84 | Type: "object", |
| 85 | Properties: property, |
| 86 | }) |
| 87 | */ |
| 88 | } |
| 89 | return js |
| 90 | } |
| 91 | |
| 92 | // Select returns a list of functions containing the function with the given name |
| 93 | func (f Functions) Select(name string) Functions { |
no test coverage detected