-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
322 lines (288 loc) · 8.85 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
package matchguru
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"text/template"
"time"
_ "time/tzdata"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/klipach/matchguru/auth"
"github.com/klipach/matchguru/chat"
"github.com/klipach/matchguru/contract"
"github.com/klipach/matchguru/game"
"github.com/klipach/matchguru/logger"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/openai"
)
const (
gcloudFuncSourceDir = "serverless_function_source_code"
)
var (
openaiAPIKey = os.Getenv("OPENAI_API_KEY")
perplexityApiKey = os.Getenv("PERPLEXITY_API_KEY")
)
func init() {
functions.HTTP("Bot", Bot)
fixDir()
}
// in GCP Functions, source code is placed in a directory named "serverless_function_source_code"
// need to change the dir to get access to template file
func fixDir() {
fileInfo, err := os.Stat(gcloudFuncSourceDir)
if err == nil && fileInfo.IsDir() {
_ = os.Chdir(gcloudFuncSourceDir)
}
}
func Bot(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := logger.FromContext(ctx)
logger.Println("bot function called")
if r.Method != http.MethodPost {
logger.Printf("invalid method: %s", r.Method)
http.Error(w, "Method Not Implemented", http.StatusNotImplemented)
return
}
token, err := auth.Authenticate(r)
if err != nil {
logger.Printf("error while authenticating: %v", err)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
mainPrompt, err := template.New("main.tmpl").ParseFiles("prompts/main.tmpl")
if err != nil {
logger.Printf("error while parsing mainPrompt: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
var msg contract.BotRequest
data, err := io.ReadAll(r.Body)
if err != nil {
logger.Printf("error while reading request body: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
logger.Printf("message: %v", string(data))
if err := json.Unmarshal(data, &msg); err != nil {
logger.Printf("error while decoding request: %v", err)
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
// test message:
if strings.TrimSpace(msg.Message) == "test" {
resp := contract.BotResponse{
Response: `<b> hi there</b>
<a href="/team/346">Team</a>
<a href="/player/4237">Player</a>
<a href="/league/384">League</a>
<a href="/fixture/19155228">Fixture</a>
<s>strikethrough</s>
<i>italic</i>
`,
}
err = json.NewEncoder(w).Encode(resp)
if err != nil {
logger.Printf("error while encoding response: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
return
}
loc, err := time.LoadLocation(msg.Timezone)
if err != nil {
logger.Printf("error while loading location: %v", err)
}
today := time.Now().In(loc).Format("2006-01-02")
_, offset := time.Now().In(loc).Zone()
timeOffset := fmt.Sprintf("%+03d:%+02d", offset/3600, offset/60%60)
gg := &game.Game{}
if msg.GameID != 0 {
gg, err = game.Fetch(ctx, msg.GameID)
if err != nil {
logger.Printf("error while fetching game: %v", err)
}
logger.Printf("game fetched %v+", gg)
}
var mainPromptStr strings.Builder
err = mainPrompt.Execute(
&mainPromptStr,
struct {
Today string
TimeOffset string
GameName string
GameStartingAt time.Time
GameLeague string
Season string
Country string
}{
Today: today,
TimeOffset: timeOffset,
GameName: gg.Name,
GameStartingAt: gg.StartingAt,
GameLeague: gg.League,
Season: gg.Season,
Country: gg.Country,
})
if err != nil {
logger.Printf("error while executing mainPrompt: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
shortPrompt, err := template.New("short.tmpl").ParseFiles("prompts/short.tmpl")
if err != nil {
logger.Printf("error while parsing shortPrompt: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
var shortPromptStr strings.Builder
err = shortPrompt.Execute(
&shortPromptStr,
struct {
Today string
TimeOffset string
GameName string
GameStartingAt time.Time
GameLeague string
Season string
Country string
}{
Today: today,
TimeOffset: timeOffset,
GameName: gg.Name,
GameStartingAt: gg.StartingAt,
GameLeague: gg.League,
Season: gg.Season,
Country: gg.Country,
})
if err != nil {
logger.Printf("error while executing shortPrompt: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
messages, err := chat.LoadHistory(ctx, token.UID, msg.ChatID)
if err != nil {
logger.Printf("error while loading chat history: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
messages = append(messages, llms.TextParts(llms.ChatMessageTypeHuman, msg.Message))
gpt4o, err := openai.New(
openai.WithModel("gpt-4o"),
openai.WithToken(openaiAPIKey),
)
if err != nil {
logger.Printf("error while creating gpt4Turbo: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
shortResp, err := gpt4o.GenerateContent(
ctx,
append(
[]llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeSystem, shortPromptStr.String()),
},
messages...,
),
llms.WithTemperature(0.8),
llms.WithMaxTokens(1000),
)
if err != nil {
logger.Printf("CreateChatCompletion short error: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
logger.Printf("short response: %s", shortResp.Choices[0].Content)
if strings.ToLower(shortResp.Choices[0].Content) != "no" {
logger.Printf("external knowledge required, perplexity request: %s", shortResp.Choices[0].Content)
perplexityClient, err := openai.New(
// Supported models: https://docs.perplexity.ai/docs/model-cards
openai.WithModel("llama-3.1-sonar-small-128k-online"),
openai.WithBaseURL("https://api.perplexity.ai"),
openai.WithToken(perplexityApiKey),
)
if err != nil {
logger.Printf("error while creating perplexity client: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
perplexityPrompt, err := template.New("perplexity.tmpl").ParseFiles("prompts/perplexity.tmpl")
if err != nil {
logger.Printf("error while parsing perplexityPrompt: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
var perplexityPromptStr strings.Builder
err = perplexityPrompt.Execute(
&perplexityPromptStr,
struct {
Today string
TimeOffset string
}{
Today: time.Now().Format("2006-01-02"),
TimeOffset: timeOffset,
})
if err != nil {
logger.Printf("error while executing perplexityPrompt: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
perplexityResp, err := perplexityClient.GenerateContent(ctx,
[]llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeSystem, perplexityPromptStr.String()),
llms.TextParts(llms.ChatMessageTypeHuman, shortResp.Choices[0].Content),
},
llms.WithTemperature(0.8),
llms.WithMaxTokens(1000),
)
if err != nil {
logger.Printf("error while generating from single prompt: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
logger.Printf("perplexity response: %s", perplexityResp.Choices[0].Content)
mainPromptStr.WriteString("Additional info for the request: " + perplexityResp.Choices[0].Content)
}
completion, err := gpt4o.GenerateContent(
ctx,
append(
[]llms.MessageContent{
llms.TextParts(llms.ChatMessageTypeSystem, mainPromptStr.String()),
},
messages...,
),
llms.WithTemperature(0.8),
llms.WithMaxTokens(1000),
)
if err != nil {
logger.Printf("ChatCompletion error: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
logger.Printf("response: %s", completion.Choices[0].Content)
response := process(completion.Choices[0].Content)
// replace newlines with <br /> for HTML
response = strings.Replace(response, "\n", "<br />", -1)
// convert input markdown to HTML to render in app
unsafeHTML := blackfriday.Run([]byte(response))
// allow only tags that are supported by app
policy := bluemonday.NewPolicy()
policy.AllowElements("br", "s", "i", "b", "a")
safeHTML := policy.SanitizeBytes(unsafeHTML)
response = string(safeHTML)
logger.Printf("processed response: %s", response)
err = json.NewEncoder(w).Encode(
contract.BotResponse{
Response: response,
},
)
if err != nil {
logger.Printf("error while encoding response: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}