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
46c7a966
authored
Jan 21, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 请求出现 0 token的时候,加入错误提示并打印日志
parent
a5d17781
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
20 deletions
+24
-20
controller/relay-text.go
+24
-20
No files found.
controller/relay-text.go
View file @
46c7a966
...
...
@@ -474,6 +474,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
defer
func
(
ctx
context
.
Context
)
{
// c.Writer.Flush()
go
func
()
{
useTimeSeconds
:=
time
.
Now
()
.
Unix
()
-
startTime
.
Unix
()
promptTokens
=
textResponse
.
Usage
.
PromptTokens
completionTokens
=
textResponse
.
Usage
.
CompletionTokens
...
...
@@ -489,38 +490,41 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
quota
=
int
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatio
)
}
totalTokens
:=
promptTokens
+
completionTokens
if
totalTokens
==
0
{
// in this case, must be some error happened
// we cannot just return, because we may have to return the pre-consumed quota
quota
=
0
}
quotaDelta
:=
quota
-
preConsumedQuota
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
err
=
model
.
CacheUpdateUserQuota
(
userId
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error update user quota cache: "
+
err
.
Error
())
}
// record all the consume log even if quota is 0
useTimeSeconds
:=
time
.
Now
()
.
Unix
()
-
startTime
.
Unix
()
var
logContent
string
if
modelPrice
==
-
1
{
logContent
=
fmt
.
Sprintf
(
"模型倍率 %.2f,分组倍率 %.2f"
,
modelRatio
,
groupRatio
)
}
else
{
logContent
=
fmt
.
Sprintf
(
"模型价格 %.2f,分组倍率 %.2f"
,
modelPrice
,
groupRatio
)
}
// record all the consume log even if quota is 0
if
totalTokens
==
0
{
// in this case, must be some error happened
// we cannot just return, because we may have to return the pre-consumed quota
quota
=
0
logContent
+=
fmt
.
Sprintf
(
"(有疑问请联系管理员)"
)
common
.
LogError
(
ctx
,
fmt
.
Sprintf
(
"total tokens is 0, cannot consume quota, userId %d, channelId %d, tokenId %d, model %s, pre-consumed quota %d"
,
userId
,
channelId
,
tokenId
,
textRequest
.
Model
,
preConsumedQuota
))
}
else
{
quotaDelta
:=
quota
-
preConsumedQuota
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
err
=
model
.
CacheUpdateUserQuota
(
userId
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error update user quota cache: "
+
err
.
Error
())
}
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
model
.
UpdateChannelUsedQuota
(
channelId
,
quota
)
}
logModel
:=
textRequest
.
Model
if
strings
.
HasPrefix
(
logModel
,
"gpt-4-gizmo"
)
{
logModel
=
"gpt-4-gizmo-*"
logContent
+=
fmt
.
Sprintf
(
",模型 %s"
,
textRequest
.
Model
)
}
model
.
RecordConsumeLog
(
ctx
,
userId
,
channelId
,
promptTokens
,
completionTokens
,
logModel
,
tokenName
,
quota
,
logContent
,
tokenId
,
userQuota
,
int
(
useTimeSeconds
),
isStream
)
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
model
.
UpdateChannelUsedQuota
(
channelId
,
quota
)
//if quota != 0 {
//
//}
...
...
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