-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
191 lines (165 loc) · 5.2 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
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
package main
import (
"fmt"
"log"
"net/url"
"os"
"time"
"github.com/caarlos0/env/v7"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
openai "github.com/sashabaranov/go-openai"
"github.com/sourcegraph/conc"
)
var cfg struct {
TelegramAPIToken string `env:"TELEGRAM_APITOKEN,required"`
OpenAIAPIKey string `env:"OPENAI_API_KEY,required"`
OPENAIModel string `env:"OPENAI_MODEL" envDefault:"gpt-3.5-turbo"`
OpenAIBaseURL string `env:"OPENAI_BASE_URL" envDefault:"https://api.openai.com"`
ModelTemperature float32 `env:"MODEL_TEMPERATURE" envDefault:"1.0"`
AllowedTelegramID []int64 `env:"ALLOWED_TELEGRAM_ID" envSeparator:","`
ConversationIdleTimeoutSeconds int `env:"CONVERSATION_IDLE_TIMEOUT_SECONDS" envDefault:"900"`
NotifyUserOnConversationIdleTimeout bool `env:"NOTIFY_USER_ON_CONVERSATION_IDLE_TIMEOUT" envDefault:"false"`
}
type User struct {
TelegramID int64
LastActiveTime time.Time
HistoryMessage []openai.ChatCompletionMessage
LatestMessage tgbotapi.Message
}
func main() {
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
os.Exit(1)
}
baseURL, err := url.JoinPath(cfg.OpenAIBaseURL, "/v1")
if err != nil {
panic(err)
}
cfg.OpenAIBaseURL = baseURL
if cfg.OPENAIModel != "gpt-3.5-turbo" && cfg.OPENAIModel != "gpt-4" {
log.Fatalf("Invalid OPENAI_MODEL: %s", cfg.OPENAIModel)
}
bot, err := tgbotapi.NewBotAPI(cfg.TelegramAPIToken)
if err != nil {
panic(err)
}
gpt := NewGPT()
log.Printf("Authorized on account %s", bot.Self.UserName)
_, _ = bot.Request(tgbotapi.NewSetMyCommands([]tgbotapi.BotCommand{
{
Command: "help",
Description: "Get help",
},
{
Command: "new",
Description: "Clear context and start a new conversation",
},
}...))
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil { // ignore any non-Message updates
continue
}
if len(cfg.AllowedTelegramID) != 0 {
var userAllowed bool
for _, allowedID := range cfg.AllowedTelegramID {
if allowedID == update.Message.Chat.ID {
userAllowed = true
}
}
if !userAllowed {
_, err := bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("You are not allowed to use this bot. User ID: %d", update.Message.Chat.ID)))
if err != nil {
log.Print(err)
}
continue
}
}
if update.Message.IsCommand() { // ignore any non-command Messages
// Create a new MessageConfig. We don't have text yet,
// so we leave it empty.
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
// Extract the command from the Message.
switch update.Message.Command() {
case "start":
msg.Text = "Welcome to ChatGPT bot! Write something to start a conversation. Use /new to clear context and start a new conversation."
case "help":
msg.Text = "Write something to start a conversation. Use /new to clear context and start a new conversation."
case "new":
gpt.ResetUser(update.Message.From.ID)
msg.Text = "OK, let's start a new conversation."
default:
msg.Text = "I don't know that command"
}
if _, err := bot.Send(msg); err != nil {
log.Print(err)
}
} else {
answerChan := make(chan string)
throttledAnswerChan := make(chan string)
userID := update.Message.Chat.ID
msg := update.Message.Text
wg := conc.NewWaitGroup()
wg.Go(func() {
contextTrimmed, err := gpt.SendMessage(userID, msg, answerChan)
if err != nil {
log.Print(err)
_, err = bot.Send(tgbotapi.NewMessage(userID, err.Error()))
if err != nil {
log.Print(err)
}
return
}
if contextTrimmed {
msg := tgbotapi.NewMessage(userID, "Context trimmed.")
_, err = bot.Send(msg)
if err != nil {
log.Print(err)
}
}
})
wg.Go(func() {
lastUpdateTime := time.Now()
var currentAnswer string
for answer := range answerChan {
currentAnswer = answer
// Update message every 2.5 seconds to avoid hitting Telegram API limits. In the documentation,
// Although the documentation states that the limit is one message per second, in practice, it is
// still rate-limited.
// https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this
if lastUpdateTime.Add(time.Duration(2500) * time.Millisecond).Before(time.Now()) {
throttledAnswerChan <- currentAnswer
lastUpdateTime = time.Now()
}
}
throttledAnswerChan <- currentAnswer
close(throttledAnswerChan)
})
wg.Go(func() {
_, err := bot.Send(tgbotapi.NewChatAction(userID, tgbotapi.ChatTyping))
if err != nil {
log.Print(err)
}
var messageID int
for currentAnswer := range throttledAnswerChan {
if messageID == 0 {
msg, err := bot.Send(tgbotapi.NewMessage(userID, currentAnswer))
if err != nil {
log.Print(err)
}
messageID = msg.MessageID
} else {
editedMsg := tgbotapi.NewEditMessageText(userID, messageID, currentAnswer)
_, err := bot.Send(editedMsg)
if err != nil {
log.Print(err)
}
}
}
})
wg.Wait()
}
}
}