Skip to content

Commit

Permalink
chore: update quota calc logic (close lobehub#599) (lobehub#627)
Browse files Browse the repository at this point in the history
* fix: change quota calc code (close lobehub#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 <songquanpeng@foxmail.com>
  • Loading branch information
wzxjohn and songquanpeng authored Nov 5, 2023
1 parent 3fe76c8 commit bc7c910
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions controller/relay-text.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"math"
"net/http"
"one-api/common"
"one-api/model"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit bc7c910

Please sign in to comment.