-
Notifications
You must be signed in to change notification settings - Fork 0
/
core_actions.go
123 lines (112 loc) · 2.61 KB
/
core_actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"encoding/json"
"fmt"
"log/slog"
"time"
"github.com/tmc/langchaingo/llms"
)
var CorePlugin = Plugin{
Name: "core",
Actions: []Action{
{
FunctionDefinition: llms.FunctionDefinition{
Name: "tell.user",
Description: "Reads out a message to the user, refer to the user as sir",
Parameters: json.RawMessage(`{
"type": "object",
"profperties": {
"response": {
"type": "string",
"description": "Your response to the user request"
}
},
"required": [
"response"
]
}`),
},
execute: tellUser,
plugin: nil,
},
{
FunctionDefinition: llms.FunctionDefinition{
Name: "time.get",
Description: "Get the current time",
Parameters: json.RawMessage(`{
"type": "object",
"properties": {},
"required": [],
"returns": "The current time"
}`),
},
execute: getTime,
plugin: nil,
},
{
FunctionDefinition: llms.FunctionDefinition{
Name: "day.get",
Description: "Get the current day in the week",
Parameters: json.RawMessage(`{
"type": "object",
"properties": {},
"required": [],
"returns": "The current day, i.e. Tuesday"
}`),
},
execute: getDay,
plugin: nil,
},
{
FunctionDefinition: llms.FunctionDefinition{
Name: "date.get",
Description: "Get the current date",
Parameters: json.RawMessage(`{
"type": "object",
"properties": {},
"required": [],
"returns": "The current date in the format 'YYYY-MM-DD'"
}`),
},
execute: getDate,
plugin: nil,
},
},
}
func getCorePlugin() Plugin {
// assign plugin to CorePlugin
for i := 0; i < len(CorePlugin.Actions); i++ {
CorePlugin.Actions[i].plugin = &CorePlugin
}
return CorePlugin
}
type tellUserInput struct {
Response string `json:"response"`
}
func tellUser(input map[string]any) (string, error) {
slog.Debug("Executing action: tell.user")
// convert input to json
jsonString, err := json.Marshal(input)
if err != nil {
slog.Error("Error marshalling input", "error", err)
return "", err
}
// unmarshal json
var parsedInput tellUserInput
err = json.Unmarshal(jsonString, &parsedInput)
if err != nil {
slog.Error("Error unmarshalling input", "error", err)
return "", err
}
fmt.Println(parsedInput.Response)
return parsedInput.Response, nil
}
func getTime(_ map[string]any) (string, error) {
return time.Now().Format("15:04:05"), nil
}
func getDate(_ map[string]any) (string, error) {
return time.Now().Format("2006-01-02"), nil
}
func getDay(_ map[string]any) (string, error) {
return time.Now().Weekday().String(), nil
}