From cad6f9d3ba04643c059d4421d9339c7a46881683 Mon Sep 17 00:00:00 2001 From: "Laisky.Cai" Date: Fri, 8 Mar 2024 03:22:29 +0000 Subject: [PATCH] fix: Improve quota consumption and logging in relay controller - Modify `PostConsumeQuota` function in `common.go` to check if `totalQuota` is less than 0 and add error logging - Update log content in `RecordConsumeLog` function of `common.go` to include model ratio and group ratio - Change the method of calculating user used quota and request count in `UpdateUserUsedQuotaAndRequestCount` function of `common.go` - Implement calculation of `completionRatio` and update `quota` calculation based on `completionRatio` in `postConsumeQuota` function of `helper.go` - Implement error handling for `model.PostConsumeTokenQuota` and `model.CacheUpdateUserQuota` in `helper.go` - Update `UserUsedQuota` and `ChannelUsedQuota` with new `quota` value in `helper.go` --- relay/controller/helper.go | 1 + relay/util/common.go | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/relay/controller/helper.go b/relay/controller/helper.go index 89fc69ce..5f452c16 100644 --- a/relay/controller/helper.go +++ b/relay/controller/helper.go @@ -172,6 +172,7 @@ func postConsumeQuota(ctx context.Context, usage *relaymodel.Usage, meta *util.R if err != nil { logger.Error(ctx, "error update user quota cache: "+err.Error()) } + logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f", modelRatio, groupRatio, completionRatio) model.RecordConsumeLog(ctx, meta.UserId, meta.ChannelId, promptTokens, completionTokens, textRequest.Model, meta.TokenName, quota, logContent) model.UpdateUserUsedQuotaAndRequestCount(meta.UserId, quota) diff --git a/relay/util/common.go b/relay/util/common.go index 6d993378..2eef88a6 100644 --- a/relay/util/common.go +++ b/relay/util/common.go @@ -147,13 +147,14 @@ func PostConsumeQuota(ctx context.Context, tokenId int, quotaDelta int, totalQuo logger.SysError("error update user quota cache: " + err.Error()) } // totalQuota is total quota consumed - if totalQuota != 0 { + if totalQuota >= 0 { logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f", modelRatio, groupRatio) model.RecordConsumeLog(ctx, userId, channelId, totalQuota, 0, modelName, tokenName, totalQuota, logContent) model.UpdateUserUsedQuotaAndRequestCount(userId, totalQuota) model.UpdateChannelUsedQuota(channelId, totalQuota) } - if totalQuota <= 0 { + + if totalQuota < 0 { logger.Error(ctx, fmt.Sprintf("totalQuota consumed is %d, something is wrong", totalQuota)) } }