-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer.go
150 lines (122 loc) · 3.24 KB
/
consumer.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
package main
import (
"log"
"strings"
"time"
llama "github.com/go-skynet/go-llama.cpp"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var stopButton = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Stop", "/stop"),
),
)
func ProcessQueue() {
for {
task, err := qu.Dequeue()
currentTask = task
if err == ErrQueueEmpty {
time.Sleep(time.Second * 2)
continue
}
ProcessTask(task)
}
}
type Result struct {
Text string
Err error
}
func Predict(task *Task) (chan string, chan Result) {
stream := make(chan string)
result := make(chan Result)
go func(){
callback := func(token string) bool {
select {
case stream <- token:
return true
case <- task.Stop:
return false
}
}
text, err := l.Predict(
task.Question,
llama.Debug,
llama.SetTokenCallback(callback),
llama.SetTokens(nTokens),
llama.SetThreads(nCpu),
llama.SetTopK(90),
llama.SetTopP(0.86),
llama.SetStopWords(StopWord),
)
close(stream)
result <- Result{text, err}
}()
return stream, result
}
// This function is a mess
func ProcessTask(task *Task) {
log.Printf("Start processing task from user %d\n", task.UserID)
log.Printf("The prompt is:\n%s\n", task.Question)
// Start prediction
stream, result := Predict(task)
// Resulting generated text
var answer string
var counter int
var issent bool
for {
select {
case token := <- stream:
if !issent && strings.TrimSpace(token) != "" {
answer += token
msg := tgbotapi.NewMessage(task.UserID, answer)
msg.ReplyMarkup = &stopButton
sent, err := bot.Send(msg)
if err != nil {
log.Println("[ProcessTask] error sending answer:", err)
continue
}
// Save answer message ID to stream tokens to it
task.MessageID = sent.MessageID
issent = true
continue
}
answer += token
counter++
if counter == 6 {
edited := tgbotapi.NewEditMessageText(task.UserID, task.MessageID, answer)
edited.ReplyMarkup = &stopButton
_, err := bot.Send(edited)
if err != nil {
log.Println("[ProcessTask] error streaming answer:", err)
}
counter = 0
}
case prediction := <- result:
delete := tgbotapi.NewDeleteMessage(task.UserID, task.AnnounceID)
_, err := bot.Request(delete)
if err != nil {
log.Println("Couldn't delete announce message:", err)
}
if prediction.Err != nil || strings.TrimSpace(prediction.Text) == "" || task.MessageID == 0 {
log.Println("[ProcessTask] prediction error:", prediction.Err, prediction.Text)
failure := tgbotapi.NewMessage(task.UserID, "Sorry, couldn't generate answer")
_, err := bot.Send(failure)
if err != nil {
log.Println("[ProcessTask] error sending failure message:", err)
}
return
}
edited := tgbotapi.NewEditMessageText(task.UserID, task.MessageID, prediction.Text)
// Set parse mode to Markdown if it's backticks there
if nBackticks := strings.Count(prediction.Text, "`"); nBackticks > 0 && nBackticks % 2 == 0 {
edited.ParseMode = "Markdown"
}
_, err = bot.Send(edited)
if err != nil {
log.Println("[ProcessTask] error sending answer:", err)
}
log.Printf("Generated answer is:\n%s\n", prediction.Text)
return
}
}
}