From bc7c9105f4b6cb18d7302415567d502e5681381e Mon Sep 17 00:00:00 2001 From: wzxjohn Date: Sun, 5 Nov 2023 19:15:06 +0800 Subject: [PATCH] chore: update quota calc logic (close #599) (#627) * fix: change quota calc code (close #599) Use float64 during calc and do math.Ceil after calc. This will result in the quota being used slightly more than the official standard, but it will be guaranteed that it will not be less. * chore: remove blank line --------- Co-authored-by: JustSong --- controller/relay-text.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/controller/relay-text.go b/controller/relay-text.go index 25b8bc06..a61c6f7c 100644 --- a/controller/relay-text.go +++ b/controller/relay-text.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "math" "net/http" "one-api/common" "one-api/model" @@ -414,9 +415,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode { completionRatio := common.GetCompletionRatio(textRequest.Model) promptTokens = textResponse.Usage.PromptTokens completionTokens = textResponse.Usage.CompletionTokens - - quota = promptTokens + int(float64(completionTokens)*completionRatio) - quota = int(float64(quota) * ratio) + quota = int(math.Ceil((float64(promptTokens) + float64(completionTokens)*completionRatio) * ratio)) if ratio != 0 && quota <= 0 { quota = 1 }