From 3bb5b66e8bfc2ee3f0d158c6ddfac8c3950af688 Mon Sep 17 00:00:00 2001 From: RockYang Date: Tue, 16 Apr 2024 23:49:56 +0800 Subject: [PATCH] fixed bug for chat handler doRequest method --- api/handler/chatimpl/chat_handler.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/api/handler/chatimpl/chat_handler.go b/api/handler/chatimpl/chat_handler.go index dbb9f682..f65d72f8 100644 --- a/api/handler/chatimpl/chat_handler.go +++ b/api/handler/chatimpl/chat_handler.go @@ -466,15 +466,14 @@ func (h *ChatHandler) StopGenerate(c *gin.Context) { // useOwnApiKey: 是否使用了用户自己的 API KEY func (h *ChatHandler) doRequest(ctx context.Context, req types.ApiRequest, session *types.ChatSession, apiKey *model.ApiKey) (*http.Response, error) { // if the chat model bind a KEY, use it directly - var res *gorm.DB if session.Model.KeyId > 0 { - res = h.DB.Where("id", session.Model.KeyId).Find(apiKey) + h.DB.Debug().Where("id", session.Model.KeyId).Find(apiKey) } // use the last unused key - if res.Error != nil { - res = h.DB.Where("platform = ?", session.Model.Platform).Where("type = ?", "chat").Where("enabled = ?", true).Order("last_used_at ASC").First(apiKey) + if apiKey.Id == 0 { + h.DB.Debug().Where("platform = ?", session.Model.Platform).Where("type = ?", "chat").Where("enabled = ?", true).Order("last_used_at ASC").First(apiKey) } - if res.Error != nil { + if apiKey.Id == 0 { return nil, errors.New("no available key, please import key") } var apiURL string