-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.go
425 lines (348 loc) · 10.2 KB
/
bot.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/lonelycode/botMaker"
"github.com/pkoukk/tiktoken-go"
"github.com/sashabaranov/go-openai"
"github.com/slack-go/slack/socketmode"
"jaytaylor.com/html2text"
)
type LurchBot struct {
Conversations map[string]*RollingWindow
SlackClient *socketmode.Client
ConversationWindow int
settings *botMaker.BotSettings
oai *botMaker.OAIClient
config *botMaker.Config
help string
promptTemplate string
instructions string
}
var SummaryTPL string = `{{.Instructions}}
user: Summarize the following content:
{{.Body}}`
func (l *LurchBot) Init(settings *Settings) {
// Set up conversation history
l.Conversations = make(map[string]*RollingWindow)
l.ConversationWindow = settings.ConversationWindow
if l.ConversationWindow == 0 {
l.ConversationWindow = 5
}
// Get the system config (API keys and Pinecone endpoint)
cfg := botMaker.NewConfigFromEnv()
l.config = cfg
// Set up the OAI API client
l.oai = botMaker.NewOAIClient(cfg.OpenAPIKey)
// Get the tuning for the bot, we'll use some defaults
l.settings = &settings.Bot
l.help = settings.Help
// If adding context (additional data outside GPTs training data),
// you can attach a memory store to query, only do so if a namespace is present
if l.settings.ID != "" {
l.settings.Memory = &botMaker.Pinecone{
APIEndpoint: cfg.PineconeEndpoint,
APIKey: cfg.PineconeKey,
}
}
l.promptTemplate = settings.template
l.instructions = settings.Instructions
}
func (l *LurchBot) Learn(history []*botMaker.RenderContext, with string) (int, error) {
// Create some storage
pc := &botMaker.Pinecone{
APIEndpoint: l.config.PineconeEndpoint,
APIKey: l.config.PineconeKey,
UUID: l.settings.ID,
}
brain := botMaker.Learn{
Model: openai.GPT3Dot5Turbo,
TokenLimit: 8191,
ChunkSize: 20,
Memory: pc,
Client: l.oai,
}
tplStr := `
{{range $i := .}}
{{$i.Role}}: {{$i.Content}}
{{end}}
`
var err error
tpl := template.New("learn-tpl")
tpl, err = tpl.Parse(tplStr)
if err != nil {
return 0, err
}
var b bytes.Buffer
err = tpl.Execute(&b, history)
if err != nil {
return 0, err
}
// We're using sentence based learning
count, err := brain.Learn(b.String(), fmt.Sprintf("conversation with %v", with), true)
if err != nil {
return 0, err
}
fmt.Printf("embeddings: %v\n", count)
err = writeToFile(filepath.Join("learn", fmt.Sprintf("conversation-with-%s", with)), b.String())
if err != nil {
return count, fmt.Errorf("saved to codex, but failed to save file: %v", err)
}
return count, nil
}
func writeToFile(filename string, text string) error {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()
if _, err = f.Stat(); err != nil {
return err
}
if _, err = f.Seek(0, io.SeekEnd); err != nil {
return err
}
if _, err = f.WriteString("===\n" + text); err != nil {
return err
}
return nil
}
func DownloadHTMLFromWebsite(url string) ([]byte, error) {
// Create a new http request with the user-agent header set to Firefox
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0")
// Send the request and get the response
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Check if status code is not 200 OK
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response status code is not 200 OK")
}
// Read the body of the response and return it
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func ConvertHTMLToText(html string) (string, error) {
text, err := html2text.FromString(html, html2text.Options{TextOnly: true})
if err != nil {
return "", err
}
return text, nil
}
func (l *LurchBot) Summarize(data string) (string, error) {
// We populate the Body with the query from the user
prompt := botMaker.NewBotPrompt(SummaryTPL, l.oai)
// Set an initial instruction to the bot
prompt.Instructions = "you are an AI copywriting assistant, you help summarize the following content into a maximum of 500 words."
prompt.Body = data
noMemSettings, _ := getSettings(os.Args[1])
noMemSettings.Bot.Memory = nil
// make the OpenAI query, the prompt object will render the query
// according to its template with the context embeddings pulled from Pinecone
resp, _, err := l.oai.CallUnifiedCompletionAPI(&noMemSettings.Bot, prompt)
if err != nil {
return fmt.Sprintf("I've encountered an error: %v", err), err
}
return resp, nil
}
func extractHyperlink(text string) (string, bool) {
// Define a regular expression that matches hyperlinks
re := regexp.MustCompile(`\bhttps?://\S+\b`)
// Find the first hyperlink in the text
hyperlink := re.FindString(text)
// Check if a hyperlink was found
if hyperlink == "" {
return "", false
}
return hyperlink, true
}
var SummarizerTokenCutoff = 4000
func CanSummarize(text string, model string) bool {
tke, err := tiktoken.EncodingForModel(model)
if err != nil {
log.Println(err)
return false
}
tkn := tke.Encode(text, nil, nil)
if len(tkn) > SummarizerTokenCutoff {
return false
}
return true
}
func (l *LurchBot) Expand(message string) (string, string) {
link, hasLink := extractHyperlink(message)
if !hasLink {
return "", ""
}
data, err := DownloadHTMLFromWebsite(link)
if err != nil {
log.Println(err)
return "", ""
}
text, err := ConvertHTMLToText(string(data))
if err != nil {
log.Println(err)
return "", ""
}
// compress that sh*t
text = strings.ReplaceAll(text, "\n\n", "")
text = strings.ReplaceAll(text, "\n", "")
text = strings.ReplaceAll(text, " ", " ")
sumText := text
if !CanSummarize(text, l.settings.Model) {
log.Println("text too long to summarize, trying shorter version")
for !CanSummarize(sumText, l.settings.Model) {
rem := float32(len(sumText)) * 0.7
if rem < 1 {
log.Println("[expand] page too large to summarize")
return link, "The website content was too large to process, tell the user that you couldn't summarize the page"
}
sumText = sumText[:int(rem)]
}
}
summary, err := l.Summarize(sumText)
if err != nil {
log.Println(err)
return link, fmt.Sprintf("couldn't summarize page: %v", err)
}
return link, summary
}
func (l *LurchBot) Chat(with, message string) (string, error) {
// Remember the response from the user
oldBody := message
_, ok := l.Conversations[with]
if !ok {
l.Conversations[with] = NewRollingWindow(l.ConversationWindow)
}
l.Conversations[with].Append(&botMaker.RenderContext{
Role: openai.ChatMessageRoleUser,
Content: oldBody,
})
if message == "reset" {
l.Conversations[with] = NewRollingWindow(l.ConversationWindow)
return "OK, I've wiped all history of our conversation", nil
}
if message == "help" {
helpMsg := l.help
if strings.HasPrefix(l.help, "file://") {
f := strings.ReplaceAll(l.help, "file://", "")
dat, err := os.ReadFile(f)
if err != nil {
return fmt.Sprint("hmm, I can't find my help response!"), nil
}
return string(dat), nil
} else if l.help == "" {
return "I am a but a simple chat-bot, here to serve.", nil
}
return helpMsg, nil
}
if strings.HasPrefix(strings.ToLower(message), "learn this:") {
h, ok := l.Conversations[with]
if !ok {
return "hmmm, I can't find who to learn from...", nil
}
count, err := l.Learn(h.Iterate(), with)
if err != nil {
return fmt.Sprintf("something went wrong with my brain: %v", err), err
}
l.Conversations[with] = NewRollingWindow(l.ConversationWindow)
return fmt.Sprintf("Saved %v items, I'll now wipe this exchange from my short term memory", count), nil
}
// pre-process the message
link, extraContext := l.Expand(message)
expanded := message
if link != "" {
if extraContext != "" {
expanded = fmt.Sprintf(
"%s\nThe link mentioned earlier contains the following content:\n%s\n", message, extraContext)
}
}
// We populate the Body with the query from the user
prompt := botMaker.NewBotPrompt(l.promptTemplate, l.oai)
// Set an initial instruction to the bot
prompt.Instructions = l.instructions
prompt.Body = expanded
history, ok := l.Conversations[with]
if !ok {
l.Conversations[with] = NewRollingWindow(l.ConversationWindow)
}
// Populate chat history for this user
prompt.History = history.Iterate()
// make the OpenAI query, the prompt object will render the query
// according to its template with the context embeddings pulled from Pinecone
resp, _, err := l.oai.CallUnifiedCompletionAPI(l.settings, prompt)
if err != nil {
return fmt.Sprintf("I've encountered an error: %v", err), err
}
// save the response from the bot
l.Conversations[with].Append(
&botMaker.RenderContext{
Role: openai.ChatMessageRoleAssistant,
Content: resp,
})
fullResponse, err := l.renderResponse(with, resp, prompt)
if err != nil {
return fmt.Sprintf("I've encountered an error: %v", err), err
}
return fullResponse, nil
}
func (l *LurchBot) renderResponse(with, resp string, prompt *botMaker.BotPrompt) (string, error) {
dat := map[string]interface{}{
"User": with,
"Response": resp,
"Titles": prompt.ContextTitles,
"Contexts": len(prompt.GetContextsForLastPrompt()),
"History": len(prompt.History),
}
var b bytes.Buffer
err := responseTemplate.Execute(&b, &dat)
if err != nil {
return "", err
}
return b.String(), nil
}
type RollingWindow struct {
size int
current int
buffer []*botMaker.RenderContext
}
func NewRollingWindow(size int) *RollingWindow {
return &RollingWindow{
size: size,
current: 0,
buffer: make([]*botMaker.RenderContext, size),
}
}
func (rw *RollingWindow) Append(obj *botMaker.RenderContext) {
rw.buffer[rw.current] = obj
rw.current = (rw.current + 1) % rw.size
}
func (rw *RollingWindow) Iterate() []*botMaker.RenderContext {
result := make([]*botMaker.RenderContext, 0, rw.size)
for i := 0; i < rw.size; i++ {
index := (rw.current + i) % rw.size
if rw.buffer[index] != nil {
result = append(result, rw.buffer[index])
}
}
return result
}