mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-08 09:43:42 +08:00
Compare commits
21 Commits
v0.4.3-alp
...
v0.4.4-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0cdab80a6e | ||
|
|
760183a970 | ||
|
|
58fb18aace | ||
|
|
630156dc0a | ||
|
|
5f23f59d1c | ||
|
|
538a5d7a9b | ||
|
|
593e1926e9 | ||
|
|
e87ad1f402 | ||
|
|
07cccdc8c0 | ||
|
|
f71f01662c | ||
|
|
54d7a1c2e8 | ||
|
|
f426f31bd7 | ||
|
|
2930577cd6 | ||
|
|
e09512177a | ||
|
|
d6dbaff3c2 | ||
|
|
7f9577a386 | ||
|
|
38668e7331 | ||
|
|
323f3d263a | ||
|
|
0c34ed4c61 | ||
|
|
7c7eb6b7ec | ||
|
|
8b2ef666ef |
23
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
23
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
name: 报告问题
|
||||||
|
about: 使用简练详细的语言描述你遇到的问题
|
||||||
|
title: ''
|
||||||
|
labels: bug
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**例行检查**
|
||||||
|
+ [ ] 我已确认目前没有类似 issue
|
||||||
|
+ [ ] 我已确认我已升级到最新版本
|
||||||
|
+ [ ] 我理解并愿意跟进此 issue,协助测试和提供反馈
|
||||||
|
+ [ ] 我理解并认可上述内容,并理解项目维护者精力有限,不遵循规则的 issue 可能会被无视或直接关闭
|
||||||
|
|
||||||
|
**问题描述**
|
||||||
|
|
||||||
|
**复现步骤**
|
||||||
|
|
||||||
|
**预期结果**
|
||||||
|
|
||||||
|
**相关截图**
|
||||||
|
如果没有的话,请删除此节。
|
||||||
11
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
11
.github/ISSUE_TEMPLATE/config.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
blank_issues_enabled: false
|
||||||
|
contact_links:
|
||||||
|
- name: 项目群聊
|
||||||
|
url: https://openai.justsong.cn/
|
||||||
|
about: QQ 群:828520184,自动审核,备注 One API
|
||||||
|
- name: 赞赏支持
|
||||||
|
url: https://iamazing.cn/page/reward
|
||||||
|
about: 请作者喝杯咖啡,以激励作者持续开发
|
||||||
|
- name: 付费部署或定制功能
|
||||||
|
url: https://openai.justsong.cn/
|
||||||
|
about: 加群后联系群主
|
||||||
18
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
18
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
name: 功能请求
|
||||||
|
about: 使用简练详细的语言描述希望加入的新功能
|
||||||
|
title: ''
|
||||||
|
labels: enhancement
|
||||||
|
assignees: ''
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**例行检查**
|
||||||
|
+ [ ] 我已确认目前没有类似 issue
|
||||||
|
+ [ ] 我已确认我已升级到最新版本
|
||||||
|
+ [ ] 我理解并愿意跟进此 issue,协助测试和提供反馈
|
||||||
|
+ [ ] 我理解并认可上述内容,并理解项目维护者精力有限,不遵循规则的 issue 可能会被无视或直接关闭
|
||||||
|
|
||||||
|
**功能描述**
|
||||||
|
|
||||||
|
**应用场景**
|
||||||
@@ -117,6 +117,8 @@ sudo certbot --nginx
|
|||||||
sudo service nginx restart
|
sudo service nginx restart
|
||||||
```
|
```
|
||||||
|
|
||||||
|
初始账号用户名为 `root`,密码为 `123456`。
|
||||||
|
|
||||||
### 手动部署
|
### 手动部署
|
||||||
1. 从 [GitHub Releases](https://github.com/songquanpeng/one-api/releases/latest) 下载可执行文件或者从源码编译:
|
1. 从 [GitHub Releases](https://github.com/songquanpeng/one-api/releases/latest) 下载可执行文件或者从源码编译:
|
||||||
```shell
|
```shell
|
||||||
@@ -208,4 +210,6 @@ https://openai.justsong.cn
|
|||||||
## 注意
|
## 注意
|
||||||
本项目为开源项目,请在遵循 OpenAI 的[使用条款](https://openai.com/policies/terms-of-use)以及法律法规的情况下使用,不得用于非法用途。
|
本项目为开源项目,请在遵循 OpenAI 的[使用条款](https://openai.com/policies/terms-of-use)以及法律法规的情况下使用,不得用于非法用途。
|
||||||
|
|
||||||
本项目依据 MIT 协议开源,请以某种方式保留 One API 的版权信息。
|
本项目使用 MIT 协议进行开源,请以某种方式保留 One API 的版权信息。
|
||||||
|
|
||||||
|
依据 MIT 协议,使用者需自行承担使用本项目的风险与责任,本开源项目开发者与此无关。
|
||||||
@@ -35,6 +35,8 @@ var WeChatAuthEnabled = false
|
|||||||
var TurnstileCheckEnabled = false
|
var TurnstileCheckEnabled = false
|
||||||
var RegisterEnabled = true
|
var RegisterEnabled = true
|
||||||
|
|
||||||
|
var LogConsumeEnabled = true
|
||||||
|
|
||||||
var SMTPServer = ""
|
var SMTPServer = ""
|
||||||
var SMTPPort = 587
|
var SMTPPort = 587
|
||||||
var SMTPAccount = ""
|
var SMTPAccount = ""
|
||||||
|
|||||||
@@ -2,16 +2,23 @@ package common
|
|||||||
|
|
||||||
import "encoding/json"
|
import "encoding/json"
|
||||||
|
|
||||||
|
// ModelRatio
|
||||||
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
// https://platform.openai.com/docs/models/model-endpoint-compatibility
|
||||||
// https://openai.com/pricing
|
// https://openai.com/pricing
|
||||||
// TODO: when a new api is enabled, check the pricing here
|
// TODO: when a new api is enabled, check the pricing here
|
||||||
|
// 1 === $0.002 / 1K tokens
|
||||||
var ModelRatio = map[string]float64{
|
var ModelRatio = map[string]float64{
|
||||||
"gpt-4": 15,
|
"gpt-4": 15,
|
||||||
"gpt-4-0314": 15,
|
"gpt-4-0314": 15,
|
||||||
|
"gpt-4-0613": 15,
|
||||||
"gpt-4-32k": 30,
|
"gpt-4-32k": 30,
|
||||||
"gpt-4-32k-0314": 30,
|
"gpt-4-32k-0314": 30,
|
||||||
"gpt-3.5-turbo": 1, // $0.002 / 1K tokens
|
"gpt-4-32k-0613": 30,
|
||||||
"gpt-3.5-turbo-0301": 1,
|
"gpt-3.5-turbo": 0.75, // $0.0015 / 1K tokens
|
||||||
|
"gpt-3.5-turbo-0301": 0.75,
|
||||||
|
"gpt-3.5-turbo-0613": 0.75,
|
||||||
|
"gpt-3.5-turbo-16k": 1.5, // $0.003 / 1K tokens
|
||||||
|
"gpt-3.5-turbo-16k-0613": 1.5,
|
||||||
"text-ada-001": 0.2,
|
"text-ada-001": 0.2,
|
||||||
"text-babbage-001": 0.25,
|
"text-babbage-001": 0.25,
|
||||||
"text-curie-001": 1,
|
"text-curie-001": 1,
|
||||||
|
|||||||
@@ -71,6 +71,33 @@ func init() {
|
|||||||
Root: "gpt-3.5-turbo-0301",
|
Root: "gpt-3.5-turbo-0301",
|
||||||
Parent: nil,
|
Parent: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-3.5-turbo-0613",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-3.5-turbo-0613",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-3.5-turbo-16k",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-3.5-turbo-16k",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-3.5-turbo-16k-0613",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-3.5-turbo-16k-0613",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Id: "gpt-4",
|
Id: "gpt-4",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
@@ -89,6 +116,15 @@ func init() {
|
|||||||
Root: "gpt-4-0314",
|
Root: "gpt-4-0314",
|
||||||
Parent: nil,
|
Parent: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-4-0613",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-4-0613",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Id: "gpt-4-32k",
|
Id: "gpt-4-32k",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
@@ -107,6 +143,15 @@ func init() {
|
|||||||
Root: "gpt-4-32k-0314",
|
Root: "gpt-4-32k-0314",
|
||||||
Parent: nil,
|
Parent: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Id: "gpt-4-32k-0613",
|
||||||
|
Object: "model",
|
||||||
|
Created: 1677649963,
|
||||||
|
OwnedBy: "openai",
|
||||||
|
Permission: permission,
|
||||||
|
Root: "gpt-4-32k-0613",
|
||||||
|
Parent: nil,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Id: "text-embedding-ada-002",
|
Id: "text-embedding-ada-002",
|
||||||
Object: "model",
|
Object: "model",
|
||||||
|
|||||||
@@ -58,6 +58,20 @@ func countTokenMessages(messages []Message, model string) int {
|
|||||||
return tokenNum
|
return tokenNum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func countTokenInput(input any, model string) int {
|
||||||
|
switch input.(type) {
|
||||||
|
case string:
|
||||||
|
return countTokenText(input.(string), model)
|
||||||
|
case []string:
|
||||||
|
text := ""
|
||||||
|
for _, s := range input.([]string) {
|
||||||
|
text += s
|
||||||
|
}
|
||||||
|
return countTokenText(text, model)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func countTokenText(text string, model string) int {
|
func countTokenText(text string, model string) int {
|
||||||
tokenEncoder := getTokenEncoder(model)
|
tokenEncoder := getTokenEncoder(model)
|
||||||
token := tokenEncoder.Encode(text, nil, nil)
|
token := tokenEncoder.Encode(text, nil, nil)
|
||||||
|
|||||||
@@ -32,13 +32,13 @@ const (
|
|||||||
type GeneralOpenAIRequest struct {
|
type GeneralOpenAIRequest struct {
|
||||||
Model string `json:"model"`
|
Model string `json:"model"`
|
||||||
Messages []Message `json:"messages"`
|
Messages []Message `json:"messages"`
|
||||||
Prompt string `json:"prompt"`
|
Prompt any `json:"prompt"`
|
||||||
Stream bool `json:"stream"`
|
Stream bool `json:"stream"`
|
||||||
MaxTokens int `json:"max_tokens"`
|
MaxTokens int `json:"max_tokens"`
|
||||||
Temperature float64 `json:"temperature"`
|
Temperature float64 `json:"temperature"`
|
||||||
TopP float64 `json:"top_p"`
|
TopP float64 `json:"top_p"`
|
||||||
N int `json:"n"`
|
N int `json:"n"`
|
||||||
Input string `json:"input"`
|
Input any `json:"input"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatRequest struct {
|
type ChatRequest struct {
|
||||||
@@ -177,6 +177,7 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
// https://github.com/songquanpeng/one-api/issues/67
|
// https://github.com/songquanpeng/one-api/issues/67
|
||||||
model_ = strings.TrimSuffix(model_, "-0301")
|
model_ = strings.TrimSuffix(model_, "-0301")
|
||||||
model_ = strings.TrimSuffix(model_, "-0314")
|
model_ = strings.TrimSuffix(model_, "-0314")
|
||||||
|
model_ = strings.TrimSuffix(model_, "-0613")
|
||||||
fullRequestURL = fmt.Sprintf("%s/openai/deployments/%s/%s", baseURL, model_, task)
|
fullRequestURL = fmt.Sprintf("%s/openai/deployments/%s/%s", baseURL, model_, task)
|
||||||
} else if channelType == common.ChannelTypePaLM {
|
} else if channelType == common.ChannelTypePaLM {
|
||||||
err := relayPaLM(textRequest, c)
|
err := relayPaLM(textRequest, c)
|
||||||
@@ -187,9 +188,9 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
case RelayModeChatCompletions:
|
case RelayModeChatCompletions:
|
||||||
promptTokens = countTokenMessages(textRequest.Messages, textRequest.Model)
|
promptTokens = countTokenMessages(textRequest.Messages, textRequest.Model)
|
||||||
case RelayModeCompletions:
|
case RelayModeCompletions:
|
||||||
promptTokens = countTokenText(textRequest.Prompt, textRequest.Model)
|
promptTokens = countTokenInput(textRequest.Prompt, textRequest.Model)
|
||||||
case RelayModeModeration:
|
case RelayModeModeration:
|
||||||
promptTokens = countTokenText(textRequest.Input, textRequest.Model)
|
promptTokens = countTokenInput(textRequest.Input, textRequest.Model)
|
||||||
}
|
}
|
||||||
preConsumedTokens := common.PreConsumedQuota
|
preConsumedTokens := common.PreConsumedQuota
|
||||||
if textRequest.MaxTokens != 0 {
|
if textRequest.MaxTokens != 0 {
|
||||||
@@ -239,16 +240,15 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
defer func() {
|
defer func() {
|
||||||
if consumeQuota {
|
if consumeQuota {
|
||||||
quota := 0
|
quota := 0
|
||||||
usingGPT4 := strings.HasPrefix(textRequest.Model, "gpt-4")
|
completionRatio := 1.34 // default for gpt-3
|
||||||
completionRatio := 1
|
if strings.HasPrefix(textRequest.Model, "gpt-4") {
|
||||||
if usingGPT4 {
|
|
||||||
completionRatio = 2
|
completionRatio = 2
|
||||||
}
|
}
|
||||||
if isStream {
|
if isStream {
|
||||||
responseTokens := countTokenText(streamResponseText, textRequest.Model)
|
responseTokens := countTokenText(streamResponseText, textRequest.Model)
|
||||||
quota = promptTokens + responseTokens*completionRatio
|
quota = promptTokens + int(float64(responseTokens)*completionRatio)
|
||||||
} else {
|
} else {
|
||||||
quota = textResponse.Usage.PromptTokens + textResponse.Usage.CompletionTokens*completionRatio
|
quota = textResponse.Usage.PromptTokens + int(float64(textResponse.Usage.CompletionTokens)*completionRatio)
|
||||||
}
|
}
|
||||||
quota = int(float64(quota) * ratio)
|
quota = int(float64(quota) * ratio)
|
||||||
if ratio != 0 && quota <= 0 {
|
if ratio != 0 && quota <= 0 {
|
||||||
@@ -261,6 +261,9 @@ func relayHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
|
|||||||
}
|
}
|
||||||
userId := c.GetInt("id")
|
userId := c.GetInt("id")
|
||||||
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("使用模型 %s 消耗 %d 点额度(模型倍率 %.2f,分组倍率 %.2f)", textRequest.Model, quota, modelRatio, groupRatio))
|
model.RecordLog(userId, model.LogTypeConsume, fmt.Sprintf("使用模型 %s 消耗 %d 点额度(模型倍率 %.2f,分组倍率 %.2f)", textRequest.Model, quota, modelRatio, groupRatio))
|
||||||
|
model.UpdateUserUsedQuotaAndRequestCount(userId, quota)
|
||||||
|
channelId := c.GetInt("channel_id")
|
||||||
|
model.UpdateChannelUsedQuota(channelId, quota)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
@@ -30,15 +30,18 @@ func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
|
|||||||
|
|
||||||
func (channel *Channel) AddAbilities() error {
|
func (channel *Channel) AddAbilities() error {
|
||||||
models_ := strings.Split(channel.Models, ",")
|
models_ := strings.Split(channel.Models, ",")
|
||||||
|
groups_ := strings.Split(channel.Group, ",")
|
||||||
abilities := make([]Ability, 0, len(models_))
|
abilities := make([]Ability, 0, len(models_))
|
||||||
for _, model := range models_ {
|
for _, model := range models_ {
|
||||||
ability := Ability{
|
for _, group := range groups_ {
|
||||||
Group: channel.Group,
|
ability := Ability{
|
||||||
Model: model,
|
Group: group,
|
||||||
ChannelId: channel.Id,
|
Model: model,
|
||||||
Enabled: channel.Status == common.ChannelStatusEnabled,
|
ChannelId: channel.Id,
|
||||||
|
Enabled: channel.Status == common.ChannelStatusEnabled,
|
||||||
|
}
|
||||||
|
abilities = append(abilities, ability)
|
||||||
}
|
}
|
||||||
abilities = append(abilities, ability)
|
|
||||||
}
|
}
|
||||||
return DB.Create(&abilities).Error
|
return DB.Create(&abilities).Error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ type Channel struct {
|
|||||||
BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"`
|
BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"`
|
||||||
Models string `json:"models"`
|
Models string `json:"models"`
|
||||||
Group string `json:"group" gorm:"type:varchar(32);default:'default'"`
|
Group string `json:"group" gorm:"type:varchar(32);default:'default'"`
|
||||||
|
UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) {
|
func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) {
|
||||||
@@ -136,3 +138,10 @@ func UpdateChannelStatusById(id int, status int) {
|
|||||||
common.SysError("failed to update channel status: " + err.Error())
|
common.SysError("failed to update channel status: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpdateChannelUsedQuota(id int, quota int) {
|
||||||
|
err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error
|
||||||
|
if err != nil {
|
||||||
|
common.SysError("failed to update channel used quota: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,6 +22,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RecordLog(userId int, logType int, content string) {
|
func RecordLog(userId int, logType int, content string) {
|
||||||
|
if logType == LogTypeConsume && !common.LogConsumeEnabled {
|
||||||
|
return
|
||||||
|
}
|
||||||
log := &Log{
|
log := &Log{
|
||||||
UserId: userId,
|
UserId: userId,
|
||||||
CreatedAt: common.GetTimestamp(),
|
CreatedAt: common.GetTimestamp(),
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ func InitOptionMap() {
|
|||||||
common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
|
common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
|
||||||
common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
|
common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
|
||||||
common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled)
|
common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled)
|
||||||
|
common.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(common.LogConsumeEnabled)
|
||||||
common.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(common.ChannelDisableThreshold, 'f', -1, 64)
|
common.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(common.ChannelDisableThreshold, 'f', -1, 64)
|
||||||
common.OptionMap["SMTPServer"] = ""
|
common.OptionMap["SMTPServer"] = ""
|
||||||
common.OptionMap["SMTPFrom"] = ""
|
common.OptionMap["SMTPFrom"] = ""
|
||||||
@@ -134,6 +135,8 @@ func updateOptionMap(key string, value string) (err error) {
|
|||||||
common.RegisterEnabled = boolValue
|
common.RegisterEnabled = boolValue
|
||||||
case "AutomaticDisableChannelEnabled":
|
case "AutomaticDisableChannelEnabled":
|
||||||
common.AutomaticDisableChannelEnabled = boolValue
|
common.AutomaticDisableChannelEnabled = boolValue
|
||||||
|
case "LogConsumeEnabled":
|
||||||
|
common.LogConsumeEnabled = boolValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch key {
|
switch key {
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ type User struct {
|
|||||||
VerificationCode string `json:"verification_code" gorm:"-:all"` // this field is only for Email verification, don't save it to database!
|
VerificationCode string `json:"verification_code" gorm:"-:all"` // this field is only for Email verification, don't save it to database!
|
||||||
AccessToken string `json:"access_token" gorm:"type:char(32);column:access_token;uniqueIndex"` // this token is for system management
|
AccessToken string `json:"access_token" gorm:"type:char(32);column:access_token;uniqueIndex"` // this token is for system management
|
||||||
Quota int `json:"quota" gorm:"type:int;default:0"`
|
Quota int `json:"quota" gorm:"type:int;default:0"`
|
||||||
|
UsedQuota int `json:"used_quota" gorm:"type:int;default:0;column:used_quota"` // used quota
|
||||||
|
RequestCount int `json:"request_count" gorm:"type:int;default:0;"` // request number
|
||||||
Group string `json:"group" gorm:"type:varchar(32);default:'default'"`
|
Group string `json:"group" gorm:"type:varchar(32);default:'default'"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,3 +264,15 @@ func GetRootUserEmail() (email string) {
|
|||||||
DB.Model(&User{}).Where("role = ?", common.RoleRootUser).Select("email").Find(&email)
|
DB.Model(&User{}).Where("role = ?", common.RoleRootUser).Select("email").Find(&email)
|
||||||
return email
|
return email
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func UpdateUserUsedQuotaAndRequestCount(id int, quota int) {
|
||||||
|
err := DB.Model(&User{}).Where("id = ?", id).Updates(
|
||||||
|
map[string]interface{}{
|
||||||
|
"used_quota": gorm.Expr("used_quota + ?", quota),
|
||||||
|
"request_count": gorm.Expr("request_count + ?", 1),
|
||||||
|
},
|
||||||
|
).Error
|
||||||
|
if err != nil {
|
||||||
|
common.SysError("Failed to update user used quota and request count: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ function renderType(type) {
|
|||||||
|
|
||||||
function renderBalance(type, balance) {
|
function renderBalance(type, balance) {
|
||||||
if (type === 5) {
|
if (type === 5) {
|
||||||
return <span>{balance.toFixed(2)}</span>
|
return <span>¥{(balance / 10000).toFixed(2)}</span>
|
||||||
}
|
}
|
||||||
return <span>${balance.toFixed(2)}</span>
|
return <span>${balance.toFixed(2)}</span>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ const SystemSetting = () => {
|
|||||||
TopUpLink: '',
|
TopUpLink: '',
|
||||||
AutomaticDisableChannelEnabled: '',
|
AutomaticDisableChannelEnabled: '',
|
||||||
ChannelDisableThreshold: 0,
|
ChannelDisableThreshold: 0,
|
||||||
|
LogConsumeEnabled: '',
|
||||||
});
|
});
|
||||||
const [originInputs, setOriginInputs] = useState({});
|
const [originInputs, setOriginInputs] = useState({});
|
||||||
let [loading, setLoading] = useState(false);
|
let [loading, setLoading] = useState(false);
|
||||||
@@ -68,6 +69,7 @@ const SystemSetting = () => {
|
|||||||
case 'TurnstileCheckEnabled':
|
case 'TurnstileCheckEnabled':
|
||||||
case 'RegisterEnabled':
|
case 'RegisterEnabled':
|
||||||
case 'AutomaticDisableChannelEnabled':
|
case 'AutomaticDisableChannelEnabled':
|
||||||
|
case 'LogConsumeEnabled':
|
||||||
value = inputs[key] === 'true' ? 'false' : 'true';
|
value = inputs[key] === 'true' ? 'false' : 'true';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -349,6 +351,12 @@ const SystemSetting = () => {
|
|||||||
placeholder='为一个 JSON 文本,键为分组名称,值为倍率'
|
placeholder='为一个 JSON 文本,键为分组名称,值为倍率'
|
||||||
/>
|
/>
|
||||||
</Form.Group>
|
</Form.Group>
|
||||||
|
<Form.Checkbox
|
||||||
|
checked={inputs.LogConsumeEnabled === 'true'}
|
||||||
|
label='启用额度消费日志记录'
|
||||||
|
name='LogConsumeEnabled'
|
||||||
|
onChange={handleInputChange}
|
||||||
|
/>
|
||||||
<Form.Button onClick={submitOperationConfig}>保存运营设置</Form.Button>
|
<Form.Button onClick={submitOperationConfig}>保存运营设置</Form.Button>
|
||||||
<Divider />
|
<Divider />
|
||||||
<Header as='h3'>
|
<Header as='h3'>
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { Link } from 'react-router-dom';
|
|||||||
import { API, showError, showSuccess } from '../helpers';
|
import { API, showError, showSuccess } from '../helpers';
|
||||||
|
|
||||||
import { ITEMS_PER_PAGE } from '../constants';
|
import { ITEMS_PER_PAGE } from '../constants';
|
||||||
import { renderGroup, renderText } from '../helpers/render';
|
import { renderGroup, renderNumber, renderText } from '../helpers/render';
|
||||||
|
|
||||||
function renderRole(role) {
|
function renderRole(role) {
|
||||||
switch (role) {
|
switch (role) {
|
||||||
@@ -197,7 +197,7 @@ const UsersTable = () => {
|
|||||||
sortUser('quota');
|
sortUser('quota');
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
剩余额度
|
统计信息
|
||||||
</Table.HeaderCell>
|
</Table.HeaderCell>
|
||||||
<Table.HeaderCell
|
<Table.HeaderCell
|
||||||
style={{ cursor: 'pointer' }}
|
style={{ cursor: 'pointer' }}
|
||||||
@@ -241,7 +241,11 @@ const UsersTable = () => {
|
|||||||
</Table.Cell>
|
</Table.Cell>
|
||||||
<Table.Cell>{renderGroup(user.group)}</Table.Cell>
|
<Table.Cell>{renderGroup(user.group)}</Table.Cell>
|
||||||
<Table.Cell>{user.email ? renderText(user.email, 30) : '无'}</Table.Cell>
|
<Table.Cell>{user.email ? renderText(user.email, 30) : '无'}</Table.Cell>
|
||||||
<Table.Cell>{user.quota}</Table.Cell>
|
<Table.Cell>
|
||||||
|
<Popup content='剩余额度' trigger={<Label>{renderNumber(user.quota)}</Label>} />
|
||||||
|
<Popup content='已用额度' trigger={<Label>{renderNumber(user.used_quota)}</Label>} />
|
||||||
|
<Popup content='请求次数' trigger={<Label>{renderNumber(user.request_count)}</Label>} />
|
||||||
|
</Table.Cell>
|
||||||
<Table.Cell>{renderRole(user.role)}</Table.Cell>
|
<Table.Cell>{renderRole(user.role)}</Table.Cell>
|
||||||
<Table.Cell>{renderStatus(user.status)}</Table.Cell>
|
<Table.Cell>{renderStatus(user.status)}</Table.Cell>
|
||||||
<Table.Cell>
|
<Table.Cell>
|
||||||
|
|||||||
@@ -8,12 +8,31 @@ export function renderText(text, limit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function renderGroup(group) {
|
export function renderGroup(group) {
|
||||||
if (group === "") {
|
if (group === '') {
|
||||||
return <Label>default</Label>
|
return <Label>default</Label>;
|
||||||
} else if (group === "vip" || group === "pro") {
|
}
|
||||||
return <Label color='yellow'>{group}</Label>
|
let groups = group.split(',');
|
||||||
} else if (group === "svip" || group === "premium") {
|
groups.sort();
|
||||||
return <Label color='red'>{group}</Label>
|
return <>
|
||||||
|
{groups.map((group) => {
|
||||||
|
if (group === 'vip' || group === 'pro') {
|
||||||
|
return <Label color='yellow'>{group}</Label>;
|
||||||
|
} else if (group === 'svip' || group === 'premium') {
|
||||||
|
return <Label color='red'>{group}</Label>;
|
||||||
|
}
|
||||||
|
return <Label>{group}</Label>;
|
||||||
|
})}
|
||||||
|
</>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function renderNumber(num) {
|
||||||
|
if (num >= 1000000000) {
|
||||||
|
return (num / 1000000000).toFixed(1) + 'B';
|
||||||
|
} else if (num >= 1000000) {
|
||||||
|
return (num / 1000000).toFixed(1) + 'M';
|
||||||
|
} else if (num >= 10000) {
|
||||||
|
return (num / 1000).toFixed(1) + 'k';
|
||||||
|
} else {
|
||||||
|
return num;
|
||||||
}
|
}
|
||||||
return <Label>{group}</Label>
|
|
||||||
}
|
}
|
||||||
@@ -15,8 +15,8 @@ const EditChannel = () => {
|
|||||||
key: '',
|
key: '',
|
||||||
base_url: '',
|
base_url: '',
|
||||||
other: '',
|
other: '',
|
||||||
group: 'default',
|
|
||||||
models: [],
|
models: [],
|
||||||
|
groups: ['default']
|
||||||
};
|
};
|
||||||
const [batch, setBatch] = useState(false);
|
const [batch, setBatch] = useState(false);
|
||||||
const [inputs, setInputs] = useState(originInputs);
|
const [inputs, setInputs] = useState(originInputs);
|
||||||
@@ -37,6 +37,11 @@ const EditChannel = () => {
|
|||||||
} else {
|
} else {
|
||||||
data.models = data.models.split(",")
|
data.models = data.models.split(",")
|
||||||
}
|
}
|
||||||
|
if (data.group === "") {
|
||||||
|
data.groups = []
|
||||||
|
} else {
|
||||||
|
data.groups = data.group.split(",")
|
||||||
|
}
|
||||||
setInputs(data);
|
setInputs(data);
|
||||||
} else {
|
} else {
|
||||||
showError(message);
|
showError(message);
|
||||||
@@ -94,6 +99,7 @@ const EditChannel = () => {
|
|||||||
}
|
}
|
||||||
let res;
|
let res;
|
||||||
localInputs.models = localInputs.models.join(",")
|
localInputs.models = localInputs.models.join(",")
|
||||||
|
localInputs.group = localInputs.groups.join(",")
|
||||||
if (isEdit) {
|
if (isEdit) {
|
||||||
res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
|
res = await API.put(`/api/channel/`, { ...localInputs, id: parseInt(channelId) });
|
||||||
} else {
|
} else {
|
||||||
@@ -185,14 +191,14 @@ const EditChannel = () => {
|
|||||||
<Form.Dropdown
|
<Form.Dropdown
|
||||||
label='分组'
|
label='分组'
|
||||||
placeholder={'请选择分组'}
|
placeholder={'请选择分组'}
|
||||||
name='group'
|
name='groups'
|
||||||
fluid
|
fluid
|
||||||
search
|
multiple
|
||||||
selection
|
selection
|
||||||
allowAdditions
|
allowAdditions
|
||||||
additionLabel={'请在系统设置页面编辑分组倍率以添加新的分组:'}
|
additionLabel={'请在系统设置页面编辑分组倍率以添加新的分组:'}
|
||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
value={inputs.group}
|
value={inputs.groups}
|
||||||
autoComplete='new-password'
|
autoComplete='new-password'
|
||||||
options={groupOptions}
|
options={groupOptions}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user