/
chat.go
468 lines (440 loc) · 17.3 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
/*
Copyright 2023 KubeAGI.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package service
import (
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kubeagi/arcadia/api/base/v1alpha1"
"github.com/kubeagi/arcadia/apiserver/config"
"github.com/kubeagi/arcadia/apiserver/pkg/auth"
"github.com/kubeagi/arcadia/apiserver/pkg/chat"
"github.com/kubeagi/arcadia/apiserver/pkg/client"
"github.com/kubeagi/arcadia/apiserver/pkg/oidc"
"github.com/kubeagi/arcadia/apiserver/pkg/requestid"
)
const (
// Time interval to check if the chat stream should be closed if no more message arrives
WaitTimeoutForChatStreaming = 120
// default prompt starter
PromptLimit = 4
)
type ChatService struct {
server *chat.ChatServer
}
func NewChatService(cli runtimeclient.Client, isGpts bool) (*ChatService, error) {
return &ChatService{chat.NewChatServer(cli, isGpts)}, nil
}
// @Summary chat with application
// @Schemes
// @Description chat with application
// @Tags application
// @Accept json
// @Produce json
// @Param namespace header string true "namespace this request is in"
// @Param debug query bool false "Should the chat request be treated as debugging?"
// @Param request body chat.ChatReqBody true "query params"
// @Success 200 {object} chat.ChatRespBody "blocking mode, will return all field; streaming mode, only conversation_id, message and created_at will be returned"
// @Failure 400 {object} chat.ErrorResp
// @Failure 500 {object} chat.ErrorResp
// @Router /chat [post]
func (cs *ChatService) ChatHandler() gin.HandlerFunc {
return func(c *gin.Context) {
req := chat.ChatReqBody{StartTime: time.Now()}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
req.AppNamespace = NamespaceInHeader(c)
req.Debug = c.Query("debug") == "true"
req.NewChat = len(req.ConversationID) == 0
if req.NewChat {
req.ConversationID = string(uuid.NewUUID())
}
messageID := string(uuid.NewUUID())
var response *chat.ChatRespBody
var err error
logger := klog.FromContext(c.Request.Context())
chatTimeoutSecond := pointer.Float64(WaitTimeoutForChatStreaming)
if req.ResponseMode.IsStreaming() {
buf := strings.Builder{}
// handle chat streaming mode
respStream := make(chan string, 1)
manualStop := make(chan bool)
go func() {
defer func() {
if e := recover(); e != nil {
err, ok := e.(error)
if ok {
logger.Error(err, "A panic occurred when run chat.AppRun")
} else {
logger.Error(fmt.Errorf("get err:%#v", e), "A panic occurred when run chat.AppRun")
}
}
}()
response, err = cs.server.AppRun(c.Request.Context(), req, respStream, messageID, chatTimeoutSecond)
if err != nil {
c.SSEvent("error", chat.ChatRespBody{
MessageID: messageID,
ConversationID: req.ConversationID,
Message: err.Error(),
CreatedAt: time.Now(),
Latency: time.Since(req.StartTime).Milliseconds(),
})
// c.JSON(http.StatusInternalServerError, chat.ErrorResp{Err: err.Error()})
logger.Error(err, "error resp, stop the stream")
manualStop <- true
return
}
if response != nil {
if str := buf.String(); response.Message == str || strings.TrimSpace(str) == strings.TrimSpace(response.Message) {
logger.Info("blocking resp is same with streaming resp, no new message received, stop the stream")
manualStop <- true
}
}
}()
LatestTimestampGetDataFromLLM := time.Now()
// Use ticker to check if there is no data from llm for a long time and close the entire stream
ticker := time.NewTicker(time.Microsecond * 500)
defer ticker.Stop()
go func() {
for range ticker.C {
timeout := time.Second * time.Duration(*chatTimeoutSecond)
if time.Since(LatestTimestampGetDataFromLLM) > timeout {
logger.Info("no data from LLM for a long time, stop the stream", "timeout", timeout)
manualStop <- true
return
}
}
}()
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("Transfer-Encoding", "chunked")
logger.Info("start to receive messages...")
clientDisconnected := c.Stream(func(w io.Writer) bool {
for {
select {
case <-manualStop:
return false
case msg, ok := <-respStream:
if !ok {
return false
}
t := time.Now()
c.SSEvent("", chat.ChatRespBody{
MessageID: messageID,
ConversationID: req.ConversationID,
Message: msg,
CreatedAt: t,
Latency: time.Since(req.StartTime).Milliseconds(),
})
LatestTimestampGetDataFromLLM = t
buf.WriteString(msg)
return true
}
}
})
if clientDisconnected {
manualStop <- true
logger.Info("chatHandler: the client is disconnected")
}
logger.Info("end to receive messages")
} else {
// handle chat blocking mode
response, err = cs.server.AppRun(c.Request.Context(), req, nil, messageID, chatTimeoutSecond)
if err != nil {
c.JSON(http.StatusInternalServerError, chat.ErrorResp{Err: err.Error()})
logger.Error(err, "error resp")
return
}
c.JSON(http.StatusOK, response)
}
switch {
case logger.V(5).Enabled():
logger.Info("chat done", "req", req, "resp", response)
case logger.V(3).Enabled():
logger.Info("chat done", "req", req)
default:
logger.Info("chat done")
}
}
}
// @Summary receive conversational files for one conversation
// @Schemes
// @Description receive conversational files for one conversation
// @Tags application
// @Accept multipart/form-data
// @Produce json
//
// @Param namespace header string true "namespace this request is in"
// @Param app_name formData string true "The app name for this conversation"
// @Param conversation_id formData string false "The conversation id for this file"
// @Param file formData file true "This is the file for the conversation"
//
// @Success 200 {object} chat.ChatRespBody
// @Failure 400 {object} chat.ErrorResp
// @Failure 500 {object} chat.ErrorResp
// @Router /chat/conversations/file [post]
func (cs *ChatService) ChatFile() gin.HandlerFunc {
return func(c *gin.Context) {
req := chat.ConversationFilesReqBody{
StartTime: time.Now(),
}
if err := c.ShouldBind(&req); err != nil {
klog.FromContext(c.Request.Context()).Error(err, "conversationFileHandler: error binding json")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
req.AppNamespace = NamespaceInHeader(c)
app, err := cs.server.GetApp(c.Request.Context(), req.APPName, req.AppNamespace)
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "conversationFileHandler: error get app")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
if !pointer.BoolDeref(app.Spec.EnableUploadFile, true) {
c.JSON(http.StatusForbidden, chat.ErrorResp{Err: "file upload is not enabled"})
return
}
req.Debug = c.Query("debug") == "true"
// check if this is a new chat
req.NewChat = len(req.ConversationID) == 0
if req.NewChat {
req.ConversationID = string(uuid.NewUUID())
}
file, err := c.FormFile("file")
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "error receive conversational file")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
messageID := string(uuid.NewUUID())
// Upload the file to specific dst.
resp, err := cs.server.ReceiveConversationFile(c.Request.Context(), messageID, req, file)
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "error receive conversational file")
c.JSON(http.StatusInternalServerError, chat.ErrorResp{Err: err.Error()})
return
}
c.JSON(http.StatusOK, resp)
klog.FromContext(c.Request.Context()).V(3).Info("receive conversational file done", "req", req)
}
}
// @Summary list all conversations
// @Schemes
// @Description list all conversations
// @Tags application
// @Accept json
// @Produce json
// @Param namespace header string true "namespace this request is in"
// @Param request body chat.APPMetadata false "query params, if not set will return all current user's conversations"
// @Success 200 {object} []storage.Conversation
// @Failure 400 {object} chat.ErrorResp
// @Failure 500 {object} chat.ErrorResp
// @Router /chat/conversations [post]
func (cs *ChatService) ListConversationHandler() gin.HandlerFunc {
return func(c *gin.Context) {
req := chat.APPMetadata{}
_ = c.ShouldBindJSON(&req)
req.AppNamespace = NamespaceInHeader(c)
resp, err := cs.server.ListConversations(c.Request.Context(), req)
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "error list conversation")
c.JSON(http.StatusInternalServerError, chat.ErrorResp{Err: err.Error()})
return
}
if err := cs.server.FillAppIconToConversations(c.Request.Context(), &resp); err != nil {
// note: fill app icon is try our best, don't need to return error
klog.FromContext(c.Request.Context()).Error(err, "error fill app icon to conversations")
}
klog.FromContext(c.Request.Context()).V(3).Info("list conversation done", "req", req)
c.JSON(http.StatusOK, resp)
}
}
// @Summary delete one conversation
// @Schemes
// @Description delete one conversation
// @Tags application
// @Accept json
// @Produce json
// @Param conversationID path string true "conversationID"
// @Success 200 {object} chat.SimpleResp
// @Failure 400 {object} chat.ErrorResp
// @Failure 500 {object} chat.ErrorResp
// @Router /chat/conversations/:conversationID [delete]
func (cs *ChatService) DeleteConversationHandler() gin.HandlerFunc {
return func(c *gin.Context) {
conversationID := c.Param("conversationID")
if conversationID == "" {
err := errors.New("conversationID is required")
klog.FromContext(c.Request.Context()).Error(err, "conversationID is required")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
err := cs.server.DeleteConversation(c.Request.Context(), conversationID)
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "error delete conversation")
c.JSON(http.StatusInternalServerError, chat.ErrorResp{Err: err.Error()})
return
}
klog.FromContext(c.Request.Context()).V(3).Info("delete conversation done", "conversationID", conversationID)
c.JSON(http.StatusOK, chat.SimpleResp{Message: "ok"})
}
}
// @Summary get all messages history for one conversation
// @Schemes
// @Description get all messages history for one conversation
// @Tags application
// @Accept json
// @Produce json
// @Param namespace header string true "namespace this request is in"
// @Param request body chat.ConversationReqBody true "query params"
// @Success 200 {object} storage.Conversation
// @Failure 400 {object} chat.ErrorResp
// @Failure 500 {object} chat.ErrorResp
// @Router /chat/messages [post]
func (cs *ChatService) HistoryHandler() gin.HandlerFunc {
return func(c *gin.Context) {
req := chat.ConversationReqBody{}
if err := c.ShouldBindJSON(&req); err != nil {
klog.FromContext(c.Request.Context()).Error(err, "historyHandler: error binding json")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
req.AppNamespace = NamespaceInHeader(c)
resp, err := cs.server.ListMessages(c.Request.Context(), req)
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "error list messages")
c.JSON(http.StatusInternalServerError, chat.ErrorResp{Err: err.Error()})
return
}
c.JSON(http.StatusOK, resp)
klog.FromContext(c.Request.Context()).V(3).Info("get message history done", "req", req)
}
}
// @Summary get one message references
// @Schemes
// @Description get one message's references
// @Tags application
// @Accept json
// @Produce json
// @Param namespace header string true "namespace this request is in"
// @Param messageID path string true "messageID"
// @Param request body chat.MessageReqBody true "query params"
// @Success 200 {object} []retriever.Reference
// @Failure 400 {object} chat.ErrorResp
// @Failure 500 {object} chat.ErrorResp
// @Router /chat/messages/:messageID/references [post]
func (cs *ChatService) ReferenceHandler() gin.HandlerFunc {
return func(c *gin.Context) {
messageID := c.Param("messageID")
if messageID == "" {
err := errors.New("messageID is required")
klog.FromContext(c.Request.Context()).Error(err, "messageID is required")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
req := chat.MessageReqBody{
MessageID: messageID,
}
req.AppNamespace = NamespaceInHeader(c)
if err := c.ShouldBindJSON(&req); err != nil {
klog.FromContext(c.Request.Context()).Error(err, "referenceHandler: error binding json")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
resp, err := cs.server.GetMessageReferences(c.Request.Context(), req)
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "error get message references")
c.JSON(http.StatusInternalServerError, chat.ErrorResp{Err: err.Error()})
return
}
klog.FromContext(c.Request.Context()).V(3).Info("get message references done", "req", req)
c.JSON(http.StatusOK, resp)
}
}
// @Summary get app's prompt starters
// @Schemes
// @Description get app's prompt starters
// @Tags application
// @Accept json
// @Produce json
// @Param namespace header string true "namespace this request is in"
// @Param limit query int false "how many prompts you need should > 0 and < 10"
// @Param request body chat.APPMetadata true "query params"
// @Success 200 {object} []string
// @Failure 400 {object} chat.ErrorResp
// @Failure 500 {object} chat.ErrorResp
// @Router /chat/prompt-starter [post]
func (cs *ChatService) PromptStartersHandler() gin.HandlerFunc {
return func(c *gin.Context) {
req := chat.APPMetadata{}
if err := c.ShouldBindJSON(&req); err != nil {
klog.FromContext(c.Request.Context()).Error(err, "PromptStartersHandler: error binding json")
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
req.AppNamespace = NamespaceInHeader(c)
limit := c.Query("limit")
limitVal := PromptLimit
if limit != "" {
var err error
limitVal, err = strconv.Atoi(limit)
if err != nil {
c.JSON(http.StatusBadRequest, chat.ErrorResp{Err: err.Error()})
return
}
if limitVal > 10 || limitVal < 1 {
limitVal = PromptLimit
}
}
resp, err := cs.server.ListPromptStarters(c.Request.Context(), req, limitVal)
if err != nil {
klog.FromContext(c.Request.Context()).Error(err, "error get Prompt Starters")
c.JSON(http.StatusInternalServerError, chat.ErrorResp{Err: err.Error()})
return
}
klog.FromContext(c.Request.Context()).V(3).Info("get Prompt Starters done", "req", req)
c.JSON(http.StatusOK, resp)
}
}
func NamespaceInHeader(c *gin.Context) string {
return c.GetHeader(namespaceHeader)
}
func registerChat(g *gin.RouterGroup, conf config.ServerConfig) {
c, err := client.GetClient(nil)
if err != nil {
panic(err)
}
chatService, err := NewChatService(c, false)
if err != nil {
panic(err)
}
g.POST("", auth.AuthInterceptor(conf.EnableOIDC, oidc.Verifier, v1alpha1.GroupVersion, "get", "applications"), requestid.RequestIDInterceptor(), chatService.ChatHandler()) // chat with bot
g.POST("/conversations/file", auth.AuthInterceptor(conf.EnableOIDC, oidc.Verifier, v1alpha1.GroupVersion, "get", "applications"), requestid.RequestIDInterceptor(), chatService.ChatFile()) // upload fles for conversation
g.POST("/conversations", auth.AuthInterceptor(conf.EnableOIDC, oidc.Verifier, v1alpha1.GroupVersion, "get", "applications"), requestid.RequestIDInterceptor(), chatService.ListConversationHandler()) // list conversations
g.DELETE("/conversations/:conversationID", auth.AuthInterceptor(conf.EnableOIDC, oidc.Verifier, v1alpha1.GroupVersion, "get", "applications"), requestid.RequestIDInterceptor(), chatService.DeleteConversationHandler()) // delete conversation
g.POST("/messages", auth.AuthInterceptor(conf.EnableOIDC, oidc.Verifier, v1alpha1.GroupVersion, "get", "applications"), requestid.RequestIDInterceptor(), chatService.HistoryHandler()) // messages history
g.POST("/messages/:messageID/references", auth.AuthInterceptor(conf.EnableOIDC, oidc.Verifier, v1alpha1.GroupVersion, "get", "applications"), requestid.RequestIDInterceptor(), chatService.ReferenceHandler()) // messages reference
g.POST("/prompt-starter", auth.AuthInterceptor(conf.EnableOIDC, oidc.Verifier, v1alpha1.GroupVersion, "get", "applications"), requestid.RequestIDInterceptor(), chatService.PromptStartersHandler())
}