From 36c27d609223882b3970f7f60d769a0593dcf1ce Mon Sep 17 00:00:00 2001 From: RockYang Date: Wed, 15 May 2024 15:30:34 +0800 Subject: [PATCH] handler chat error in the chat entry func --- api/handler/chatimpl/azure_handler.go | 4 +--- api/handler/chatimpl/baidu_handler.go | 4 ++-- api/handler/chatimpl/chatglm_handler.go | 3 +-- api/handler/chatimpl/openai_handler.go | 11 +++-------- api/handler/chatimpl/qwen_handler.go | 3 +-- api/handler/chatimpl/xunfei_handler.go | 4 ++-- api/service/license_service.go | 8 ++++---- 7 files changed, 14 insertions(+), 23 deletions(-) diff --git a/api/handler/chatimpl/azure_handler.go b/api/handler/chatimpl/azure_handler.go index 09347698..ac5ad7ff 100644 --- a/api/handler/chatimpl/azure_handler.go +++ b/api/handler/chatimpl/azure_handler.go @@ -42,12 +42,10 @@ func (h *ChatHandler) sendAzureMessage( logger.Info("HTTP请求完成,耗时:", time.Now().Sub(start)) if err != nil { if strings.Contains(err.Error(), "context canceled") { - logger.Info("用户取消了请求:", prompt) - return nil + return fmt.Errorf("用户取消了请求:%s", prompt) } else if strings.Contains(err.Error(), "no available key") { return errors.New("抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!") } - return err } else { defer response.Body.Close() diff --git a/api/handler/chatimpl/baidu_handler.go b/api/handler/chatimpl/baidu_handler.go index 42d3e90d..6e591a33 100644 --- a/api/handler/chatimpl/baidu_handler.go +++ b/api/handler/chatimpl/baidu_handler.go @@ -58,9 +58,9 @@ func (h *ChatHandler) sendBaiduMessage( response, err := h.doRequest(ctx, req, session, &apiKey) logger.Info("HTTP请求完成,耗时:", time.Now().Sub(start)) if err != nil { + logger.Error(err) if strings.Contains(err.Error(), "context canceled") { - logger.Info("用户取消了请求:", prompt) - return nil + return fmt.Errorf("用户取消了请求:%s", prompt) } else if strings.Contains(err.Error(), "no available key") { return errors.New("抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!") } diff --git a/api/handler/chatimpl/chatglm_handler.go b/api/handler/chatimpl/chatglm_handler.go index 647d5780..53e83bcb 100644 --- a/api/handler/chatimpl/chatglm_handler.go +++ b/api/handler/chatimpl/chatglm_handler.go @@ -43,8 +43,7 @@ func (h *ChatHandler) sendChatGLMMessage( logger.Info("HTTP请求完成,耗时:", time.Now().Sub(start)) if err != nil { if strings.Contains(err.Error(), "context canceled") { - logger.Info("用户取消了请求:", prompt) - return nil + return fmt.Errorf("用户取消了请求:%s", prompt) } else if strings.Contains(err.Error(), "no available key") { return errors.New("抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!") } diff --git a/api/handler/chatimpl/openai_handler.go b/api/handler/chatimpl/openai_handler.go index 5078d102..3878c46c 100644 --- a/api/handler/chatimpl/openai_handler.go +++ b/api/handler/chatimpl/openai_handler.go @@ -42,16 +42,11 @@ func (h *ChatHandler) sendOpenAiMessage( response, err := h.doRequest(ctx, req, session, &apiKey) logger.Info("HTTP请求完成,耗时:", time.Now().Sub(start)) if err != nil { - logger.Error(err) if strings.Contains(err.Error(), "context canceled") { - logger.Info("用户取消了请求:", prompt) - return nil + return fmt.Errorf("用户取消了请求:%s", prompt) } else if strings.Contains(err.Error(), "no available key") { - utils.ReplyMessage(ws, "抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!") - return nil + return errors.New("抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!") } - - utils.ReplyMessage(ws, err.Error()) return err } else { defer response.Body.Close() @@ -82,7 +77,7 @@ func (h *ChatHandler) sendOpenAiMessage( if len(responseBody.Choices) == 0 { // Fixed: 兼容 Azure API 第一个输出空行 continue } - + if responseBody.Choices[0].FinishReason == "stop" && len(contents) == 0 { utils.ReplyMessage(ws, "抱歉😔😔😔,AI助手由于未知原因已经停止输出内容。") break diff --git a/api/handler/chatimpl/qwen_handler.go b/api/handler/chatimpl/qwen_handler.go index 0d8bfa64..7b7eb1ac 100644 --- a/api/handler/chatimpl/qwen_handler.go +++ b/api/handler/chatimpl/qwen_handler.go @@ -57,8 +57,7 @@ func (h *ChatHandler) sendQWenMessage( logger.Info("HTTP请求完成,耗时:", time.Now().Sub(start)) if err != nil { if strings.Contains(err.Error(), "context canceled") { - logger.Info("用户取消了请求:", prompt) - return nil + return fmt.Errorf("用户取消了请求:%s", prompt) } else if strings.Contains(err.Error(), "no available key") { return errors.New("抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!") } diff --git a/api/handler/chatimpl/xunfei_handler.go b/api/handler/chatimpl/xunfei_handler.go index bbe668e2..afc1c165 100644 --- a/api/handler/chatimpl/xunfei_handler.go +++ b/api/handler/chatimpl/xunfei_handler.go @@ -13,6 +13,7 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" + "errors" "fmt" "geekai/core/types" "geekai/store/model" @@ -87,8 +88,7 @@ func (h *ChatHandler) sendXunFeiMessage( res = h.DB.Where("platform", session.Model.Platform).Where("type", "chat").Where("enabled", true).Order("last_used_at ASC").First(&apiKey) } if res.Error != nil { - utils.ReplyMessage(ws, "抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!") - return nil + return errors.New("抱歉😔😔😔,系统已经没有可用的 API KEY,请联系管理员!") } // 更新 API KEY 的最后使用时间 h.DB.Model(&apiKey).UpdateColumn("last_used_at", time.Now().Unix()) diff --git a/api/service/license_service.go b/api/service/license_service.go index 47b1f420..94e08c0b 100644 --- a/api/service/license_service.go +++ b/api/service/license_service.go @@ -23,7 +23,7 @@ import ( type LicenseService struct { config types.ApiConfig levelDB *store.LevelDB - license *types.License + license *types.License urlWhiteList []string machineId string } @@ -40,7 +40,7 @@ func NewLicenseService(server *core.AppServer, levelDB *store.LevelDB) *LicenseS return &LicenseService{ config: server.Config.ApiConfig, levelDB: levelDB, - license: &license, + license: &license, machineId: machineId, } } @@ -186,13 +186,13 @@ func (s *LicenseService) IsValidApiURL(uri string) error { return nil } - if s.urlWhiteList == nil || len(s.urlWhiteList) == 0 { + if len(s.urlWhiteList) == 0 { urls, err := s.fetchUrlWhiteList() if err == nil { s.urlWhiteList = urls } } - + for _, v := range s.urlWhiteList { if strings.HasPrefix(uri, v) { return nil