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
672f261c
authored
Mar 04, 2024
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 记录请求的token用量
parent
5cd2c572
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
6 additions
and
4 deletions
+6
-4
model/log.go
+1
-1
model/usedata.go
+5
-3
No files found.
model/log.go
View file @
672f261c
...
...
@@ -85,7 +85,7 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
}
if
common
.
DataExportEnabled
{
common
.
SafeGoroutine
(
func
()
{
LogQuotaData
(
userId
,
username
,
modelName
,
quota
,
common
.
GetTimestamp
())
LogQuotaData
(
userId
,
username
,
modelName
,
quota
,
common
.
GetTimestamp
()
,
promptTokens
+
completionTokens
)
})
}
}
...
...
model/usedata.go
View file @
672f261c
...
...
@@ -15,6 +15,7 @@ type QuotaData struct {
Username
string
`json:"username" gorm:"index:idx_qdt_model_user_name,priority:2;size:64;default:''"`
ModelName
string
`json:"model_name" gorm:"index:idx_qdt_model_user_name,priority:1;size:64;default:''"`
CreatedAt
int64
`json:"created_at" gorm:"bigint;index:idx_qdt_created_at,priority:2"`
TokenUsed
int
`json:"token_used" gorm:"default:0"`
Count
int
`json:"count" gorm:"default:0"`
Quota
int
`json:"quota" gorm:"default:0"`
}
...
...
@@ -38,7 +39,7 @@ func UpdateQuotaData() {
var
CacheQuotaData
=
make
(
map
[
string
]
*
QuotaData
)
var
CacheQuotaDataLock
=
sync
.
Mutex
{}
func
logQuotaDataCache
(
userId
int
,
username
string
,
modelName
string
,
quota
int
,
createdAt
int64
)
{
func
logQuotaDataCache
(
userId
int
,
username
string
,
modelName
string
,
quota
int
,
createdAt
int64
,
tokenUsed
int
)
{
key
:=
fmt
.
Sprintf
(
"%d-%s-%s-%d"
,
userId
,
username
,
modelName
,
createdAt
)
quotaData
,
ok
:=
CacheQuotaData
[
key
]
if
ok
{
...
...
@@ -52,18 +53,19 @@ func logQuotaDataCache(userId int, username string, modelName string, quota int,
CreatedAt
:
createdAt
,
Count
:
1
,
Quota
:
quota
,
TokenUsed
:
tokenUsed
,
}
}
CacheQuotaData
[
key
]
=
quotaData
}
func
LogQuotaData
(
userId
int
,
username
string
,
modelName
string
,
quota
int
,
createdAt
int64
)
{
func
LogQuotaData
(
userId
int
,
username
string
,
modelName
string
,
quota
int
,
createdAt
int64
,
tokenUsed
int
)
{
// 只精确到小时
createdAt
=
createdAt
-
(
createdAt
%
3600
)
CacheQuotaDataLock
.
Lock
()
defer
CacheQuotaDataLock
.
Unlock
()
logQuotaDataCache
(
userId
,
username
,
modelName
,
quota
,
createdAt
)
logQuotaDataCache
(
userId
,
username
,
modelName
,
quota
,
createdAt
,
tokenUsed
)
}
func
SaveQuotaDataCache
()
{
...
...
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