/
pair.go
192 lines (158 loc) · 5.07 KB
/
pair.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
package cmd
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"`
MoodeBaseURL string `env:"MOODE_BASE_URL,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 Execute() {
fileContentChan := make(chan string)
go runSongService(fileContentChan)
//go readFileAndStoreValue()
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: "start",
Description: "Start a new chat",
},
}...))
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
}
userID := update.Message.Chat.ID
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
}
}
go handleUpdates(bot, gpt, userID, fileContentChan)
select {}
}
}
func handleUpdates(bot *tgbotapi.BotAPI, gpt *GPT, userID int64, fileContentChan <-chan string) {
currentFileContent := ""
for {
// Ждем получения нового значения fileContent
fileContent := <-fileContentChan
// Проверяем, изменилось ли значение fileContent
if fileContent != currentFileContent {
// Обновляем текущее значение fileContent
currentFileContent = fileContent
// Вызываем processQueries с новым значением fileContent
processQueries(bot, gpt, userID, currentFileContent)
}
// Добавляем небольшую задержку перед следующей проверкой
time.Sleep(time.Second)
}
}
func processQueries(bot *tgbotapi.BotAPI, gpt *GPT, userID int64, fileContent string) {
answerChan := make(chan string)
prevFileContent := fileContent
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
go func() {
sendPrompt(bot, gpt, userID, fileContent, answerChan)
}()
var messageID int
for {
select {
case currentAnswer, ok := <-answerChan:
if !ok {
return
}
fmt.Println("Received answer:", currentAnswer)
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)
}
}
timer.Reset(5 * time.Second)
case <-timer.C:
if fileContent != prevFileContent {
prevFileContent = fileContent
sendPrompt(bot, gpt, userID, fileContent, answerChan)
}
timer.Reset(5 * time.Second)
}
}
}
func sendPrompt(bot *tgbotapi.BotAPI, gpt *GPT, userID int64, fileContent string, answerChan chan<- string) {
prompt := "Act as a radio host. Write me a short transitional intro (two sentences) from the track in the previous prompt to the track titled " + fileContent
fmt.Println("Sending prompt:", prompt)
contextTrimmed, err := gpt.SendMessage(userID, prompt, answerChan)
if err != nil {
log.Print(err)
_, err = bot.Send(tgbotapi.NewMessage(userID, err.Error()))
if err != nil {
log.Print(err)
}
}
if contextTrimmed {
msg := tgbotapi.NewMessage(userID, "Context trimmed.")
_, err = bot.Send(msg)
if err != nil {
log.Print(err)
}
}
}