From f0ffe144371a4e118478306b906ddb7a5f027024 Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Thu, 11 Jan 2024 14:53:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=AB=98=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E4=B8=8B=EF=BC=8C=E9=AB=98=E9=A2=9D=E5=BA=A6=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BD=BF=E7=94=A8=E4=BD=8E=E9=A2=9D=E5=BA=A6=E4=BB=A4?= =?UTF-8?q?=E7=89=8C=E6=B2=A1=E6=9C=89=E9=A2=84=E6=89=A3=E8=B4=B9=E8=80=8C?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=BB=A4=E7=89=8C=E5=A4=A7=E9=A2=9D=E6=AC=A0?= =?UTF-8?q?=E8=B4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/relay-text.go | 24 +++++++++++++++--------- middleware/auth.go | 4 ++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/controller/relay-text.go b/controller/relay-text.go index 64338545..5a7544fb 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -53,6 +53,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { tokenId := c.GetInt("token_id") userId := c.GetInt("id") group := c.GetString("group") + tokenUnlimited := c.GetBool("token_unlimited_quota") var textRequest GeneralOpenAIRequest err := common.UnmarshalBodyReusable(c, &textRequest) if err != nil { @@ -240,15 +241,20 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { return errorWrapper(err, "decrease_user_quota_failed", http.StatusInternalServerError) } if userQuota > 100*preConsumedQuota { - // in this case, we do not pre-consume quota - // because the user has enough quota - preConsumedQuota = 0 - common.LogInfo(c.Request.Context(), fmt.Sprintf("user %d has enough quota %d, trusted and no need to pre-consume", userId, userQuota)) - } - if preConsumedQuota > 0 { - err := model.PreConsumeTokenQuota(tokenId, preConsumedQuota) - if err != nil { - return errorWrapper(err, "pre_consume_token_quota_failed", http.StatusForbidden) + // 用户额度充足,判断令牌额度是否充足 + if !tokenUnlimited { + // 非无限令牌,判断令牌额度是否充足 + tokenQuota := c.GetInt("token_quota") + if tokenQuota > 100*preConsumedQuota { + // 令牌额度充足,信任令牌 + preConsumedQuota = 0 + common.LogInfo(c.Request.Context(), fmt.Sprintf("user %d quota %d and token %d quota %d are enough, trusted and no need to pre-consume", userId, userQuota, tokenId, tokenQuota)) + } + } else { + // in this case, we do not pre-consume quota + // because the user has enough quota + preConsumedQuota = 0 + common.LogInfo(c.Request.Context(), fmt.Sprintf("user %d with unlimited token has enough quota %d, trusted and no need to pre-consume", userId, userQuota)) } } var requestBody io.Reader diff --git a/middleware/auth.go b/middleware/auth.go index ad7e64b7..b3ad67b4 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -106,6 +106,10 @@ func TokenAuth() func(c *gin.Context) { c.Set("id", token.UserId) c.Set("token_id", token.Id) c.Set("token_name", token.Name) + c.Set("token_unlimited_quota", token.UnlimitedQuota) + if !token.UnlimitedQuota { + c.Set("token_quota", token.RemainQuota) + } if len(parts) > 1 { if model.IsAdmin(token.UserId) { c.Set("channelId", parts[1])