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
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
12 deletions
+16
-12
controller/relay-text.go
+16
-12
No files found.
controller/relay-text.go
View file @
46c7a966
...
@@ -474,6 +474,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -474,6 +474,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
defer
func
(
ctx
context
.
Context
)
{
defer
func
(
ctx
context
.
Context
)
{
// c.Writer.Flush()
// c.Writer.Flush()
go
func
()
{
go
func
()
{
useTimeSeconds
:=
time
.
Now
()
.
Unix
()
-
startTime
.
Unix
()
promptTokens
=
textResponse
.
Usage
.
PromptTokens
promptTokens
=
textResponse
.
Usage
.
PromptTokens
completionTokens
=
textResponse
.
Usage
.
CompletionTokens
completionTokens
=
textResponse
.
Usage
.
CompletionTokens
...
@@ -489,11 +490,21 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -489,11 +490,21 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
quota
=
int
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatio
)
quota
=
int
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatio
)
}
}
totalTokens
:=
promptTokens
+
completionTokens
totalTokens
:=
promptTokens
+
completionTokens
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
{
if
totalTokens
==
0
{
// in this case, must be some error happened
// in this case, must be some error happened
// we cannot just return, because we may have to return the pre-consumed quota
// we cannot just return, because we may have to return the pre-consumed quota
quota
=
0
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
quotaDelta
:=
quota
-
preConsumedQuota
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quotaDelta
,
preConsumedQuota
,
true
)
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -503,24 +514,17 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -503,24 +514,17 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
if
err
!=
nil
{
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error update user quota cache: "
+
err
.
Error
())
common
.
LogError
(
ctx
,
"error update user quota cache: "
+
err
.
Error
())
}
}
model
.
UpdateUserUsedQuotaAndRequestCount
(
userId
,
quota
)
// record all the consume log even if quota is 0
model
.
UpdateChannelUsedQuota
(
channelId
,
quota
)
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
)
}
}
logModel
:=
textRequest
.
Model
logModel
:=
textRequest
.
Model
if
strings
.
HasPrefix
(
logModel
,
"gpt-4-gizmo"
)
{
if
strings
.
HasPrefix
(
logModel
,
"gpt-4-gizmo"
)
{
logModel
=
"gpt-4-gizmo-*"
logModel
=
"gpt-4-gizmo-*"
logContent
+=
fmt
.
Sprintf
(
",模型 %s"
,
textRequest
.
Model
)
logContent
+=
fmt
.
Sprintf
(
",模型 %s"
,
textRequest
.
Model
)
}
}
model
.
RecordConsumeLog
(
ctx
,
userId
,
channelId
,
promptTokens
,
completionTokens
,
logModel
,
tokenName
,
quota
,
logContent
,
tokenId
,
userQuota
,
int
(
useTimeSeconds
),
isStream
)
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 {
//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