forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.go
150 lines (130 loc) · 3.51 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
package manager
import (
"chat/adapter"
"chat/addition/web"
"chat/admin"
"chat/auth"
"chat/channel"
"chat/globals"
"chat/manager/conversation"
"chat/utils"
"fmt"
"github.com/gin-gonic/gin"
"time"
)
const defaultMessage = "Sorry, I don't understand. Please try again."
const defaultQuotaMessage = "You don't have enough quota to use this model. please [buy](/buy) or [subscribe](/subscribe) to get more. (or try to refresh the page)"
func CollectQuota(c *gin.Context, user *auth.User, buffer *utils.Buffer, uncountable bool) {
db := utils.GetDBFromContext(c)
quota := buffer.GetQuota()
if buffer.IsEmpty() {
return
}
if !uncountable && quota > 0 && user != nil {
user.UseQuota(db, quota)
}
}
func MockStreamSender(conn *Connection, message string) {
for _, line := range utils.SplitLangItems(message) {
time.Sleep(100 * time.Millisecond)
conn.Send(globals.ChatSegmentResponse{
Message: line + " ",
End: false,
Quota: 0,
})
if signal := conn.PeekWithType(StopType); signal != nil {
// stop signal from client
break
}
}
conn.Send(globals.ChatSegmentResponse{
End: true,
Quota: 0,
})
}
func ChatHandler(conn *Connection, user *auth.User, instance *conversation.Conversation) string {
defer func() {
if err := recover(); err != nil {
globals.Warn(fmt.Sprintf("caught panic from chat handler: %s (instance: %s, client: %s)",
err, instance.GetModel(), conn.GetCtx().ClientIP(),
))
}
}()
segment := web.UsingWebSegment(instance)
model := instance.GetModel()
db := conn.GetDB()
cache := conn.GetCache()
check, plan := auth.CanEnableModelWithSubscription(db, cache, user, model)
conn.Send(globals.ChatSegmentResponse{
Conversation: instance.GetId(),
})
if !check {
conn.Send(globals.ChatSegmentResponse{
Message: defaultQuotaMessage,
Quota: 0,
End: true,
})
return defaultQuotaMessage
}
if form := ExtractCacheData(conn.GetCtx(), &CacheProps{
Message: segment,
Model: model,
Reversible: plan,
}); form != nil {
MockStreamSender(conn, form.Message)
return form.Message
}
buffer := utils.NewBuffer(model, segment)
err := channel.NewChatRequest(&adapter.ChatProps{
Model: model,
Message: segment,
Plan: plan,
Buffer: *buffer,
}, func(data string) error {
if signal := conn.PeekWithType(StopType); signal != nil {
// stop signal from client
return fmt.Errorf("signal")
}
return conn.SendClient(globals.ChatSegmentResponse{
Message: buffer.Write(data),
Quota: buffer.GetQuota(),
End: false,
Plan: plan,
})
})
admin.AnalysisRequest(model, buffer, err)
if err != nil && err.Error() != "signal" {
globals.Warn(fmt.Sprintf("caught error from chat handler: %s (instance: %s, client: %s)", err, model, conn.GetCtx().ClientIP()))
auth.RevertSubscriptionUsage(db, cache, user, model)
CollectQuota(conn.GetCtx(), user, buffer, plan)
conn.Send(globals.ChatSegmentResponse{
Message: err.Error(),
End: true,
})
return err.Error()
}
CollectQuota(conn.GetCtx(), user, buffer, plan)
if buffer.IsEmpty() {
conn.Send(globals.ChatSegmentResponse{
Message: defaultMessage,
End: true,
})
return defaultMessage
}
conn.Send(globals.ChatSegmentResponse{
End: true,
Quota: buffer.GetQuota(),
Plan: plan,
})
result := buffer.ReadWithDefault(defaultMessage)
if err == nil && result != defaultMessage {
SaveCacheData(conn.GetCtx(), &CacheProps{
Message: segment,
Model: model,
Reversible: plan,
}, &CacheData{
Message: result,
})
}
return result
}