forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletions.go
62 lines (52 loc) · 1.43 KB
/
completions.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
package manager
import (
adaptercommon "chat/adapter/common"
"chat/addition/web"
"chat/admin"
"chat/auth"
"chat/channel"
"chat/globals"
"chat/utils"
"fmt"
"runtime/debug"
"github.com/gin-gonic/gin"
)
func NativeChatHandler(c *gin.Context, user *auth.User, model string, message []globals.Message, enableWeb bool) (string, float32) {
defer func() {
if err := recover(); err != nil {
stack := debug.Stack()
globals.Warn(fmt.Sprintf("caught panic from chat handler: %s (instance: %s, client: %s)\n%s",
err, model, c.ClientIP(), stack,
))
}
}()
segment := web.ToSearched(enableWeb, message)
db := utils.GetDBFromContext(c)
cache := utils.GetCacheFromContext(c)
check, plan := auth.CanEnableModelWithSubscription(db, cache, user, model)
if check != nil {
return check.Error(), 0
}
buffer := utils.NewBuffer(model, segment, channel.ChargeInstance.GetCharge(model))
hit, err := channel.NewChatRequestWithCache(
cache, buffer,
auth.GetGroup(db, user),
adaptercommon.CreateChatProps(&adaptercommon.ChatProps{
Model: model,
Message: segment,
}, buffer),
func(resp *globals.Chunk) error {
buffer.WriteChunk(resp)
return nil
},
)
admin.AnalyseRequest(model, buffer, err)
if err != nil {
auth.RevertSubscriptionUsage(db, cache, user, model)
return err.Error(), 0
}
if !hit {
CollectQuota(c, user, buffer, plan, err)
}
return buffer.ReadWithDefault(defaultMessage), buffer.GetQuota()
}