Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5e060857
authored
Mar 17, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: Improve token quota consumption logic
parent
9b2cc6ad
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
27 deletions
+38
-27
relay/helper/price.go
+13
-2
relay/relay-text.go
+9
-8
service/quota.go
+16
-17
No files found.
relay/helper/price.go
View file @
5e060857
...
@@ -20,6 +20,10 @@ type PriceData struct {
...
@@ -20,6 +20,10 @@ type PriceData struct {
ShouldPreConsumedQuota
int
ShouldPreConsumedQuota
int
}
}
func
(
p
PriceData
)
ToSetting
()
string
{
return
fmt
.
Sprintf
(
"ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f, ShouldPreConsumedQuota: %d"
,
p
.
ModelPrice
,
p
.
ModelRatio
,
p
.
CompletionRatio
,
p
.
CacheRatio
,
p
.
GroupRatio
,
p
.
UsePrice
,
p
.
CacheCreationRatio
,
p
.
ShouldPreConsumedQuota
)
}
func
ModelPriceHelper
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
promptTokens
int
,
maxTokens
int
)
(
PriceData
,
error
)
{
func
ModelPriceHelper
(
c
*
gin
.
Context
,
info
*
relaycommon
.
RelayInfo
,
promptTokens
int
,
maxTokens
int
)
(
PriceData
,
error
)
{
modelPrice
,
usePrice
:=
operation_setting
.
GetModelPrice
(
info
.
OriginModelName
,
false
)
modelPrice
,
usePrice
:=
operation_setting
.
GetModelPrice
(
info
.
OriginModelName
,
false
)
groupRatio
:=
setting
.
GetGroupRatio
(
info
.
Group
)
groupRatio
:=
setting
.
GetGroupRatio
(
info
.
Group
)
...
@@ -50,7 +54,8 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
...
@@ -50,7 +54,8 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
}
else
{
}
else
{
preConsumedQuota
=
int
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatio
)
preConsumedQuota
=
int
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatio
)
}
}
return
PriceData
{
priceData
:=
PriceData
{
ModelPrice
:
modelPrice
,
ModelPrice
:
modelPrice
,
ModelRatio
:
modelRatio
,
ModelRatio
:
modelRatio
,
CompletionRatio
:
completionRatio
,
CompletionRatio
:
completionRatio
,
...
@@ -59,5 +64,11 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
...
@@ -59,5 +64,11 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
CacheRatio
:
cacheRatio
,
CacheRatio
:
cacheRatio
,
CacheCreationRatio
:
cacheCreationRatio
,
CacheCreationRatio
:
cacheCreationRatio
,
ShouldPreConsumedQuota
:
preConsumedQuota
,
ShouldPreConsumedQuota
:
preConsumedQuota
,
},
nil
}
if
common
.
DebugEnabled
{
println
(
fmt
.
Sprintf
(
"model_price_helper result: %s"
,
priceData
.
ToSetting
()))
}
return
priceData
,
nil
}
}
relay/relay-text.go
View file @
5e060857
...
@@ -109,7 +109,7 @@ func TextHelper(c *gin.Context) (openaiErr *dto.OpenAIErrorWithStatusCode) {
...
@@ -109,7 +109,7 @@ func TextHelper(c *gin.Context) (openaiErr *dto.OpenAIErrorWithStatusCode) {
c
.
Set
(
"prompt_tokens"
,
promptTokens
)
c
.
Set
(
"prompt_tokens"
,
promptTokens
)
}
}
priceData
,
err
:=
helper
.
ModelPriceHelper
(
c
,
relayInfo
,
promptTokens
,
int
(
textRequest
.
MaxTokens
))
priceData
,
err
:=
helper
.
ModelPriceHelper
(
c
,
relayInfo
,
promptTokens
,
int
(
math
.
Max
(
float64
(
textRequest
.
MaxTokens
),
float64
(
textRequest
.
MaxCompletionTokens
))
))
if
err
!=
nil
{
if
err
!=
nil
{
return
service
.
OpenAIErrorWrapperLocal
(
err
,
"model_price_error"
,
http
.
StatusInternalServerError
)
return
service
.
OpenAIErrorWrapperLocal
(
err
,
"model_price_error"
,
http
.
StatusInternalServerError
)
}
}
...
@@ -372,17 +372,18 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -372,17 +372,18 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
common
.
LogError
(
ctx
,
fmt
.
Sprintf
(
"total tokens is 0, cannot consume quota, userId %d, channelId %d, "
+
common
.
LogError
(
ctx
,
fmt
.
Sprintf
(
"total tokens is 0, cannot consume quota, userId %d, channelId %d, "
+
"tokenId %d, model %s, pre-consumed quota %d"
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
relayInfo
.
TokenId
,
modelName
,
preConsumedQuota
))
"tokenId %d, model %s, pre-consumed quota %d"
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
relayInfo
.
TokenId
,
modelName
,
preConsumedQuota
))
}
else
{
}
else
{
quotaDelta
:=
quota
-
preConsumedQuota
if
quotaDelta
!=
0
{
err
:=
service
.
PostConsumeQuota
(
relayInfo
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
}
model
.
UpdateUserUsedQuotaAndRequestCount
(
relayInfo
.
UserId
,
quota
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
relayInfo
.
UserId
,
quota
)
model
.
UpdateChannelUsedQuota
(
relayInfo
.
ChannelId
,
quota
)
model
.
UpdateChannelUsedQuota
(
relayInfo
.
ChannelId
,
quota
)
}
}
quotaDelta
:=
quota
-
preConsumedQuota
if
quotaDelta
!=
0
{
err
:=
service
.
PostConsumeQuota
(
relayInfo
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
}
logModel
:=
modelName
logModel
:=
modelName
if
strings
.
HasPrefix
(
logModel
,
"gpt-4-gizmo"
)
{
if
strings
.
HasPrefix
(
logModel
,
"gpt-4-gizmo"
)
{
logModel
=
"gpt-4-gizmo-*"
logModel
=
"gpt-4-gizmo-*"
...
...
service/quota.go
View file @
5e060857
...
@@ -243,20 +243,18 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -243,20 +243,18 @@ func PostClaudeConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
common
.
LogError
(
ctx
,
fmt
.
Sprintf
(
"total tokens is 0, cannot consume quota, userId %d, channelId %d, "
+
common
.
LogError
(
ctx
,
fmt
.
Sprintf
(
"total tokens is 0, cannot consume quota, userId %d, channelId %d, "
+
"tokenId %d, model %s, pre-consumed quota %d"
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
relayInfo
.
TokenId
,
modelName
,
preConsumedQuota
))
"tokenId %d, model %s, pre-consumed quota %d"
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
relayInfo
.
TokenId
,
modelName
,
preConsumedQuota
))
}
else
{
}
else
{
//if sensitiveResp != nil {
// logContent += fmt.Sprintf(",敏感词:%s", strings.Join(sensitiveResp.SensitiveWords, ", "))
//}
quotaDelta
:=
quota
-
preConsumedQuota
if
quotaDelta
!=
0
{
err
:=
PostConsumeQuota
(
relayInfo
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
}
model
.
UpdateUserUsedQuotaAndRequestCount
(
relayInfo
.
UserId
,
quota
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
relayInfo
.
UserId
,
quota
)
model
.
UpdateChannelUsedQuota
(
relayInfo
.
ChannelId
,
quota
)
model
.
UpdateChannelUsedQuota
(
relayInfo
.
ChannelId
,
quota
)
}
}
quotaDelta
:=
quota
-
preConsumedQuota
if
quotaDelta
!=
0
{
err
:=
PostConsumeQuota
(
relayInfo
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
}
other
:=
GenerateClaudeOtherInfo
(
ctx
,
relayInfo
,
modelRatio
,
groupRatio
,
completionRatio
,
other
:=
GenerateClaudeOtherInfo
(
ctx
,
relayInfo
,
modelRatio
,
groupRatio
,
completionRatio
,
cacheTokens
,
cacheRatio
,
cacheCreationTokens
,
cacheCreationRatio
,
modelPrice
)
cacheTokens
,
cacheRatio
,
cacheCreationTokens
,
cacheCreationRatio
,
modelPrice
)
model
.
RecordConsumeLog
(
ctx
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
promptTokens
,
completionTokens
,
modelName
,
model
.
RecordConsumeLog
(
ctx
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
promptTokens
,
completionTokens
,
modelName
,
...
@@ -318,17 +316,18 @@ func PostAudioConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -318,17 +316,18 @@ func PostAudioConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
common
.
LogError
(
ctx
,
fmt
.
Sprintf
(
"total tokens is 0, cannot consume quota, userId %d, channelId %d, "
+
common
.
LogError
(
ctx
,
fmt
.
Sprintf
(
"total tokens is 0, cannot consume quota, userId %d, channelId %d, "
+
"tokenId %d, model %s, pre-consumed quota %d"
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
relayInfo
.
TokenId
,
relayInfo
.
OriginModelName
,
preConsumedQuota
))
"tokenId %d, model %s, pre-consumed quota %d"
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
relayInfo
.
TokenId
,
relayInfo
.
OriginModelName
,
preConsumedQuota
))
}
else
{
}
else
{
quotaDelta
:=
quota
-
preConsumedQuota
if
quotaDelta
!=
0
{
err
:=
PostConsumeQuota
(
relayInfo
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
}
model
.
UpdateUserUsedQuotaAndRequestCount
(
relayInfo
.
UserId
,
quota
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
relayInfo
.
UserId
,
quota
)
model
.
UpdateChannelUsedQuota
(
relayInfo
.
ChannelId
,
quota
)
model
.
UpdateChannelUsedQuota
(
relayInfo
.
ChannelId
,
quota
)
}
}
quotaDelta
:=
quota
-
preConsumedQuota
if
quotaDelta
!=
0
{
err
:=
PostConsumeQuota
(
relayInfo
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
}
logModel
:=
relayInfo
.
OriginModelName
logModel
:=
relayInfo
.
OriginModelName
if
extraContent
!=
""
{
if
extraContent
!=
""
{
logContent
+=
", "
+
extraContent
logContent
+=
", "
+
extraContent
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment