/
chat.go
958 lines (815 loc) · 25.2 KB
/
chat.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
package http
import (
"bufio"
"bytes"
"context"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"image/jpeg"
"image/png"
"io"
"math"
"net/http"
urllib "net/url"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
"github.com/Laisky/errors/v2"
gmw "github.com/Laisky/gin-middlewares/v5"
gutils "github.com/Laisky/go-utils/v4"
"github.com/Laisky/go-utils/v4/json"
"github.com/Laisky/zap"
"github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
"golang.org/x/net/html"
"golang.org/x/sync/errgroup"
"github.com/Laisky/go-ramjet/internal/tasks/gptchat/config"
"github.com/Laisky/go-ramjet/internal/tasks/gptchat/db"
"github.com/Laisky/go-ramjet/library/log"
)
var (
dataReg = regexp.MustCompile(`^data: (.*)$`)
// llmRespCache cache llm response to quick response
llmRespCache = gutils.NewExpCache[string](context.Background(), time.Second*3)
)
// ChatHandler handle api request
func ChatHandler(ctx *gin.Context) {
toolcalls := sendAndParseChat(ctx)
if toolcalls == nil {
return
}
AbortErr(ctx, errors.New("tool calls not implemented"))
}
func sendAndParseChat(ctx *gin.Context) (toolCalls []OpenaiCompletionStreamRespToolCall) {
logger := gmw.GetLogger(ctx)
frontReq, openaiReq, err := convert2OpenaiRequest(ctx) //nolint:bodyclose
if AbortErr(ctx, err) {
return
}
// read cache
if cacheKey, err := req2CacheKey(frontReq); err != nil {
logger.Warn("marshal req for cache key", zap.Error(err))
} else if respContent, ok := llmRespCache.Load(cacheKey); ok {
res := &OpenaiCompletionStreamResp{
Choices: []OpenaiCompletionStreamRespChoice{
{
Delta: OpenaiCompletionStreamRespDelta{
Content: respContent,
},
FinishReason: "stop",
},
},
}
if data, err := json.Marshal(res); err != nil {
logger.Warn("marshal resp", zap.Error(err))
} else {
data = append([]byte("data: "), data...)
data = append(data, []byte("\n\n")...)
if _, err = io.Copy(ctx.Writer, bytes.NewReader(data)); err != nil {
logger.Warn("resp from cache", zap.Error(err))
} else {
logger.Debug("hit cache for llm response")
return
}
}
}
// send request to openai
logger.Debug("try send request to upstream server",
zap.String("url", openaiReq.URL.String()))
resp, err := httpcli.Do(openaiReq) //nolint: bodyclose
if AbortErr(ctx, err) {
return
}
defer gutils.LogErr(resp.Body.Close, logger)
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
AbortErr(ctx, errors.Errorf("request model %q got [%d]%s",
frontReq.Model, resp.StatusCode, string(body)))
return
}
CopyHeader(ctx.Writer.Header(), resp.Header)
isStream := strings.Contains(resp.Header.Get("Content-Type"), "text/event-stream")
if !isStream {
if _, err = io.Copy(ctx.Writer, resp.Body); AbortErr(ctx, err) {
return
}
return
}
bodyReader := resp.Body
reader := bufio.NewScanner(bodyReader)
reader.Split(bufio.ScanLines)
var respContent string
var lastResp *OpenaiCompletionStreamResp
var line []byte
for reader.Scan() {
line = bytes.TrimSpace(reader.Bytes())
// logger.Debug("got response line", zap.ByteString("line", line)) // debug only
if len(line) == 0 {
continue
}
var chunk []byte
if matched := dataReg.FindAllSubmatch(line, -1); len(matched) != 0 {
chunk = matched[0][1]
} else {
logger.Warn("unsupport resp line", zap.ByteString("line", line))
continue
}
if len(chunk) == 0 {
logger.Debug("empty chunk")
continue
}
_, err = io.Copy(ctx.Writer, bytes.NewReader(append(line, []byte("\n\n")...)))
if AbortErr(ctx, err) {
return
}
if bytes.Equal(chunk, []byte("[DONE]")) {
logger.Debug("got [DONE]")
lastResp = &OpenaiCompletionStreamResp{
Choices: []OpenaiCompletionStreamRespChoice{
{
FinishReason: "[DONE]",
},
},
}
break
}
lastResp = new(OpenaiCompletionStreamResp)
if err = json.Unmarshal(chunk, lastResp); err != nil {
logger.Warn("unmarshal resp",
zap.ByteString("line", line),
zap.ByteString("chunk", chunk),
zap.Error(err))
continue
}
if len(lastResp.Choices) > 0 {
if len(lastResp.Choices[0].Delta.ToolCalls) != 0 {
logger.Debug("got tool calls")
return lastResp.Choices[0].Delta.ToolCalls
}
respContent += lastResp.Choices[0].Delta.Content
}
// new oai api will return empty choices first
if len(lastResp.Choices) == 0 {
continue
}
// check if resp is end
if !isStream ||
len(lastResp.Choices) == 0 ||
lastResp.Choices[0].FinishReason != "" {
logger.Debug("got last resp",
zap.Any("is_stream", isStream),
zap.Int("choices", len(lastResp.Choices)),
zap.String("finish_reason", lastResp.Choices[0].FinishReason),
)
break
}
}
if strings.ToLower(os.Getenv("DISABLE_LLM_CONSERVATION_AUDIO")) != "true" {
if respContent != "" {
go saveLLMConservation(frontReq, respContent)
}
}
if lastResp == nil {
AbortErr(ctx, errors.New("no response"))
return
}
// scanner quit unexpected, write last line
if len(lastResp.Choices) != 0 &&
lastResp.Choices[0].FinishReason == "" {
lastResp.Choices[0].FinishReason = "stop"
lastResp.Choices[0].Delta.Content = " [TERMINATED UNEXPECTEDLY]"
payload, err := json.MarshalToString(lastResp)
if AbortErr(ctx, err) {
return
}
_, err = io.Copy(ctx.Writer, strings.NewReader("\ndata: "+payload))
if AbortErr(ctx, err) {
return
}
} else if gutils.IsEmpty(lastResp) {
return // bypass empty response
} else if isStream || len(lastResp.Choices) == 0 || lastResp.Choices[0].FinishReason != "" {
return // normal response
} else {
AbortErr(ctx, errors.Errorf("unsupport resp body %q", string(line)))
}
return nil
}
func req2CacheKey(req *FrontendReq) (string, error) {
data, err := json.Marshal(req)
if err != nil {
return "", errors.Wrap(err, "marshal req")
}
hashed := sha1.Sum(data)
return hex.EncodeToString(hashed[:]), nil
}
// SaveLlmConservationHandler save llm conservation
func SaveLlmConservationHandler(ctx *gin.Context) {
req := new(LLMConservationReq)
if err := ctx.BindJSON(req); AbortErr(ctx, err) {
return
}
freq := new(FrontendReq)
if err := copier.Copy(freq, req); AbortErr(ctx, err) {
return
}
go saveLLMConservation(freq, req.Response)
}
func saveLLMConservation(req *FrontendReq, respContent string) {
logger := log.Logger.Named("save_llm")
// save to cache
if cacheKey, err := req2CacheKey(req); err != nil {
logger.Warn("marshal req for cache key", zap.Error(err))
} else {
llmRespCache.Store(cacheKey, respContent)
}
// save to db
openaidb, err := db.GetOpenaiDB()
if err != nil {
logger.Error("get openai db", zap.Error(err))
return
}
docu := &db.OpenaiConservation{
Model: req.Model,
MaxTokens: req.MaxTokens,
Completion: respContent,
}
for _, msg := range req.Messages {
docu.Prompt = append(docu.Prompt, db.OpenaiMessage{
Role: msg.Role.String(),
Content: msg.Content,
})
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
ret, err := openaidb.GetCol("conservations").InsertOne(ctx, docu)
if err != nil {
logger.Error("insert conservation", zap.Error(err))
return
}
logger.Debug("save conservation", zap.Any("id", ret.InsertedID))
}
// VisionTokenPrice vision token price($/500000)
const VisionTokenPrice = 5000
// CountVisionImagePrice count vision image tokens
//
// https://openai.com/pricing
func CountVisionImagePrice(width int, height int, resolution VisionImageResolution) (int, error) {
switch resolution {
case VisionImageResolutionLow:
return 85, nil // fixed price
case VisionImageResolutionHigh:
h := math.Ceil(float64(height) / 512)
w := math.Ceil(float64(width) / 512)
n := w * h
total := 85 + 170*n
return int(total) * VisionTokenPrice, nil
default:
return 0, errors.Errorf("unsupport resolution %q", resolution)
}
}
func imageType(cnt []byte) string {
contentType := http.DetectContentType(cnt)
if strings.HasPrefix(contentType, "image/") {
return contentType
}
log.Logger.Warn("unsupport image content type", zap.String("type", contentType))
return "image/jpeg"
}
func imageSize(cnt []byte) (width, height int, err error) {
contentType := http.DetectContentType(cnt)
switch contentType {
case "image/jpeg", "image/jpg":
img, err := jpeg.Decode(bytes.NewReader(cnt))
if err != nil {
return 0, 0, errors.Wrap(err, "decode jpeg")
}
bounds := img.Bounds()
return bounds.Dx(), bounds.Dy(), nil
case "image/png":
img, err := png.Decode(bytes.NewReader(cnt))
if err != nil {
return 0, 0, errors.Wrap(err, "decode png")
}
bounds := img.Bounds()
return bounds.Dx(), bounds.Dy(), nil
default:
return 0, 0, errors.Errorf("unsupport image content type %q", contentType)
}
}
var (
// hdResolutionMarker enable hd resolution for gpt-4-vision only
// if user has permission and mention "hd" in prompt
hdResolutionMarker = regexp.MustCompile(`\bhd\b`)
)
func convert2OpenaiRequest(ctx *gin.Context) (frontendReq *FrontendReq, openaiReq *http.Request, err error) {
logger := gmw.GetLogger(ctx)
user, err := getUserByAuthHeader(ctx)
if err != nil {
return nil, nil, errors.Wrap(err, "get user")
}
newUrl := fmt.Sprintf("%s/%s", user.APIBase, "v1/chat/completions")
if ctx.Request.URL.RawQuery != "" {
newUrl += "?" + ctx.Request.URL.RawQuery
}
var reqBody []byte
if gutils.Contains([]string{http.MethodPost, http.MethodPut}, ctx.Request.Method) {
frontendReq, err = bodyChecker(ctx.Request.Body)
if err != nil {
return nil, nil, errors.Wrap(err, "request is illegal")
}
// enhance user query
if config.Config.RamjetURL != "" &&
frontendReq.LaiskyExtra != nil &&
!frontendReq.LaiskyExtra.ChatSwitch.DisableHttpsCrawler {
frontendReq.embeddingUrlContent(ctx, user)
}
if frontendReq.LaiskyExtra != nil &&
frontendReq.LaiskyExtra.ChatSwitch.EnableGoogleSearch {
frontendReq.embeddingGoogleSearch(ctx, user)
}
// fmt.Println(frontendReq.Messages)
frontendReq.LaiskyExtra = nil
if err := user.IsModelAllowed(ctx, frontendReq.Model, frontendReq.PromptTokens()); err != nil {
return nil, nil, errors.Wrapf(err, "check is model allowed for user %q", user.UserName)
}
if frontendReq.Stream {
enableHeartBeatForStreamReq(ctx)
}
var openaiReq any
MODEL_SWITCH:
switch frontendReq.Model {
case "gpt-4-turbo-preview",
"gpt-4-1106-preview",
"gpt-4-0125-preview",
"gpt-4",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0613",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-16k-0613",
"gpt-3.5-turbo",
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-1106",
"gpt-3.5-turbo-0125",
"claude-instant-1",
"claude-2",
// "llama2-70b-4096",
"mixtral-8x7b-32768",
"gemma-7b-it",
"llama3-8b-8192",
"llama3-70b-8192",
"gemini-pro":
req := new(OpenaiChatReq[string])
if err := copier.Copy(req, frontendReq); err != nil {
return nil, nil, errors.Wrap(err, "copy to chat req")
}
openaiReq = req
case "claude-3-opus", // support text and vision at the same time
"claude-3-sonnet",
"claude-3-haiku",
"gpt-4-turbo-2024-04-09",
"gpt-4-turbo":
lastMessage := frontendReq.Messages[len(frontendReq.Messages)-1]
if len(lastMessage.Files) == 0 { // no images, text only
req := new(OpenaiChatReq[string])
if err := copier.Copy(req, frontendReq); err != nil {
return nil, nil, errors.Wrap(err, "copy to chat req")
}
openaiReq = req
break MODEL_SWITCH
}
fallthrough // jump to vision part
case "gpt-4-vision-preview", // only support vision
"gemini-pro-vision":
lastMessage := frontendReq.Messages[len(frontendReq.Messages)-1]
req := new(OpenaiChatReq[[]OpenaiVisionMessageContent])
if err := copier.Copy(req, frontendReq); err != nil {
return nil, nil, errors.Wrap(err, "copy to chat req")
}
if len(lastMessage.Files) == 0 {
// there is no images attached, so rewrite model to non-vision model
req.Model = "gpt-3.5-turbo"
}
req.Messages = []OpenaiReqMessage[[]OpenaiVisionMessageContent]{
{
Role: OpenaiMessageRoleUser,
Content: []OpenaiVisionMessageContent{
{
Type: OpenaiVisionMessageContentTypeText,
Text: lastMessage.Content,
},
},
},
}
totalFileSize := 0
for i, f := range lastMessage.Files {
resolution := VisionImageResolutionLow
// if user has permission and image size is large than 1MB,
// use high resolution
if (user.BYOK || user.NoLimitExpensiveModels) && hdResolutionMarker.MatchString(lastMessage.Content) {
resolution = VisionImageResolutionHigh
}
req.Messages[0].Content = append(req.Messages[0].Content, OpenaiVisionMessageContent{
Type: OpenaiVisionMessageContentTypeImageUrl,
ImageUrl: &OpenaiVisionMessageContentImageUrl{
URL: fmt.Sprintf("data:%s;base64,", imageType(f.Content)) +
base64.StdEncoding.EncodeToString(f.Content),
Detail: resolution,
},
})
if i >= 6 {
break // only support 6 images for cost saving
}
totalFileSize += len(f.Content)
if totalFileSize > 10*1024*1024 {
return nil, nil, errors.Errorf("total file size should less than 10MB, got %d", totalFileSize)
}
}
openaiReq = req
case "text-davinci-003":
newUrl = fmt.Sprintf("%s/%s", user.APIBase, "v1/completions")
openaiReq = new(OpenaiCompletionReq)
if err := copier.Copy(openaiReq, frontendReq); err != nil {
return nil, nil, errors.Wrap(err, "copy to completion req")
}
default:
return nil, nil, errors.Errorf("unsupport chat model %q", frontendReq.Model)
}
if reqBody, err = json.Marshal(openaiReq); err != nil {
return nil, nil, errors.Wrap(err, "marshal new body")
}
logger.Debug("prepare request to upstream server") // zap.ByteString("payload", reqBody),
}
req, err := http.NewRequestWithContext(gmw.Ctx(ctx),
ctx.Request.Method, newUrl, bytes.NewReader(reqBody))
if err != nil {
return nil, nil, errors.Wrap(err, "new request")
}
CopyHeader(req.Header, ctx.Request.Header)
req.Header.Set("authorization", "Bearer "+user.OpenaiToken)
// if set header "Accept-Encoding" manually,
// golang's http client will not auto decompress response body
req.Header.Del("Accept-Encoding")
return frontendReq, req, nil
}
func (r *FrontendReq) fillDefault() {
r.MaxTokens = gutils.OptionalVal(&r.MaxTokens, 500)
r.Temperature = gutils.OptionalVal(&r.Temperature, 1)
r.TopP = gutils.OptionalVal(&r.TopP, 1)
r.N = gutils.OptionalVal(&r.N, 1)
r.Model = gutils.OptionalVal(&r.Model, ChatModel())
// r.BestOf = gutils.OptionalVal(&r.BestOf, 1)
}
// UserQueryType user query type
type UserQueryType string
const (
// UserQueryTypeSearch search by embeddings chunks
UserQueryTypeSearch UserQueryType = "search"
// UserQueryTypeScan scan by map-reduce
UserQueryTypeScan UserQueryType = "scan"
)
var (
urlContentCache = gutils.NewExpCache[[]byte](context.Background(), 24*time.Hour)
urlRegexp = regexp.MustCompile(`https:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)`)
)
// fetchURLContent fetch url content
func fetchURLContent(gctx *gin.Context, url string) (content []byte, err error) {
content, ok := urlContentCache.Load(url)
if ok {
log.Logger.Debug("hit cache for query mentioned url", zap.String("url", url))
return content, nil
}
ctx, cancel := context.WithTimeout(gmw.Ctx(gctx), 25*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil)
if err != nil {
return nil, errors.Wrapf(err, "new request %q", url)
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537")
resp, err := httpcli.Do(req) // nolint:bodyclose
if err != nil {
return nil, errors.Wrapf(err, "do request %q", url)
}
defer gutils.LogErr(resp.Body.Close, log.Logger)
contentType := resp.Header.Get("Content-Type")
switch {
case strings.Contains(contentType, "text/html") ||
strings.Contains(contentType, "application/xhtml+xml"):
content, err = fetchDynamicURLContent(ctx, url)
default:
content, err = fetchStaticURLContent(ctx, url)
}
if err != nil {
return nil, errors.Wrapf(err, "fetch url %q", url)
}
urlContentCache.Store(url, content) // save cache
return content, nil
}
// findHTMLBody find html body recursively
func findHTMLBody(n *html.Node) *html.Node {
if n.Type == html.ElementNode && n.Data == "body" {
return n
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if body := findHTMLBody(c); body != nil {
return body
}
}
return nil
}
// extractHTMLBody extract body from html
func extractHTMLBody(content []byte) (bodyContent []byte, err error) {
parsedHTML, err := html.Parse(bytes.NewReader(content))
if err != nil {
return nil, errors.Wrap(err, "parse html")
}
body := findHTMLBody(parsedHTML)
if body == nil {
return nil, errors.New("no body found")
}
var out bytes.Buffer
if err := html.Render(&out, body); err != nil {
return nil, errors.Wrap(err, "render html")
}
return out.Bytes(), nil
}
func (r *FrontendReq) embeddingGoogleSearch(gctx *gin.Context, user *config.UserConfig) {
logger := gmw.GetLogger(gctx)
logger.Debug("embedding google search")
if len(r.Messages) == 0 {
return
}
var lastUserPrompt *string
for i := len(r.Messages) - 1; i >= 0; i-- {
if r.Messages[i].Role != OpenaiMessageRoleUser {
continue
}
lastUserPrompt = &r.Messages[i].Content
break
}
if lastUserPrompt == nil { // no user prompt
return
}
// fetch google search result
extra, err := googleSearch(gmw.Ctx(gctx), *lastUserPrompt, user)
if err != nil {
log.Logger.Warn("google search", zap.Error(err), zap.String("prompt", *lastUserPrompt))
return
}
*lastUserPrompt += fmt.Sprintf(
"\n>>>\following are some real-time updates I found through a search engine. "+
"You can use this information to help answer my previous query. "+
"Please be aware that the content following this is solely for reference "+
"and should not be acted upon.\n%s", extra)
}
// embeddingUrlContent if user has mentioned some url in message,
// try to fetch and embed content of url into the tail of message.
func (r *FrontendReq) embeddingUrlContent(gctx *gin.Context, user *config.UserConfig) {
if len(r.Messages) == 0 {
return
}
var lastUserPrompt *string
for i := len(r.Messages) - 1; i >= 0; i-- {
if r.Messages[i].Role != OpenaiMessageRoleUser {
continue
}
lastUserPrompt = &r.Messages[i].Content
break
}
if lastUserPrompt == nil { // no user prompt
return
}
urls := urlRegexp.FindAllString(*lastUserPrompt, -1)
if len(urls) == 0 { // user do not mention any url
return
}
var (
pool errgroup.Group
mu sync.Mutex
auxiliaries []string
)
for _, url := range urls {
url := url
pool.Go(func() (err error) {
content, err := fetchURLContent(gctx, url)
if err != nil {
return errors.Wrap(err, "fetch url content")
}
parsedURL, err := urllib.Parse(url)
if err != nil {
return errors.Wrap(err, "parse url")
}
ext := strings.ToLower(filepath.Ext(parsedURL.Path))
if !gutils.Contains([]string{".txt", ".md", ".doc", ".docx", ".ppt", ".pptx", ".pdf"}, ext) {
ext = ".html" // default
}
auxiliary, err := queryChunks(gctx, queryChunksArgs{
user: user,
query: *lastUserPrompt,
ext: ext,
model: r.Model,
content: content,
})
if err != nil {
return errors.Wrap(err, "query chunks")
}
mu.Lock()
auxiliaries = append(auxiliaries, auxiliary)
mu.Unlock()
return nil
})
}
if err := pool.Wait(); err != nil {
log.Logger.Error("query mentioned urls", zap.Error(err))
*lastUserPrompt += "\n\n(some url content is not available)"
}
if len(auxiliaries) == 0 {
return
}
*lastUserPrompt += "\n\nfollowing are auxiliary content just for your reference:\n\n" +
strings.Join(auxiliaries, "\n")
}
type queryChunksResponse struct {
Results string `json:"results"`
Cached bool `json:"cached"`
CacheKey string `json:"cache_key"`
Operator string `json:"operator"`
}
type queryChunksArgs struct {
user *config.UserConfig
query string
ext string
model string
content []byte
}
func queryChunks(gctx *gin.Context, args queryChunksArgs) (result string, err error) {
log.Logger.Debug("query ramjet to search chunks",
zap.String("ext", args.ext))
reqData := map[string]any{
"content": base64.StdEncoding.EncodeToString(args.content),
"query": args.query,
"ext": args.ext,
"model": args.model,
"max_chunks": 10000,
}
if args.user.IsFree {
reqData["max_chunks"] = 500
}
postBody, err := json.Marshal(reqData)
if err != nil {
return "", errors.Wrap(err, "marshal post body")
}
queryChunkURL := fmt.Sprintf("%s/gptchat/query/chunks", config.Config.RamjetURL)
queryCtx, queryCancel := context.WithTimeout(gmw.Ctx(gctx), 180*time.Second)
defer queryCancel()
req, err := http.NewRequestWithContext(queryCtx, http.MethodPost, queryChunkURL, bytes.NewReader(postBody))
if err != nil {
return "", errors.Wrapf(err, "new request %q", queryChunkURL)
}
req.Header.Set("Authorization", "Bearer "+args.user.OpenaiToken)
if err := setUserAuth(gctx, req); err != nil {
return "", errors.Wrap(err, "set user auth")
}
resp, err := httpcli.Do(req) // nolint:bodyclose
if err != nil {
return "", errors.Wrapf(err, "do request %q", queryChunkURL)
}
defer gutils.LogErr(resp.Body.Close, log.Logger)
if resp.StatusCode != http.StatusOK {
return "", errors.Errorf("[%d]%s", resp.StatusCode, queryChunkURL)
}
args.content, err = io.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "read response body")
}
respData := new(queryChunksResponse)
if err = json.Unmarshal(args.content, respData); err != nil {
return "", errors.Wrap(err, "unmarshal response body")
}
log.Logger.Debug("got ramjet parsed chunks",
// zap.String("result", respData.Results),
zap.Bool("cached", respData.Cached),
zap.String("cache_key", respData.CacheKey),
zap.String("operator", respData.Operator),
)
return respData.Results, nil
}
func enableHeartBeatForStreamReq(gctx *gin.Context) {
heartCtx, heartCancel := context.WithCancel(gmw.Ctx(gctx))
var allCleaned = make(chan struct{})
defer func() {
heartCancel()
<-allCleaned
}()
go func() {
defer close(allCleaned)
for {
select {
case <-heartCtx.Done():
return
default:
}
if _, err := io.Copy(gctx.Writer, bytes.NewReader([]byte("data: [HEARTBEAT]\n\n"))); err != nil {
log.Logger.Warn("failed write heartbeat msg to sse", zap.Error(err))
return
}
gctx.Writer.Flush()
time.Sleep(time.Second)
}
}()
}
func bodyChecker(body io.ReadCloser) (userReq *FrontendReq, err error) {
payload, err := io.ReadAll(body)
if err != nil {
return nil, errors.Wrap(err, "read request body")
}
userReq = new(FrontendReq)
if err = json.Unmarshal(payload, userReq); err != nil {
return nil, errors.Wrap(err, "parse request")
}
userReq.fillDefault()
if len(userReq.Messages) == 0 {
return nil, errors.New("no messages")
}
return userReq, err
}
// OneShotChatHandler handle one shot chat request
func OneShotChatHandler(gctx *gin.Context) {
user, err := getUserByAuthHeader(gctx)
if AbortErr(gctx, err) {
return
}
req := new(OneShotChatRequest)
if err := gctx.BindJSON(req); AbortErr(gctx, err) {
return
}
resp, err := OneshotChat(gmw.Ctx(gctx), user, req.SystemPrompt, req.UserPrompt)
if AbortErr(gctx, err) {
return
}
gctx.JSON(http.StatusOK, gin.H{
"response": resp,
})
}
// OneshotChat get ai response from gpt-3.5-turbo
//
// # Args:
// - systemPrompt: system prompt
// - userPrompt: user prompt
func OneshotChat(ctx context.Context, user *config.UserConfig, systemPrompt, userPrompt string) (answer string, err error) {
logger := gmw.GetLogger(ctx)
if userPrompt == "" {
userPrompt = "The following is a conversation with Chat-GPT, an AI created by OpenAI. The AI is helpful, creative, clever, and very friendly, it's mainly focused on solving coding problems, so it likely provide code example whenever it can and every code block is rendered as markdown. However, it also has a sense of humor and can talk about anything. Please answer user's last question, and if possible, reference the context as much as you can."
}
body, err := json.Marshal(OpenaiChatReq[string]{
Model: "gpt-3.5-turbo",
MaxTokens: 1000,
Stream: false,
Messages: []OpenaiReqMessage[string]{
{
Role: OpenaiMessageRoleSystem,
Content: systemPrompt,
},
{
Role: OpenaiMessageRoleUser,
Content: userPrompt,
},
},
})
if err != nil {
return "", errors.Wrap(err, "marshal req")
}
url := fmt.Sprintf("%s/%s", user.APIBase, "v1/chat/completions")
req, err := http.NewRequestWithContext(ctx,
http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return "", errors.Wrap(err, "new request")
}
logger.Info("send one-shot chat request",
zap.String("user", user.UserName),
)
req.Header.Add("Authorization", "Bearer "+user.Token)
req.Header.Add("Content-Type", "application/json")
resp, err := httpcli.Do(req)
if err != nil {
return "", errors.Wrap(err, "do request")
}
defer gutils.LogErr(resp.Body.Close, log.Logger)
if resp.StatusCode != http.StatusOK {
return "", errors.Errorf("[%d]%s", resp.StatusCode, url)
}
respData := new(OpenaiCompletionResp)
if err = json.NewDecoder(resp.Body).Decode(respData); err != nil {
return "", errors.Wrap(err, "decode response")
}
if len(respData.Choices) == 0 {
return "", errors.New("no choices")
}
return respData.Choices[0].Message.Content, nil
}