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
e1b760d9
authored
Nov 07, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: realtime扣费时检测令牌额度
(cherry picked from commit 91511b8b64fc0d28dbf657cb97e12b7d1e50070d)
parent
f66198b9
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
0 deletions
+11
-0
service/quota.go
+11
-0
No files found.
service/quota.go
View file @
e1b760d9
...
@@ -9,6 +9,7 @@ import (
...
@@ -9,6 +9,7 @@ import (
"one-api/dto"
"one-api/dto"
"one-api/model"
"one-api/model"
relaycommon
"one-api/relay/common"
relaycommon
"one-api/relay/common"
"strings"
"time"
"time"
)
)
...
@@ -20,6 +21,12 @@ func PreWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usag
...
@@ -20,6 +21,12 @@ func PreWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usag
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
token
,
err
:=
model
.
CacheGetTokenByKey
(
strings
.
TrimLeft
(
relayInfo
.
ApiKey
,
"sk-"
))
if
err
!=
nil
{
return
err
}
modelName
:=
relayInfo
.
UpstreamModelName
modelName
:=
relayInfo
.
UpstreamModelName
textInputTokens
:=
usage
.
InputTokenDetails
.
TextTokens
textInputTokens
:=
usage
.
InputTokenDetails
.
TextTokens
textOutTokens
:=
usage
.
OutputTokenDetails
.
TextTokens
textOutTokens
:=
usage
.
OutputTokenDetails
.
TextTokens
...
@@ -46,6 +53,10 @@ func PreWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usag
...
@@ -46,6 +53,10 @@ func PreWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usag
return
errors
.
New
(
fmt
.
Sprintf
(
"用户额度不足,剩余额度为 %d"
,
userQuota
))
return
errors
.
New
(
fmt
.
Sprintf
(
"用户额度不足,剩余额度为 %d"
,
userQuota
))
}
}
if
token
.
RemainQuota
<
quota
{
return
errors
.
New
(
fmt
.
Sprintf
(
"令牌额度不足,剩余额度为 %d"
,
token
.
RemainQuota
))
}
err
=
model
.
PostConsumeTokenQuota
(
relayInfo
,
0
,
quota
,
0
,
false
)
err
=
model
.
PostConsumeTokenQuota
(
relayInfo
,
0
,
quota
,
0
,
false
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
...
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