|
@@ -12,6 +12,8 @@ import (
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
+ "wechat-api/ent/employee"
|
|
|
+ "wechat-api/ent/wxcard"
|
|
|
"wechat-api/ent/wxcarduser"
|
|
|
"wechat-api/hook/dify"
|
|
|
"wechat-api/hook/fastgpt"
|
|
@@ -84,14 +86,22 @@ func (l *SubmitApiChatLogic) SubmitApiChat(req *types.ChatRecordsInfo, w http.Re
|
|
|
}
|
|
|
|
|
|
if *req.BotType == 2 { // 从FastGPT里获取回答
|
|
|
- fastgptSendChat(l, w, *req.Content, sessionId, userId)
|
|
|
+ card, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.ID(*req.BotId)).Only(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ return //TODO 这里应该报错
|
|
|
+ }
|
|
|
+ fastgptSendChat(l, w, *req.Content, card.APIBase, card.APIKey, sessionId, userId)
|
|
|
} else if *req.BotType == 3 { // 从数字员工里获取回答
|
|
|
- difySendChat(l, w, *req.Content, sessionId, userId)
|
|
|
+ employee, err := l.svcCtx.DB.Employee.Query().Where(employee.ID(*req.BotId)).Only(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ return //TODO 这里应该报错
|
|
|
+ }
|
|
|
+ difySendChat(l, w, *req.Content, employee.APIBase, employee.APIKey, sessionId, userId)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// fastgptSendChat 往 FastGPT 发送内容并获得响应信息
|
|
|
-func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content string, sessionId, userId uint64) {
|
|
|
+func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiBase, apiKey string, sessionId, userId uint64) {
|
|
|
userInfo, _ := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(userId)).First(l.ctx)
|
|
|
|
|
|
var chatReq fastgpt.ChatReq
|
|
@@ -113,7 +123,8 @@ func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content strin
|
|
|
fmt.Printf("request data:%v\n", string(jsonData))
|
|
|
|
|
|
// 建立HTTP請求
|
|
|
- url := fastgpt.GetBaseUrl() + fastgpt.GetChatUrl() // 替換為正確的FastGPT API端點
|
|
|
+ url := apiBase + fastgpt.GetChatUrl()
|
|
|
+ fmt.Printf("url=%v token=%v\n", url, apiKey)
|
|
|
request, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
|
|
if err != nil {
|
|
|
fmt.Printf("Error creating request:%v", err)
|
|
@@ -122,7 +133,7 @@ func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content strin
|
|
|
|
|
|
// 設置請求頭
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
- request.Header.Set("Authorization", "Bearer "+fastgpt.GetChatToken())
|
|
|
+ request.Header.Set("Authorization", "Bearer "+apiKey)
|
|
|
request.Header.Set("Transfer-Encoding", "chunked")
|
|
|
|
|
|
// 發送請求
|
|
@@ -182,7 +193,7 @@ func fastgptSendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content strin
|
|
|
}
|
|
|
|
|
|
// difySendChat 往 Dify 发送内容并获得响应信息
|
|
|
-func difySendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content string, sessionId, userId uint64) {
|
|
|
+func difySendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content, apiBase, apiKey string, sessionId, userId uint64) {
|
|
|
userInfo, _ := l.svcCtx.DB.WxCardUser.Query().Where(wxcarduser.ID(userId)).First(l.ctx)
|
|
|
|
|
|
var chatReq dify.ChatReq
|
|
@@ -200,7 +211,7 @@ func difySendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content string,
|
|
|
fmt.Printf("request data:%v\n", string(jsonData))
|
|
|
|
|
|
// 建立HTTP請求
|
|
|
- url := dify.GetBaseUrl() + dify.GetChatUrl() // 替換為正確的FastGPT API端點
|
|
|
+ url := apiBase + dify.GetChatUrl() // 替換為正確的FastGPT API端點
|
|
|
request, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
|
|
|
if err != nil {
|
|
|
fmt.Printf("Error creating request:%v", err)
|
|
@@ -209,7 +220,7 @@ func difySendChat(l *SubmitApiChatLogic, w http.ResponseWriter, content string,
|
|
|
|
|
|
// 設置請求頭
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
- request.Header.Set("Authorization", "Bearer "+dify.GetChatToken())
|
|
|
+ request.Header.Set("Authorization", "Bearer "+apiKey)
|
|
|
request.Header.Set("Transfer-Encoding", "chunked")
|
|
|
|
|
|
// 發送請求
|