mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-12-16 21:15:56 +08:00
Compare commits
18 Commits
v0.4.7-alp
...
v0.4.8-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6f05128368 | ||
|
|
9b178a28a3 | ||
|
|
4a6a7f4635 | ||
|
|
6b1a24d650 | ||
|
|
94ba3dd024 | ||
|
|
f6eb4e5628 | ||
|
|
57bd907f83 | ||
|
|
dd8e8d5ee8 | ||
|
|
1ca1aa0cdc | ||
|
|
f2ba0c0300 | ||
|
|
f5c1fcd3c3 | ||
|
|
5fdf670a19 | ||
|
|
3ce982d8ee | ||
|
|
a515f9284e | ||
|
|
cccf5e4a07 | ||
|
|
b0bfb9c9a1 | ||
|
|
3aff61a973 | ||
|
|
0fd1ff4d9e |
@@ -32,6 +32,9 @@ func GetSubscription(c *gin.Context) {
|
|||||||
if common.DisplayInCurrencyEnabled {
|
if common.DisplayInCurrencyEnabled {
|
||||||
amount /= common.QuotaPerUnit
|
amount /= common.QuotaPerUnit
|
||||||
}
|
}
|
||||||
|
if token != nil && token.UnlimitedQuota {
|
||||||
|
amount = 100000000
|
||||||
|
}
|
||||||
subscription := OpenAISubscriptionResponse{
|
subscription := OpenAISubscriptionResponse{
|
||||||
Object: "billing_subscription",
|
Object: "billing_subscription",
|
||||||
HasPaymentMethod: true,
|
HasPaymentMethod: true,
|
||||||
@@ -71,7 +74,7 @@ func GetUsage(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
usage := OpenAIUsageResponse{
|
usage := OpenAIUsageResponse{
|
||||||
Object: "list",
|
Object: "list",
|
||||||
TotalUsage: amount,
|
TotalUsage: amount * 100,
|
||||||
}
|
}
|
||||||
c.JSON(200, usage)
|
c.JSON(200, usage)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -13,7 +13,12 @@ func GetAllLogs(c *gin.Context) {
|
|||||||
p = 0
|
p = 0
|
||||||
}
|
}
|
||||||
logType, _ := strconv.Atoi(c.Query("type"))
|
logType, _ := strconv.Atoi(c.Query("type"))
|
||||||
logs, err := model.GetAllLogs(logType, p*common.ItemsPerPage, common.ItemsPerPage)
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
||||||
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
||||||
|
username := c.Query("username")
|
||||||
|
tokenName := c.Query("token_name")
|
||||||
|
modelName := c.Query("model_name")
|
||||||
|
logs, err := model.GetAllLogs(logType, startTimestamp, endTimestamp, modelName, username, tokenName, p*common.ItemsPerPage, common.ItemsPerPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -35,7 +40,11 @@ func GetUserLogs(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
userId := c.GetInt("id")
|
userId := c.GetInt("id")
|
||||||
logType, _ := strconv.Atoi(c.Query("type"))
|
logType, _ := strconv.Atoi(c.Query("type"))
|
||||||
logs, err := model.GetUserLogs(userId, logType, p*common.ItemsPerPage, common.ItemsPerPage)
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
||||||
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
||||||
|
tokenName := c.Query("token_name")
|
||||||
|
modelName := c.Query("model_name")
|
||||||
|
logs, err := model.GetUserLogs(userId, logType, startTimestamp, endTimestamp, modelName, tokenName, p*common.ItemsPerPage, common.ItemsPerPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -84,3 +93,41 @@ func SearchUserLogs(c *gin.Context) {
|
|||||||
"data": logs,
|
"data": logs,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetLogsStat(c *gin.Context) {
|
||||||
|
logType, _ := strconv.Atoi(c.Query("type"))
|
||||||
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
||||||
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
||||||
|
tokenName := c.Query("token_name")
|
||||||
|
username := c.Query("username")
|
||||||
|
modelName := c.Query("model_name")
|
||||||
|
quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
|
||||||
|
//tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, "")
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": "",
|
||||||
|
"data": gin.H{
|
||||||
|
"quota": quotaNum,
|
||||||
|
//"token": tokenNum,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetLogsSelfStat(c *gin.Context) {
|
||||||
|
username := c.GetString("username")
|
||||||
|
logType, _ := strconv.Atoi(c.Query("type"))
|
||||||
|
startTimestamp, _ := strconv.ParseInt(c.Query("start_timestamp"), 10, 64)
|
||||||
|
endTimestamp, _ := strconv.ParseInt(c.Query("end_timestamp"), 10, 64)
|
||||||
|
tokenName := c.Query("token_name")
|
||||||
|
modelName := c.Query("model_name")
|
||||||
|
quotaNum := model.SumUsedQuota(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
|
||||||
|
//tokenNum := model.SumUsedToken(logType, startTimestamp, endTimestamp, modelName, username, tokenName)
|
||||||
|
c.JSON(200, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": "",
|
||||||
|
"data": gin.H{
|
||||||
|
"quota": quotaNum,
|
||||||
|
//"token": tokenNum,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -224,6 +224,24 @@ func init() {
|
|||||||
Root: "text-moderation-stable",
|
Root: "text-moderation-stable",
|
||||||
Parent: nil,
|
Parent: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Id: "text-davinci-edit-001",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "text-davinci-edit-001",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "code-davinci-edit-001",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "code-davinci-edit-001",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
openAIModelsMap = make(map[string]OpenAIModels)
|
openAIModelsMap = make(map[string]OpenAIModels)
|
||||||
for _, model := range openAIModels {
|
for _, model := range openAIModels {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"io"
|
"io"
|
||||||
@@ -26,9 +27,32 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
return errorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
return errorWrapper(err, "bind_request_body_failed", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if relayMode == RelayModeModeration && textRequest.Model == "" {
|
if relayMode == RelayModeModerations && textRequest.Model == "" {
|
||||||
textRequest.Model = "text-moderation-latest"
|
textRequest.Model = "text-moderation-latest"
|
||||||
}
|
}
|
||||||
|
// request validation
|
||||||
|
if textRequest.Model == "" {
|
||||||
|
return errorWrapper(errors.New("model is required"), "required_field_missing", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
switch relayMode {
|
||||||
|
case RelayModeCompletions:
|
||||||
|
if textRequest.Prompt == "" {
|
||||||
|
return errorWrapper(errors.New("field prompt is required"), "required_field_missing", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
case RelayModeChatCompletions:
|
||||||
|
if textRequest.Messages == nil || len(textRequest.Messages) == 0 {
|
||||||
|
return errorWrapper(errors.New("field messages is required"), "required_field_missing", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
case RelayModeEmbeddings:
|
||||||
|
case RelayModeModerations:
|
||||||
|
if textRequest.Input == "" {
|
||||||
|
return errorWrapper(errors.New("field input is required"), "required_field_missing", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
case RelayModeEdits:
|
||||||
|
if textRequest.Instruction == "" {
|
||||||
|
return errorWrapper(errors.New("field instruction is required"), "required_field_missing", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
}
|
||||||
baseURL := common.ChannelBaseURLs[channelType]
|
baseURL := common.ChannelBaseURLs[channelType]
|
||||||
requestURL := c.Request.URL.String()
|
requestURL := c.Request.URL.String()
|
||||||
if c.GetString("base_url") != "" {
|
if c.GetString("base_url") != "" {
|
||||||
@@ -58,12 +82,13 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var promptTokens int
|
var promptTokens int
|
||||||
|
var completionTokens int
|
||||||
switch relayMode {
|
switch relayMode {
|
||||||
case RelayModeChatCompletions:
|
case RelayModeChatCompletions:
|
||||||
promptTokens = countTokenMessages(textRequest.Messages, textRequest.Model)
|
promptTokens = countTokenMessages(textRequest.Messages, textRequest.Model)
|
||||||
case RelayModeCompletions:
|
case RelayModeCompletions:
|
||||||
promptTokens = countTokenInput(textRequest.Prompt, textRequest.Model)
|
promptTokens = countTokenInput(textRequest.Prompt, textRequest.Model)
|
||||||
case RelayModeModeration:
|
case RelayModeModerations:
|
||||||
promptTokens = countTokenInput(textRequest.Input, textRequest.Model)
|
promptTokens = countTokenInput(textRequest.Input, textRequest.Model)
|
||||||
}
|
}
|
||||||
preConsumedTokens := common.PreConsumedQuota
|
preConsumedTokens := common.PreConsumedQuota
|
||||||
@@ -123,30 +148,43 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
defer func() {
|
defer func() {
|
||||||
if consumeQuota {
|
if consumeQuota {
|
||||||
quota := 0
|
quota := 0
|
||||||
completionRatio := 1.34 // default for gpt-3
|
completionRatio := 1.0
|
||||||
|
if strings.HasPrefix(textRequest.Model, "gpt-3.5") {
|
||||||
|
completionRatio = 1.333333
|
||||||
|
}
|
||||||
if strings.HasPrefix(textRequest.Model, "gpt-4") {
|
if strings.HasPrefix(textRequest.Model, "gpt-4") {
|
||||||
completionRatio = 2
|
completionRatio = 2
|
||||||
}
|
}
|
||||||
if isStream {
|
if isStream {
|
||||||
responseTokens := countTokenText(streamResponseText, textRequest.Model)
|
completionTokens = countTokenText(streamResponseText, textRequest.Model)
|
||||||
quota = promptTokens + int(float64(responseTokens)*completionRatio)
|
|
||||||
} else {
|
} else {
|
||||||
quota = textResponse.Usage.PromptTokens + int(float64(textResponse.Usage.CompletionTokens)*completionRatio)
|
promptTokens = textResponse.Usage.PromptTokens
|
||||||
|
completionTokens = textResponse.Usage.CompletionTokens
|
||||||
}
|
}
|
||||||
|
quota = promptTokens + int(float64(completionTokens)*completionRatio)
|
||||||
quota = int(float64(quota) * ratio)
|
quota = int(float64(quota) * ratio)
|
||||||
if ratio != 0 && quota <= 0 {
|
if ratio != 0 && quota <= 0 {
|
||||||
quota = 1
|
quota = 1
|
||||||
}
|
}
|
||||||
|
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
|
quotaDelta := quota - preConsumedQuota
|
||||||
err := model.PostConsumeTokenQuota(tokenId, quotaDelta)
|
err := model.PostConsumeTokenQuota(tokenId, quotaDelta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.SysError("error consuming token remain quota: " + err.Error())
|
common.SysError("error consuming token remain quota: " + err.Error())
|
||||||
}
|
}
|
||||||
tokenName := c.GetString("token_name")
|
if quota != 0 {
|
||||||
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("通过令牌「%s」使用模型 %s 消耗 %s(模型倍率 %.2f,分组倍率 %.2f)", tokenName, textRequest.Model, common.LogQuota(quota), modelRatio, groupRatio))
|
tokenName := c.GetString("token_name")
|
||||||
model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
|
logContent := fmt.Sprintf("模型倍率 %.2f,分组倍率 %.2f,补全倍率 %.2f", modelRatio, groupRatio, completionRatio)
|
||||||
channelId := c.GetInt("channel_id")
|
model.RecordConsumeLog(userId, promptTokens, completionTokens, textRequest.Model, tokenName, quota, logContent)
|
||||||
model.UpdateChannelUsedQuota(channelId, quota)
|
model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
|
||||||
|
channelId := c.GetInt("channel_id")
|
||||||
|
model.UpdateChannelUsedQuota(channelId, quota)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,9 @@ const (
|
|||||||
RelayModeChatCompletions
|
RelayModeChatCompletions
|
||||||
RelayModeCompletions
|
RelayModeCompletions
|
||||||
RelayModeEmbeddings
|
RelayModeEmbeddings
|
||||||
RelayModeModeration
|
RelayModeModerations
|
||||||
RelayModeImagesGenerations
|
RelayModeImagesGenerations
|
||||||
|
RelayModeEdits
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://platform.openai.com/docs/api-reference/chat
|
// https://platform.openai.com/docs/api-reference/chat
|
||||||
@@ -35,6 +36,7 @@ type GeneralOpenAIRequest struct {
|
|||||||
TopP float64 `json:"top_p"`
|
TopP float64 `json:"top_p"`
|
||||||
N int `json:"n"`
|
N int `json:"n"`
|
||||||
Input any `json:"input"`
|
Input any `json:"input"`
|
||||||
|
Instruction string `json:"instruction"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatRequest struct {
|
type ChatRequest struct {
|
||||||
@@ -99,9 +101,11 @@ func Relay(c *gin.Context) {
|
|||||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/embeddings") {
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/embeddings") {
|
||||||
relayMode = RelayModeEmbeddings
|
relayMode = RelayModeEmbeddings
|
||||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/moderations") {
|
||||||
relayMode = RelayModeModeration
|
relayMode = RelayModeModerations
|
||||||
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/images/generations") {
|
||||||
relayMode = RelayModeImagesGenerations
|
relayMode = RelayModeImagesGenerations
|
||||||
|
} else if strings.HasPrefix(c.Request.URL.Path, "/v1/edits") {
|
||||||
|
relayMode = RelayModeEdits
|
||||||
}
|
}
|
||||||
var err *OpenAIErrorWithStatusCode
|
var err *OpenAIErrorWithStatusCode
|
||||||
switch relayMode {
|
switch relayMode {
|
||||||
|
|||||||
44
i18n/en.json
44
i18n/en.json
@@ -366,12 +366,12 @@
|
|||||||
"添加新的用户": "Add New User",
|
"添加新的用户": "Add New User",
|
||||||
"自定义": "Custom",
|
"自定义": "Custom",
|
||||||
"等价金额": "Equivalent Amount",
|
"等价金额": "Equivalent Amount",
|
||||||
"错误": "Error",
|
"未登录或登录已过期,请重新登录": "Not logged in or login has expired, please log in again",
|
||||||
"错误:未登录或登录已过期,请重新登录": "Error: Not logged in or login has expired, please log in again",
|
"请求次数过多,请稍后再试": "Too many requests, please try again later",
|
||||||
"错误:请求次数过多,请稍后再试": "Error: Too many requests, please try again later",
|
"服务器内部错误,请联系管理员": "Server internal error, please contact the administrator",
|
||||||
"错误:服务器内部错误,请联系管理员": "Error: Server internal error, please contact the administrator",
|
|
||||||
"本站仅作演示之用,无服务端": "This site is for demonstration purposes only, no server-side",
|
"本站仅作演示之用,无服务端": "This site is for demonstration purposes only, no server-side",
|
||||||
"错误:": "Error:",
|
"超级管理员未设置充值链接!": "Super administrator has not set the recharge link!",
|
||||||
|
"错误:": "Error: ",
|
||||||
"新版本可用:${data.version},请使用快捷键 Shift + F5 刷新页面": "New version available: ${data.version}, please refresh the page using shortcut Shift + F5",
|
"新版本可用:${data.version},请使用快捷键 Shift + F5 刷新页面": "New version available: ${data.version}, please refresh the page using shortcut Shift + F5",
|
||||||
"无法正常连接至服务器": "Unable to connect to the server normally",
|
"无法正常连接至服务器": "Unable to connect to the server normally",
|
||||||
"管理渠道": "Manage Channels",
|
"管理渠道": "Manage Channels",
|
||||||
@@ -426,5 +426,35 @@
|
|||||||
"更新令牌信息": "Update Token Information",
|
"更新令牌信息": "Update Token Information",
|
||||||
"请输入充值码!": "Please enter the recharge code!",
|
"请输入充值码!": "Please enter the recharge code!",
|
||||||
"请输入名称": "Please enter a name",
|
"请输入名称": "Please enter a name",
|
||||||
"请输入密钥,一行一个": "Please enter the key, one per line"
|
"请输入密钥,一行一个": "Please enter the key, one per line",
|
||||||
}
|
"请输入额度": "Please enter the quota",
|
||||||
|
"令牌创建成功": "Token created successfully",
|
||||||
|
"令牌更新成功": "Token updated successfully",
|
||||||
|
"充值成功!": "Recharge successful!",
|
||||||
|
"更新用户信息": "Update User Information",
|
||||||
|
"请输入新的用户名": "Please enter a new username",
|
||||||
|
"密码": "Password",
|
||||||
|
"请输入新的密码": "Please enter a new password",
|
||||||
|
"显示名称": "Display Name",
|
||||||
|
"请输入新的显示名称": "Please enter a new display name",
|
||||||
|
"已绑定的 GitHub 账户": "GitHub Account Bound",
|
||||||
|
"此项只读,需要用户通过个人设置页面的相关绑定按钮进行绑定,不可直接修改": "This item is read-only. Users need to bind through the relevant binding button on the personal settings page, and cannot be modified directly",
|
||||||
|
"已绑定的微信账户": "WeChat Account Bound",
|
||||||
|
"已绑定的邮箱账户": "Email Account Bound",
|
||||||
|
"用户信息更新成功!": "User information updated successfully!",
|
||||||
|
"模型倍率 %.2f,分组倍率 %.2f": "model rate %.2f, group rate %.2f",
|
||||||
|
"使用明细(总消耗额度:{renderQuota(stat.quota)})": "Usage Details (Total Consumption Quota: {renderQuota(stat.quota)})",
|
||||||
|
"用户名称": "User Name",
|
||||||
|
"令牌名称": "Token Name",
|
||||||
|
"留空则查询全部用户": "Leave blank to query all users",
|
||||||
|
"留空则查询全部令牌": "Leave blank to query all tokens",
|
||||||
|
"模型名称": "Model Name",
|
||||||
|
"留空则查询全部模型": "Leave blank to query all models",
|
||||||
|
"起始时间": "Start Time",
|
||||||
|
"结束时间": "End Time",
|
||||||
|
"查询": "Query",
|
||||||
|
"提示": "Prompt",
|
||||||
|
"补全": "Completion",
|
||||||
|
"消耗额度": "Used Quota",
|
||||||
|
"可选值": "Optional Values"
|
||||||
|
}
|
||||||
|
|||||||
112
model/log.go
112
model/log.go
@@ -6,11 +6,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Log struct {
|
type Log struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"user_id" gorm:"index"`
|
UserId int `json:"user_id"`
|
||||||
CreatedAt int64 `json:"created_at" gorm:"bigint"`
|
CreatedAt int64 `json:"created_at" gorm:"bigint;index"`
|
||||||
Type int `json:"type" gorm:"index"`
|
Type int `json:"type" gorm:"index"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
|
Username string `json:"username" gorm:"index;default:''"`
|
||||||
|
TokenName string `json:"token_name" gorm:"index;default:''"`
|
||||||
|
ModelName string `json:"model_name" gorm:"index;default:''"`
|
||||||
|
Quota int `json:"quota" gorm:"default:0"`
|
||||||
|
PromptTokens int `json:"prompt_tokens" gorm:"default:0"`
|
||||||
|
CompletionTokens int `json:"completion_tokens" gorm:"default:0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -27,6 +33,7 @@ func RecordLog(userId int, logType int, content string) {
|
|||||||
}
|
}
|
||||||
log := &Log{
|
log := &Log{
|
||||||
UserId: userId,
|
UserId: userId,
|
||||||
|
Username: GetUsernameById(userId),
|
||||||
CreatedAt: common.GetTimestamp(),
|
CreatedAt: common.GetTimestamp(),
|
||||||
Type: logType,
|
Type: logType,
|
||||||
Content: content,
|
Content: content,
|
||||||
@@ -37,24 +44,73 @@ func RecordLog(userId int, logType int, content string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllLogs(logType int, startIdx int, num int) (logs []*Log, err error) {
|
func RecordConsumeLog(userId int, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string) {
|
||||||
|
if !common.LogConsumeEnabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log := &Log{
|
||||||
|
UserId: userId,
|
||||||
|
Username: GetUsernameById(userId),
|
||||||
|
CreatedAt: common.GetTimestamp(),
|
||||||
|
Type: LogTypeConsume,
|
||||||
|
Content: content,
|
||||||
|
PromptTokens: promptTokens,
|
||||||
|
CompletionTokens: completionTokens,
|
||||||
|
TokenName: tokenName,
|
||||||
|
ModelName: modelName,
|
||||||
|
Quota: quota,
|
||||||
|
}
|
||||||
|
err := DB.Create(log).Error
|
||||||
|
if err != nil {
|
||||||
|
common.SysError("failed to record log: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, startIdx int, num int) (logs []*Log, err error) {
|
||||||
var tx *gorm.DB
|
var tx *gorm.DB
|
||||||
if logType == LogTypeUnknown {
|
if logType == LogTypeUnknown {
|
||||||
tx = DB
|
tx = DB
|
||||||
} else {
|
} else {
|
||||||
tx = DB.Where("type = ?", logType)
|
tx = DB.Where("type = ?", logType)
|
||||||
}
|
}
|
||||||
|
if modelName != "" {
|
||||||
|
tx = tx.Where("model_name = ?", modelName)
|
||||||
|
}
|
||||||
|
if username != "" {
|
||||||
|
tx = tx.Where("username = ?", username)
|
||||||
|
}
|
||||||
|
if tokenName != "" {
|
||||||
|
tx = tx.Where("token_name = ?", tokenName)
|
||||||
|
}
|
||||||
|
if startTimestamp != 0 {
|
||||||
|
tx = tx.Where("created_at >= ?", startTimestamp)
|
||||||
|
}
|
||||||
|
if endTimestamp != 0 {
|
||||||
|
tx = tx.Where("created_at <= ?", endTimestamp)
|
||||||
|
}
|
||||||
err = tx.Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error
|
err = tx.Order("id desc").Limit(num).Offset(startIdx).Find(&logs).Error
|
||||||
return logs, err
|
return logs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserLogs(userId int, logType int, startIdx int, num int) (logs []*Log, err error) {
|
func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int64, modelName string, tokenName string, startIdx int, num int) (logs []*Log, err error) {
|
||||||
var tx *gorm.DB
|
var tx *gorm.DB
|
||||||
if logType == LogTypeUnknown {
|
if logType == LogTypeUnknown {
|
||||||
tx = DB.Where("user_id = ?", userId)
|
tx = DB.Where("user_id = ?", userId)
|
||||||
} else {
|
} else {
|
||||||
tx = DB.Where("user_id = ? and type = ?", userId, logType)
|
tx = DB.Where("user_id = ? and type = ?", userId, logType)
|
||||||
}
|
}
|
||||||
|
if modelName != "" {
|
||||||
|
tx = tx.Where("model_name = ?", modelName)
|
||||||
|
}
|
||||||
|
if tokenName != "" {
|
||||||
|
tx = tx.Where("token_name = ?", tokenName)
|
||||||
|
}
|
||||||
|
if startTimestamp != 0 {
|
||||||
|
tx = tx.Where("created_at >= ?", startTimestamp)
|
||||||
|
}
|
||||||
|
if endTimestamp != 0 {
|
||||||
|
tx = tx.Where("created_at <= ?", endTimestamp)
|
||||||
|
}
|
||||||
err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error
|
err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error
|
||||||
return logs, err
|
return logs, err
|
||||||
}
|
}
|
||||||
@@ -68,3 +124,45 @@ func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) {
|
|||||||
err = DB.Where("user_id = ? and type = ?", userId, keyword).Order("id desc").Limit(common.MaxRecentItems).Omit("id").Find(&logs).Error
|
err = DB.Where("user_id = ? and type = ?", userId, keyword).Order("id desc").Limit(common.MaxRecentItems).Omit("id").Find(&logs).Error
|
||||||
return logs, err
|
return logs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (quota int) {
|
||||||
|
tx := DB.Table("logs").Select("sum(quota)")
|
||||||
|
if username != "" {
|
||||||
|
tx = tx.Where("username = ?", username)
|
||||||
|
}
|
||||||
|
if tokenName != "" {
|
||||||
|
tx = tx.Where("token_name = ?", tokenName)
|
||||||
|
}
|
||||||
|
if startTimestamp != 0 {
|
||||||
|
tx = tx.Where("created_at >= ?", startTimestamp)
|
||||||
|
}
|
||||||
|
if endTimestamp != 0 {
|
||||||
|
tx = tx.Where("created_at <= ?", endTimestamp)
|
||||||
|
}
|
||||||
|
if modelName != "" {
|
||||||
|
tx = tx.Where("model_name = ?", modelName)
|
||||||
|
}
|
||||||
|
tx.Where("type = ?", LogTypeConsume).Scan("a)
|
||||||
|
return quota
|
||||||
|
}
|
||||||
|
|
||||||
|
func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (token int) {
|
||||||
|
tx := DB.Table("logs").Select("sum(prompt_tokens) + sum(completion_tokens)")
|
||||||
|
if username != "" {
|
||||||
|
tx = tx.Where("username = ?", username)
|
||||||
|
}
|
||||||
|
if tokenName != "" {
|
||||||
|
tx = tx.Where("token_name = ?", tokenName)
|
||||||
|
}
|
||||||
|
if startTimestamp != 0 {
|
||||||
|
tx = tx.Where("created_at >= ?", startTimestamp)
|
||||||
|
}
|
||||||
|
if endTimestamp != 0 {
|
||||||
|
tx = tx.Where("created_at <= ?", endTimestamp)
|
||||||
|
}
|
||||||
|
if modelName != "" {
|
||||||
|
tx = tx.Where("model_name = ?", modelName)
|
||||||
|
}
|
||||||
|
tx.Where("type = ?", LogTypeConsume).Scan(&token)
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|||||||
@@ -303,3 +303,8 @@ func UpdateUserUsedQuotaAndRequestCount(id int, quota int) {
|
|||||||
common.SysError("failed to update user used quota and request count: " + err.Error())
|
common.SysError("failed to update user used quota and request count: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUsernameById(id int) (username string) {
|
||||||
|
DB.Model(&User{}).Where("id = ?", id).Select("username").Find(&username)
|
||||||
|
return username
|
||||||
|
}
|
||||||
|
|||||||
@@ -96,6 +96,8 @@ func SetApiRouter(router *gin.Engine) {
|
|||||||
}
|
}
|
||||||
logRoute := apiRouter.Group("/log")
|
logRoute := apiRouter.Group("/log")
|
||||||
logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
|
logRoute.GET("/", middleware.AdminAuth(), controller.GetAllLogs)
|
||||||
|
logRoute.GET("/stat", middleware.AdminAuth(), controller.GetLogsStat)
|
||||||
|
logRoute.GET("/self/stat", middleware.UserAuth(), controller.GetLogsSelfStat)
|
||||||
logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
|
logRoute.GET("/search", middleware.AdminAuth(), controller.SearchAllLogs)
|
||||||
logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
|
logRoute.GET("/self", middleware.UserAuth(), controller.GetUserLogs)
|
||||||
logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
|
logRoute.GET("/self/search", middleware.UserAuth(), controller.SearchUserLogs)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func SetRelayRouter(router *gin.Engine) {
|
|||||||
{
|
{
|
||||||
relayV1Router.POST("/completions", controller.Relay)
|
relayV1Router.POST("/completions", controller.Relay)
|
||||||
relayV1Router.POST("/chat/completions", controller.Relay)
|
relayV1Router.POST("/chat/completions", controller.Relay)
|
||||||
relayV1Router.POST("/edits", controller.RelayNotImplemented)
|
relayV1Router.POST("/edits", controller.Relay)
|
||||||
relayV1Router.POST("/images/generations", controller.RelayNotImplemented)
|
relayV1Router.POST("/images/generations", controller.RelayNotImplemented)
|
||||||
relayV1Router.POST("/images/edits", controller.RelayNotImplemented)
|
relayV1Router.POST("/images/edits", controller.RelayNotImplemented)
|
||||||
relayV1Router.POST("/images/variations", controller.RelayNotImplemented)
|
relayV1Router.POST("/images/variations", controller.RelayNotImplemented)
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { Button, Label, Pagination, Select, Table } from 'semantic-ui-react';
|
import { Button, Form, Header, Label, Pagination, Segment, Select, Table } from 'semantic-ui-react';
|
||||||
import { API, isAdmin, showError, timestamp2string } from '../helpers';
|
import { API, isAdmin, showError, timestamp2string } from '../helpers';
|
||||||
|
|
||||||
import { ITEMS_PER_PAGE } from '../constants';
|
import { ITEMS_PER_PAGE } from '../constants';
|
||||||
|
import { renderQuota } from '../helpers/render';
|
||||||
|
|
||||||
function renderTimestamp(timestamp) {
|
function renderTimestamp(timestamp) {
|
||||||
return (
|
return (
|
||||||
@@ -14,7 +15,7 @@ function renderTimestamp(timestamp) {
|
|||||||
|
|
||||||
const MODE_OPTIONS = [
|
const MODE_OPTIONS = [
|
||||||
{ key: 'all', text: '全部用户', value: 'all' },
|
{ key: 'all', text: '全部用户', value: 'all' },
|
||||||
{ key: 'self', text: '当前用户', value: 'self' },
|
{ key: 'self', text: '当前用户', value: 'self' }
|
||||||
];
|
];
|
||||||
|
|
||||||
const LOG_OPTIONS = [
|
const LOG_OPTIONS = [
|
||||||
@@ -47,13 +48,58 @@ const LogsTable = () => {
|
|||||||
const [searchKeyword, setSearchKeyword] = useState('');
|
const [searchKeyword, setSearchKeyword] = useState('');
|
||||||
const [searching, setSearching] = useState(false);
|
const [searching, setSearching] = useState(false);
|
||||||
const [logType, setLogType] = useState(0);
|
const [logType, setLogType] = useState(0);
|
||||||
const [mode, setMode] = useState('self'); // all, self
|
const isAdminUser = isAdmin();
|
||||||
const showModePanel = isAdmin();
|
let now = new Date();
|
||||||
|
const [inputs, setInputs] = useState({
|
||||||
|
username: '',
|
||||||
|
token_name: '',
|
||||||
|
model_name: '',
|
||||||
|
start_timestamp: timestamp2string(0),
|
||||||
|
end_timestamp: timestamp2string(now.getTime() / 1000 + 3600)
|
||||||
|
});
|
||||||
|
const { username, token_name, model_name, start_timestamp, end_timestamp } = inputs;
|
||||||
|
|
||||||
|
const [stat, setStat] = useState({
|
||||||
|
quota: 0,
|
||||||
|
token: 0
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleInputChange = (e, { name, value }) => {
|
||||||
|
setInputs((inputs) => ({ ...inputs, [name]: value }));
|
||||||
|
};
|
||||||
|
|
||||||
|
const getLogSelfStat = async () => {
|
||||||
|
let localStartTimestamp = Date.parse(start_timestamp) / 1000;
|
||||||
|
let localEndTimestamp = Date.parse(end_timestamp) / 1000;
|
||||||
|
let res = await API.get(`/api/log/self/stat?type=${logType}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`);
|
||||||
|
const { success, message, data } = res.data;
|
||||||
|
if (success) {
|
||||||
|
setStat(data);
|
||||||
|
} else {
|
||||||
|
showError(message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getLogStat = async () => {
|
||||||
|
let localStartTimestamp = Date.parse(start_timestamp) / 1000;
|
||||||
|
let localEndTimestamp = Date.parse(end_timestamp) / 1000;
|
||||||
|
let res = await API.get(`/api/log/stat?type=${logType}&username=${username}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`);
|
||||||
|
const { success, message, data } = res.data;
|
||||||
|
if (success) {
|
||||||
|
setStat(data);
|
||||||
|
} else {
|
||||||
|
showError(message);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const loadLogs = async (startIdx) => {
|
const loadLogs = async (startIdx) => {
|
||||||
let url = `/api/log/self/?p=${startIdx}&type=${logType}`;
|
let url = '';
|
||||||
if (mode === 'all') {
|
let localStartTimestamp = Date.parse(start_timestamp) / 1000;
|
||||||
url = `/api/log/?p=${startIdx}&type=${logType}`;
|
let localEndTimestamp = Date.parse(end_timestamp) / 1000;
|
||||||
|
if (isAdminUser) {
|
||||||
|
url = `/api/log/?p=${startIdx}&type=${logType}&username=${username}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`;
|
||||||
|
} else {
|
||||||
|
url = `/api/log/self/?p=${startIdx}&type=${logType}&token_name=${token_name}&model_name=${model_name}&start_timestamp=${localStartTimestamp}&end_timestamp=${localEndTimestamp}`;
|
||||||
}
|
}
|
||||||
const res = await API.get(url);
|
const res = await API.get(url);
|
||||||
const { success, message, data } = res.data;
|
const { success, message, data } = res.data;
|
||||||
@@ -61,7 +107,7 @@ const LogsTable = () => {
|
|||||||
if (startIdx === 0) {
|
if (startIdx === 0) {
|
||||||
setLogs(data);
|
setLogs(data);
|
||||||
} else {
|
} else {
|
||||||
let newLogs = logs;
|
let newLogs = [...logs];
|
||||||
newLogs.push(...data);
|
newLogs.push(...data);
|
||||||
setLogs(newLogs);
|
setLogs(newLogs);
|
||||||
}
|
}
|
||||||
@@ -83,20 +129,18 @@ const LogsTable = () => {
|
|||||||
|
|
||||||
const refresh = async () => {
|
const refresh = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
setActivePage(1)
|
||||||
await loadLogs(0);
|
await loadLogs(0);
|
||||||
|
if (isAdminUser) {
|
||||||
|
getLogStat().then();
|
||||||
|
} else {
|
||||||
|
getLogSelfStat().then();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
loadLogs(0)
|
|
||||||
.then()
|
|
||||||
.catch((reason) => {
|
|
||||||
showError(reason);
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
refresh().then();
|
refresh().then();
|
||||||
}, [mode, logType]);
|
}, [logType]);
|
||||||
|
|
||||||
const searchLogs = async () => {
|
const searchLogs = async () => {
|
||||||
if (searchKeyword === '') {
|
if (searchKeyword === '') {
|
||||||
@@ -125,9 +169,17 @@ const LogsTable = () => {
|
|||||||
if (logs.length === 0) return;
|
if (logs.length === 0) return;
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
let sortedLogs = [...logs];
|
let sortedLogs = [...logs];
|
||||||
sortedLogs.sort((a, b) => {
|
if (typeof sortedLogs[0][key] === 'string'){
|
||||||
return ('' + a[key]).localeCompare(b[key]);
|
sortedLogs.sort((a, b) => {
|
||||||
});
|
return ('' + a[key]).localeCompare(b[key]);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sortedLogs.sort((a, b) => {
|
||||||
|
if (a[key] === b[key]) return 0;
|
||||||
|
if (a[key] > b[key]) return -1;
|
||||||
|
if (a[key] < b[key]) return 1;
|
||||||
|
});
|
||||||
|
}
|
||||||
if (sortedLogs[0].id === logs[0].id) {
|
if (sortedLogs[0].id === logs[0].id) {
|
||||||
sortedLogs.reverse();
|
sortedLogs.reverse();
|
||||||
}
|
}
|
||||||
@@ -137,118 +189,178 @@ const LogsTable = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Table basic>
|
<Segment>
|
||||||
<Table.Header>
|
<Header as='h3'>使用明细(总消耗额度:{renderQuota(stat.quota)})</Header>
|
||||||
<Table.Row>
|
<Form>
|
||||||
<Table.HeaderCell
|
<Form.Group>
|
||||||
style={{ cursor: 'pointer' }}
|
|
||||||
onClick={() => {
|
|
||||||
sortLog('created_time');
|
|
||||||
}}
|
|
||||||
width={3}
|
|
||||||
>
|
|
||||||
时间
|
|
||||||
</Table.HeaderCell>
|
|
||||||
{
|
{
|
||||||
showModePanel && (
|
isAdminUser && (
|
||||||
<Table.HeaderCell
|
<Form.Input fluid label={'用户名称'} width={2} value={username}
|
||||||
|
placeholder={'可选值'} name='username'
|
||||||
|
onChange={handleInputChange} />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
<Form.Input fluid label={'令牌名称'} width={isAdminUser ? 2 : 3} value={token_name}
|
||||||
|
placeholder={'可选值'} name='token_name' onChange={handleInputChange} />
|
||||||
|
<Form.Input fluid label='模型名称' width={isAdminUser ? 2 : 3} value={model_name} placeholder='可选值'
|
||||||
|
name='model_name'
|
||||||
|
onChange={handleInputChange} />
|
||||||
|
<Form.Input fluid label='起始时间' width={4} value={start_timestamp} type='datetime-local'
|
||||||
|
name='start_timestamp'
|
||||||
|
onChange={handleInputChange} />
|
||||||
|
<Form.Input fluid label='结束时间' width={4} value={end_timestamp} type='datetime-local'
|
||||||
|
name='end_timestamp'
|
||||||
|
onChange={handleInputChange} />
|
||||||
|
<Form.Button fluid label='操作' width={2} onClick={refresh}>查询</Form.Button>
|
||||||
|
</Form.Group>
|
||||||
|
</Form>
|
||||||
|
<Table basic compact size='small'>
|
||||||
|
<Table.Header>
|
||||||
|
<Table.Row>
|
||||||
|
<Table.HeaderCell
|
||||||
|
style={{ cursor: 'pointer' }}
|
||||||
|
onClick={() => {
|
||||||
|
sortLog('created_time');
|
||||||
|
}}
|
||||||
|
width={3}
|
||||||
|
>
|
||||||
|
时间
|
||||||
|
</Table.HeaderCell>
|
||||||
|
{
|
||||||
|
isAdminUser && <Table.HeaderCell
|
||||||
style={{ cursor: 'pointer' }}
|
style={{ cursor: 'pointer' }}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
sortLog('user_id');
|
sortLog('username');
|
||||||
}}
|
}}
|
||||||
width={1}
|
width={1}
|
||||||
>
|
>
|
||||||
用户
|
用户
|
||||||
</Table.HeaderCell>
|
</Table.HeaderCell>
|
||||||
)
|
|
||||||
}
|
|
||||||
<Table.HeaderCell
|
|
||||||
style={{ cursor: 'pointer' }}
|
|
||||||
onClick={() => {
|
|
||||||
sortLog('type');
|
|
||||||
}}
|
|
||||||
width={2}
|
|
||||||
>
|
|
||||||
类型
|
|
||||||
</Table.HeaderCell>
|
|
||||||
<Table.HeaderCell
|
|
||||||
style={{ cursor: 'pointer' }}
|
|
||||||
onClick={() => {
|
|
||||||
sortLog('content');
|
|
||||||
}}
|
|
||||||
width={showModePanel ? 10 : 11}
|
|
||||||
>
|
|
||||||
详情
|
|
||||||
</Table.HeaderCell>
|
|
||||||
</Table.Row>
|
|
||||||
</Table.Header>
|
|
||||||
|
|
||||||
<Table.Body>
|
|
||||||
{logs
|
|
||||||
.slice(
|
|
||||||
(activePage - 1) * ITEMS_PER_PAGE,
|
|
||||||
activePage * ITEMS_PER_PAGE
|
|
||||||
)
|
|
||||||
.map((log, idx) => {
|
|
||||||
if (log.deleted) return <></>;
|
|
||||||
return (
|
|
||||||
<Table.Row key={log.created_at}>
|
|
||||||
<Table.Cell>{renderTimestamp(log.created_at)}</Table.Cell>
|
|
||||||
{
|
|
||||||
showModePanel && (
|
|
||||||
<Table.Cell><Label>{log.user_id}</Label></Table.Cell>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
<Table.Cell>{renderType(log.type)}</Table.Cell>
|
|
||||||
<Table.Cell>{log.content}</Table.Cell>
|
|
||||||
</Table.Row>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</Table.Body>
|
|
||||||
|
|
||||||
<Table.Footer>
|
|
||||||
<Table.Row>
|
|
||||||
<Table.HeaderCell colSpan={showModePanel ? '5' : '4'}>
|
|
||||||
{
|
|
||||||
showModePanel && (
|
|
||||||
<Select
|
|
||||||
placeholder='选择模式'
|
|
||||||
options={MODE_OPTIONS}
|
|
||||||
style={{ marginRight: '8px' }}
|
|
||||||
name='mode'
|
|
||||||
value={mode}
|
|
||||||
onChange={(e, { name, value }) => {
|
|
||||||
setMode(value);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
<Select
|
<Table.HeaderCell
|
||||||
placeholder='选择明细分类'
|
style={{ cursor: 'pointer' }}
|
||||||
options={LOG_OPTIONS}
|
onClick={() => {
|
||||||
style={{ marginRight: '8px' }}
|
sortLog('token_name');
|
||||||
name='logType'
|
|
||||||
value={logType}
|
|
||||||
onChange={(e, { name, value }) => {
|
|
||||||
setLogType(value);
|
|
||||||
}}
|
}}
|
||||||
/>
|
width={1}
|
||||||
<Button size='small' onClick={refresh} loading={loading}>刷新</Button>
|
>
|
||||||
<Pagination
|
令牌
|
||||||
floated='right'
|
</Table.HeaderCell>
|
||||||
activePage={activePage}
|
<Table.HeaderCell
|
||||||
onPageChange={onPaginationChange}
|
style={{ cursor: 'pointer' }}
|
||||||
size='small'
|
onClick={() => {
|
||||||
siblingRange={1}
|
sortLog('type');
|
||||||
totalPages={
|
}}
|
||||||
Math.ceil(logs.length / ITEMS_PER_PAGE) +
|
width={1}
|
||||||
(logs.length % ITEMS_PER_PAGE === 0 ? 1 : 0)
|
>
|
||||||
}
|
类型
|
||||||
/>
|
</Table.HeaderCell>
|
||||||
</Table.HeaderCell>
|
<Table.HeaderCell
|
||||||
</Table.Row>
|
style={{ cursor: 'pointer' }}
|
||||||
</Table.Footer>
|
onClick={() => {
|
||||||
</Table>
|
sortLog('model_name');
|
||||||
|
}}
|
||||||
|
width={2}
|
||||||
|
>
|
||||||
|
模型
|
||||||
|
</Table.HeaderCell>
|
||||||
|
<Table.HeaderCell
|
||||||
|
style={{ cursor: 'pointer' }}
|
||||||
|
onClick={() => {
|
||||||
|
sortLog('prompt_tokens');
|
||||||
|
}}
|
||||||
|
width={1}
|
||||||
|
>
|
||||||
|
提示
|
||||||
|
</Table.HeaderCell>
|
||||||
|
<Table.HeaderCell
|
||||||
|
style={{ cursor: 'pointer' }}
|
||||||
|
onClick={() => {
|
||||||
|
sortLog('completion_tokens');
|
||||||
|
}}
|
||||||
|
width={1}
|
||||||
|
>
|
||||||
|
补全
|
||||||
|
</Table.HeaderCell>
|
||||||
|
<Table.HeaderCell
|
||||||
|
style={{ cursor: 'pointer' }}
|
||||||
|
onClick={() => {
|
||||||
|
sortLog('quota');
|
||||||
|
}}
|
||||||
|
width={2}
|
||||||
|
>
|
||||||
|
消耗额度
|
||||||
|
</Table.HeaderCell>
|
||||||
|
<Table.HeaderCell
|
||||||
|
style={{ cursor: 'pointer' }}
|
||||||
|
onClick={() => {
|
||||||
|
sortLog('content');
|
||||||
|
}}
|
||||||
|
width={isAdminUser ? 4 : 5}
|
||||||
|
>
|
||||||
|
详情
|
||||||
|
</Table.HeaderCell>
|
||||||
|
</Table.Row>
|
||||||
|
</Table.Header>
|
||||||
|
|
||||||
|
<Table.Body>
|
||||||
|
{logs
|
||||||
|
.slice(
|
||||||
|
(activePage - 1) * ITEMS_PER_PAGE,
|
||||||
|
activePage * ITEMS_PER_PAGE
|
||||||
|
)
|
||||||
|
.map((log, idx) => {
|
||||||
|
if (log.deleted) return <></>;
|
||||||
|
return (
|
||||||
|
<Table.Row key={log.created_at}>
|
||||||
|
<Table.Cell>{renderTimestamp(log.created_at)}</Table.Cell>
|
||||||
|
{
|
||||||
|
isAdminUser && (
|
||||||
|
<Table.Cell>{log.username ? <Label>{log.username}</Label> : ''}</Table.Cell>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
<Table.Cell>{log.token_name ? <Label basic>{log.token_name}</Label> : ''}</Table.Cell>
|
||||||
|
<Table.Cell>{renderType(log.type)}</Table.Cell>
|
||||||
|
<Table.Cell>{log.model_name ? <Label basic>{log.model_name}</Label> : ''}</Table.Cell>
|
||||||
|
<Table.Cell>{log.prompt_tokens ? log.prompt_tokens : ''}</Table.Cell>
|
||||||
|
<Table.Cell>{log.completion_tokens ? log.completion_tokens : ''}</Table.Cell>
|
||||||
|
<Table.Cell>{log.quota ? renderQuota(log.quota, 6) : ''}</Table.Cell>
|
||||||
|
<Table.Cell>{log.content}</Table.Cell>
|
||||||
|
</Table.Row>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</Table.Body>
|
||||||
|
|
||||||
|
<Table.Footer>
|
||||||
|
<Table.Row>
|
||||||
|
<Table.HeaderCell colSpan={'9'}>
|
||||||
|
<Select
|
||||||
|
placeholder='选择明细分类'
|
||||||
|
options={LOG_OPTIONS}
|
||||||
|
style={{ marginRight: '8px' }}
|
||||||
|
name='logType'
|
||||||
|
value={logType}
|
||||||
|
onChange={(e, { name, value }) => {
|
||||||
|
setLogType(value);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<Button size='small' onClick={refresh} loading={loading}>刷新</Button>
|
||||||
|
<Pagination
|
||||||
|
floated='right'
|
||||||
|
activePage={activePage}
|
||||||
|
onPageChange={onPaginationChange}
|
||||||
|
size='small'
|
||||||
|
siblingRange={1}
|
||||||
|
totalPages={
|
||||||
|
Math.ceil(logs.length / ITEMS_PER_PAGE) +
|
||||||
|
(logs.length % ITEMS_PER_PAGE === 0 ? 1 : 0)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</Table.HeaderCell>
|
||||||
|
</Table.Row>
|
||||||
|
</Table.Footer>
|
||||||
|
</Table>
|
||||||
|
</Segment>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,10 +4,7 @@ import LogsTable from '../../components/LogsTable';
|
|||||||
|
|
||||||
const Token = () => (
|
const Token = () => (
|
||||||
<>
|
<>
|
||||||
<Segment>
|
<LogsTable />
|
||||||
<Header as='h3'>额度明细</Header>
|
|
||||||
<LogsTable />
|
|
||||||
</Segment>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import React, { useEffect, useState } from 'react';
|
|||||||
import { Button, Form, Header, Segment } from 'semantic-ui-react';
|
import { Button, Form, Header, Segment } from 'semantic-ui-react';
|
||||||
import { useParams } from 'react-router-dom';
|
import { useParams } from 'react-router-dom';
|
||||||
import { API, showError, showSuccess } from '../../helpers';
|
import { API, showError, showSuccess } from '../../helpers';
|
||||||
|
import { renderQuota, renderQuotaWithPrompt } from '../../helpers/render';
|
||||||
|
|
||||||
const EditUser = () => {
|
const EditUser = () => {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
@@ -134,7 +135,7 @@ const EditUser = () => {
|
|||||||
</Form.Field>
|
</Form.Field>
|
||||||
<Form.Field>
|
<Form.Field>
|
||||||
<Form.Input
|
<Form.Input
|
||||||
label='剩余额度'
|
label={`剩余额度${renderQuotaWithPrompt(quota)}`}
|
||||||
name='quota'
|
name='quota'
|
||||||
placeholder={'请输入新的剩余额度'}
|
placeholder={'请输入新的剩余额度'}
|
||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
|
|||||||
Reference in New Issue
Block a user