-
Notifications
You must be signed in to change notification settings - Fork 1
/
guifunction.go
193 lines (161 loc) · 4.84 KB
/
guifunction.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
// create, edit, and make function calls
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"strconv"
"strings"
"github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/jsonschema"
)
func (agent *Agent) hfunction(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
type Funcdef struct {
Name string
Description string
}
var data struct {
Currentfunctions []Funcdef
Savedfunctions []string
}
for _, item := range agent.req.Functions {
newfunc := Funcdef{
Name: item.Name,
Description: item.Description,
}
data.Currentfunctions = append(data.Currentfunctions, newfunc)
}
data.Savedfunctions, _ = getsavefilelist("Functions")
render(w, hfunctionpage, data)
}
if r.Method == http.MethodPost {
functionname := r.FormValue("functionname")
functiondescription := r.FormValue("functiondescription")
parameters := r.FormValue("edittext")
var jsonData map[string]interface{}
_ = json.Unmarshal([]byte(parameters), &jsonData)
newfunction := openai.FunctionDefinition{
Name: functionname,
Description: functiondescription,
Parameters: jsonData,
}
agent.setfunction(newfunction)
r.Method = http.MethodGet
agent.hfunction(w, r)
}
query := strings.TrimPrefix(r.URL.Path, "/function/")
if r.Method == http.MethodPatch {
if query == "" {
f := openai.FunctionDefinition{
Name: "New",
Description: "New",
Parameters: jsonschema.Definition{
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"Variable1": {
Type: jsonschema.Object,
Properties: map[string]jsonschema.Definition{
"Variable2": {
Type: jsonschema.String,
Description: "Description of variable",
},
},
},
"Variable3": {
Type: jsonschema.String,
Enum: []string{"Choice1", "Choice2"},
},
},
Required: []string{"Variable1", "Variable3"},
},
}
agent.hfunctionedit(w, r, f)
} else {
for _, function := range agent.req.Functions {
if query == function.Name {
agent.hfunctionedit(w, r, function)
continue
}
}
}
}
if r.Method == http.MethodDelete {
agent.removefunction(query)
r.Method = http.MethodGet
agent.hfunction(w, r)
}
}
func (agent *Agent) hfunctiondata(w http.ResponseWriter, r *http.Request) {
query := strings.TrimPrefix(r.URL.Path, "/function/data/")
if r.Method == http.MethodGet {
functionname := query
newfunction, err := agent.functionload(functionname)
if err != nil {
fmt.Println(err)
}
agent.setfunction(newfunction)
agent.hfunction(w, r)
}
if r.Method == http.MethodPost {
newfunction := openai.FunctionDefinition{
Name: r.FormValue("functionname"),
Description: r.FormValue("functiondescription"),
Parameters: map[string]interface{}{},
}
edittext := r.FormValue("edittext")
edittext = strings.ReplaceAll(edittext, "\n", "")
edittext = strings.ReplaceAll(edittext, " ", "")
var jsonData map[string]interface{}
err := json.Unmarshal([]byte(edittext), &jsonData)
if err != nil {
fmt.Println("Error:", err)
}
newfunction.Parameters = jsonData
agent.savefile(newfunction, "Functions", newfunction.Name)
r.Method = http.MethodGet
agent.hfunction(w, r)
}
if r.Method == http.MethodDelete {
functionname := query
deletefile("Functions", functionname)
agent.hfunction(w, r)
}
}
func (agent *Agent) hfunctionedit(w http.ResponseWriter, r *http.Request, f openai.FunctionDefinition) {
data := openai.FunctionDefinition{}
functiondata, err := json.MarshalIndent(f.Parameters, "", " ")
if err != nil {
fmt.Println("Error:", err)
}
data.Name = f.Name
data.Description = f.Description
data.Parameters = string(functiondata)
render(w, hfunctioneditpage, data)
}
func (agent *Agent) hfunctionrun(w http.ResponseWriter, r *http.Request) {
rawquery := strings.TrimPrefix(r.URL.Path, "/function/run/")
query := strings.Split(rawquery, "/")
fmt.Println(rawquery, query)
function := Response{
FunctionCall: &openai.FunctionCall{
Name: query[0],
Arguments: agent.req.Messages[len(agent.req.Messages)-1].Content,
},
}
response := agent.callfunction(&function)
w.Header().Set("HX-Trigger-After-Settle", `tokenupdate`)
var data struct {
Header template.HTML
Role string
Content string
Index string
Function string
}
data.Header = template.HTML(`<div id="message" class="message" style="background-color: #393939">`)
data.Role = openai.ChatMessageRoleAssistant
data.Content = response.Message.Content
data.Index = strconv.Itoa(len(agent.req.Messages) - 1)
render(w, hchatnewpage, data)
}