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
2d668d77
authored
Feb 29, 2024
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: fix preConsumeQuota error
parent
436afe92
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
8 deletions
+7
-8
relay/relay-text.go
+7
-8
No files found.
relay/relay-text.go
View file @
2d668d77
...
@@ -113,7 +113,7 @@ func TextHelper(c *gin.Context) *dto.OpenAIErrorWithStatusCode {
...
@@ -113,7 +113,7 @@ func TextHelper(c *gin.Context) *dto.OpenAIErrorWithStatusCode {
}
}
// pre-consume quota 预消耗配额
// pre-consume quota 预消耗配额
userQuota
,
openaiErr
:=
preConsumeQuota
(
c
,
preConsumedQuota
,
relayInfo
)
preConsumedQuota
,
userQuota
,
openaiErr
:=
preConsumeQuota
(
c
,
preConsumedQuota
,
relayInfo
)
if
err
!=
nil
{
if
err
!=
nil
{
return
openaiErr
return
openaiErr
}
}
...
@@ -153,7 +153,6 @@ func TextHelper(c *gin.Context) *dto.OpenAIErrorWithStatusCode {
...
@@ -153,7 +153,6 @@ func TextHelper(c *gin.Context) *dto.OpenAIErrorWithStatusCode {
if
openaiErr
!=
nil
{
if
openaiErr
!=
nil
{
return
openaiErr
return
openaiErr
}
}
postConsumeQuota
(
c
,
relayInfo
,
*
textRequest
,
usage
,
ratio
,
preConsumedQuota
,
userQuota
,
modelRatio
,
groupRatio
,
modelPrice
)
postConsumeQuota
(
c
,
relayInfo
,
*
textRequest
,
usage
,
ratio
,
preConsumedQuota
,
userQuota
,
modelRatio
,
groupRatio
,
modelPrice
)
return
nil
return
nil
}
}
...
@@ -178,17 +177,17 @@ func getPromptTokens(textRequest *dto.GeneralOpenAIRequest, info *relaycommon.Re
...
@@ -178,17 +177,17 @@ func getPromptTokens(textRequest *dto.GeneralOpenAIRequest, info *relaycommon.Re
}
}
// 预扣费并返回用户剩余配额
// 预扣费并返回用户剩余配额
func
preConsumeQuota
(
c
*
gin
.
Context
,
preConsumedQuota
int
,
relayInfo
*
relaycommon
.
RelayInfo
)
(
int
,
*
dto
.
OpenAIErrorWithStatusCode
)
{
func
preConsumeQuota
(
c
*
gin
.
Context
,
preConsumedQuota
int
,
relayInfo
*
relaycommon
.
RelayInfo
)
(
int
,
int
,
*
dto
.
OpenAIErrorWithStatusCode
)
{
userQuota
,
err
:=
model
.
CacheGetUserQuota
(
relayInfo
.
UserId
)
userQuota
,
err
:=
model
.
CacheGetUserQuota
(
relayInfo
.
UserId
)
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
service
.
OpenAIErrorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
return
0
,
0
,
service
.
OpenAIErrorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
}
}
if
userQuota
<
0
||
userQuota
-
preConsumedQuota
<
0
{
if
userQuota
<
0
||
userQuota
-
preConsumedQuota
<
0
{
return
0
,
service
.
OpenAIErrorWrapper
(
errors
.
New
(
"user quota is not enough"
),
"insufficient_user_quota"
,
http
.
StatusForbidden
)
return
0
,
0
,
service
.
OpenAIErrorWrapper
(
errors
.
New
(
"user quota is not enough"
),
"insufficient_user_quota"
,
http
.
StatusForbidden
)
}
}
err
=
model
.
CacheDecreaseUserQuota
(
relayInfo
.
UserId
,
preConsumedQuota
)
err
=
model
.
CacheDecreaseUserQuota
(
relayInfo
.
UserId
,
preConsumedQuota
)
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
service
.
OpenAIErrorWrapper
(
err
,
"decrease_user_quota_failed"
,
http
.
StatusInternalServerError
)
return
0
,
0
,
service
.
OpenAIErrorWrapper
(
err
,
"decrease_user_quota_failed"
,
http
.
StatusInternalServerError
)
}
}
if
userQuota
>
100
*
preConsumedQuota
{
if
userQuota
>
100
*
preConsumedQuota
{
// 用户额度充足,判断令牌额度是否充足
// 用户额度充足,判断令牌额度是否充足
...
@@ -210,10 +209,10 @@ func preConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo
...
@@ -210,10 +209,10 @@ func preConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo
if
preConsumedQuota
>
0
{
if
preConsumedQuota
>
0
{
userQuota
,
err
=
model
.
PreConsumeTokenQuota
(
relayInfo
.
TokenId
,
preConsumedQuota
)
userQuota
,
err
=
model
.
PreConsumeTokenQuota
(
relayInfo
.
TokenId
,
preConsumedQuota
)
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
service
.
OpenAIErrorWrapper
(
err
,
"pre_consume_token_quota_failed"
,
http
.
StatusForbidden
)
return
0
,
0
,
service
.
OpenAIErrorWrapper
(
err
,
"pre_consume_token_quota_failed"
,
http
.
StatusForbidden
)
}
}
}
}
return
userQuota
,
nil
return
preConsumedQuota
,
userQuota
,
nil
}
}
func
postConsumeQuota
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
,
textRequest
dto
.
GeneralOpenAIRequest
,
usage
*
dto
.
Usage
,
ratio
float64
,
preConsumedQuota
int
,
userQuota
int
,
modelRatio
float64
,
groupRatio
float64
,
modelPrice
float64
)
{
func
postConsumeQuota
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
,
textRequest
dto
.
GeneralOpenAIRequest
,
usage
*
dto
.
Usage
,
ratio
float64
,
preConsumedQuota
int
,
userQuota
int
,
modelRatio
float64
,
groupRatio
float64
,
modelPrice
float64
)
{
...
...
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