|
@@ -90,18 +90,18 @@ func (l *SubmitApiChatLogic) SubmitApiChat(req *types.ChatRecordsInfo, w http.Re
|
|
if err != nil {
|
|
if err != nil {
|
|
return //TODO 这里应该报错
|
|
return //TODO 这里应该报错
|
|
}
|
|
}
|
|
- fastgptSendChat(l, w, *req.Content, card.APIBase, card.APIKey, sessionId, userId)
|
|
|
|
|
|
+ fastgptSendChat(l, w, *req.Content, card.APIBase, card.APIKey, sessionId, userId, *req.BotId, *req.BotType)
|
|
} else if *req.BotType == 3 { // 从数字员工里获取回答
|
|
} else if *req.BotType == 3 { // 从数字员工里获取回答
|
|
employee, err := l.svcCtx.DB.Employee.Query().Where(employee.ID(*req.BotId)).Only(l.ctx)
|
|
employee, err := l.svcCtx.DB.Employee.Query().Where(employee.ID(*req.BotId)).Only(l.ctx)
|
|
if err != nil {
|
|
if err != nil {
|
|
return //TODO 这里应该报错
|
|
return //TODO 这里应该报错
|
|
}
|
|
}
|
|
- difySendChat(l, w, *req.Content, employee.APIBase, employee.APIKey, sessionId, userId)
|
|
|
|
|
|
+ difySendChat(l, w, *req.Content, employee.APIBase, employee.APIKey, sessionId, userId, *req.BotId, *req.BotType)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// fastgptSendChat 往 FastGPT 发送内容并获得响应信息
|
|
// fastgptSendChat 往 FastGPT 发送内容并获得响应信息
|
|
-func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiBase, apiKey string, sessionId, userId uint64) {
|
|
|
|
|
|
+func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiBase, apiKey string, sessionId, userId, botId uint64, botType uint8) {
|
|
userInfo, _ := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(userId)).First(l.ctx)
|
|
userInfo, _ := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(userId)).First(l.ctx)
|
|
|
|
|
|
var chatReq fastgpt.ChatReq
|
|
var chatReq fastgpt.ChatReq
|
|
@@ -150,6 +150,13 @@ func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiB
|
|
w.Header().Set("Connection", "keep-alive")
|
|
w.Header().Set("Connection", "keep-alive")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
|
|
|
+ flusher, ok := w.(http.Flusher)
|
|
|
|
+ if !ok {
|
|
|
|
+ http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var answer string
|
|
for {
|
|
for {
|
|
line, err := reader.ReadString('\n')
|
|
line, err := reader.ReadString('\n')
|
|
line = strings.Trim(line, " \n")
|
|
line = strings.Trim(line, " \n")
|
|
@@ -182,10 +189,20 @@ func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiB
|
|
jsonData.Finish = finish
|
|
jsonData.Finish = finish
|
|
lineData, _ := json.Marshal(jsonData)
|
|
lineData, _ := json.Marshal(jsonData)
|
|
|
|
|
|
- _, _ = fmt.Fprintln(w, "data: "+string(lineData)+"\n")
|
|
|
|
|
|
+ // 拼接回答
|
|
|
|
+ answer = answer + jsonData.Answer
|
|
|
|
+
|
|
|
|
+ _, err = fmt.Fprintln(w, "data: "+string(lineData)+"\n")
|
|
fmt.Printf("response=%v\n", string(lineData))
|
|
fmt.Printf("response=%v\n", string(lineData))
|
|
|
|
+ if err != nil {
|
|
|
|
+ logAnswer(l, userId, sessionId, botId, botType, answer)
|
|
|
|
+ fmt.Printf("Error writing to client:%v \n", err)
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ flusher.Flush()
|
|
|
|
|
|
if finish {
|
|
if finish {
|
|
|
|
+ logAnswer(l, userId, sessionId, botId, botType, answer)
|
|
break
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -193,7 +210,7 @@ func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiB
|
|
}
|
|
}
|
|
|
|
|
|
// difySendChat 往 Dify 发送内容并获得响应信息
|
|
// difySendChat 往 Dify 发送内容并获得响应信息
|
|
-func difySendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiBase, apiKey string, sessionId, userId uint64) {
|
|
|
|
|
|
+func difySendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiBase, apiKey string, sessionId, userId, botId uint64, botType uint8) {
|
|
userInfo, _ := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(userId)).First(l.ctx)
|
|
userInfo, _ := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(userId)).First(l.ctx)
|
|
|
|
|
|
var chatReq dify.ChatReq
|
|
var chatReq dify.ChatReq
|
|
@@ -237,6 +254,13 @@ func difySendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiBase
|
|
w.Header().Set("Connection", "keep-alive")
|
|
w.Header().Set("Connection", "keep-alive")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
|
|
|
+ flusher, ok := w.(http.Flusher)
|
|
|
|
+ if !ok {
|
|
|
|
+ http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var answer string
|
|
for {
|
|
for {
|
|
line, err := reader.ReadString('\n')
|
|
line, err := reader.ReadString('\n')
|
|
line = strings.Trim(line, " \n")
|
|
line = strings.Trim(line, " \n")
|
|
@@ -275,12 +299,37 @@ func difySendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiBase
|
|
jsonData.ConversationId = chatData.ConversationId
|
|
jsonData.ConversationId = chatData.ConversationId
|
|
lineData, _ := json.Marshal(jsonData)
|
|
lineData, _ := json.Marshal(jsonData)
|
|
|
|
|
|
- _, _ = fmt.Fprintln(w, "data: "+string(lineData)+"\n")
|
|
|
|
|
|
+ // 拼接回答
|
|
|
|
+ answer = answer + jsonData.Answer
|
|
|
|
+
|
|
|
|
+ _, err = fmt.Fprintln(w, "data: "+string(lineData)+"\n")
|
|
fmt.Printf("response=%v\n", string(lineData))
|
|
fmt.Printf("response=%v\n", string(lineData))
|
|
|
|
+ if err != nil {
|
|
|
|
+ logAnswer(l, userId, sessionId, botId, botType, answer)
|
|
|
|
+ fmt.Printf("Error writing to client:%v \n", err)
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ flusher.Flush()
|
|
|
|
|
|
if finish {
|
|
if finish {
|
|
|
|
+ logAnswer(l, userId, sessionId, botId, botType, answer)
|
|
break
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// logAnswer 保存Ai回答的内容
|
|
|
|
+func logAnswer(l *SubmitApiChatLogic, userId, sessionId, botId uint64, botType uint8, answer string) {
|
|
|
|
+ _, err := l.svcCtx.DB.ChatRecords.Create().
|
|
|
|
+ SetUserID(userId).
|
|
|
|
+ SetSessionID(sessionId).
|
|
|
|
+ SetBotType(botType).
|
|
|
|
+ SetBotID(botId).
|
|
|
|
+ SetContentType(2).
|
|
|
|
+ SetContent(answer).
|
|
|
|
+ Save(l.ctx)
|
|
|
|
+ if err != nil {
|
|
|
|
+ fmt.Printf("save answer failed: %v", err)
|
|
|
|
+ }
|
|
|
|
+}
|