2024-04-18 20:43:12 +00:00
|
|
|
package functions
|
2023-07-02 09:13:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-04-29 13:11:42 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2023-07-02 09:13:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Function struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Parameters map[string]interface{} `json:"parameters"`
|
|
|
|
}
|
|
|
|
type Functions []Function
|
|
|
|
|
2024-02-17 09:00:34 +00:00
|
|
|
type Tool struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Function Function `json:"function,omitempty"`
|
|
|
|
}
|
|
|
|
type Tools []Tool
|
|
|
|
|
2024-05-11 23:13:22 +00:00
|
|
|
// 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{}
|
2023-07-02 09:13:51 +00:00
|
|
|
for _, function := range f {
|
|
|
|
// t := function.Parameters["type"]
|
|
|
|
//tt := t.(string)
|
|
|
|
|
|
|
|
properties := function.Parameters["properties"]
|
2023-07-19 20:56:13 +00:00
|
|
|
defs := function.Parameters["$defs"]
|
2023-07-02 09:13:51 +00:00
|
|
|
dat, _ := json.Marshal(properties)
|
2023-07-19 20:56:13 +00:00
|
|
|
dat2, _ := json.Marshal(defs)
|
2023-07-02 09:13:51 +00:00
|
|
|
prop := map[string]interface{}{}
|
2023-07-19 20:56:13 +00:00
|
|
|
defsD := map[string]interface{}{}
|
|
|
|
|
2024-04-29 13:11:42 +00:00
|
|
|
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")
|
|
|
|
}
|
2023-07-19 20:56:13 +00:00
|
|
|
if js.Defs == nil {
|
|
|
|
js.Defs = defsD
|
|
|
|
}
|
2024-05-11 23:13:22 +00:00
|
|
|
js.OneOf = append(js.OneOf, ItemFunction{
|
2023-07-02 09:13:51 +00:00
|
|
|
Type: "object",
|
2024-05-11 23:13:22 +00:00
|
|
|
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{}
|
|
|
|
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, ItemName{
|
|
|
|
Type: "object",
|
|
|
|
Properties: NameProperties{
|
2023-07-02 09:13:51 +00:00
|
|
|
Function: FunctionName{Const: function.Name},
|
|
|
|
Arguments: Argument{
|
|
|
|
Type: "object",
|
|
|
|
Properties: prop,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return js
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|