-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
98 lines (79 loc) · 1.98 KB
/
main.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
package main
import (
"log"
"os"
"runtime"
"strconv"
llama "github.com/go-skynet/go-llama.cpp"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
var apiToken = os.Getenv("TG_TOKEN")
var modelPath = os.Getenv("MODEL_PATH")
var nTokens int
var nCpu int
var SingleMessagePrompt string
var ReplyMessagePrompt string
var StopWord = os.Getenv("STOP_WORD")
var l *llama.LLama
var bot *tgbotapi.BotAPI
var qu *TaskQueue
var currentTask *Task
func main() {
var err error
if apiToken == "" || modelPath == "" {
log.Fatalln("Please provide TG_TOKEN and MODEL_PATH env variables")
}
// Init queue
var queueSize = 1000
if s := os.Getenv("Q_SIZE"); s != "" {
if n, err := strconv.Atoi(s); err == nil {
queueSize = n
}
}
qu = NewTaskQueue(queueSize)
// N tokens
nTokens = 1000
if s := os.Getenv("N_TOKENS"); s != "" {
if n, err := strconv.Atoi(s); err == nil {
nTokens = n
}
}
// N cores
nCpu = runtime.NumCPU()
if s := os.Getenv("N_CPU"); s != "" {
if n, err := strconv.Atoi(s); err == nil {
nCpu = n
}
}
// Init Prompt templates
SingleMessagePrompt = os.Getenv("SINGLE_MESSAGE_PROMPT")
ReplyMessagePrompt = os.Getenv("REPLY_MESSAGE_PROMPT")
if SingleMessagePrompt == "" {
SingleMessagePrompt = "### User: Response to my next request. %s ### Assistant:"
}
if ReplyMessagePrompt == "" {
ReplyMessagePrompt = "### Assistant: %s ### User: %s \n### Assistant:"
}
if StopWord == "" {
StopWord = "###"
}
// Init LLAMA binding
l, err = llama.New(modelPath, llama.SetContext(1024), llama.EnableEmbeddings, llama.EnableMLock)
if err != nil {
log.Fatalf("Loading the model failed: %s", err.Error())
}
// Init Telegram API client
bot, err = tgbotapi.NewBotAPI(apiToken)
if err != nil {
log.Fatal(err)
}
// Start iterating through queue
go ProcessQueue()
// Receive updates
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
ProcessUpdate(update)
}
}