mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-16 21:23:44 +08:00
🔖 chore: migration constants
This commit is contained in:
@@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -66,7 +67,7 @@ func (channel *Channel) AddAbilities() error {
|
||||
Group: group,
|
||||
Model: model,
|
||||
ChannelId: channel.Id,
|
||||
Enabled: channel.Status == common.ChannelStatusEnabled,
|
||||
Enabled: channel.Status == config.ChannelStatusEnabled,
|
||||
Priority: channel.Priority,
|
||||
Weight: channel.Weight,
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package model
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"one-api/common/utils"
|
||||
"strings"
|
||||
@@ -38,7 +38,7 @@ func FilterOnlyChat() ChannelsFilterFunc {
|
||||
}
|
||||
|
||||
func (cc *ChannelsChooser) Cooldowns(channelId int) bool {
|
||||
if common.RetryCooldownSeconds == 0 {
|
||||
if config.RetryCooldownSeconds == 0 {
|
||||
return false
|
||||
}
|
||||
cc.Lock()
|
||||
@@ -47,7 +47,7 @@ func (cc *ChannelsChooser) Cooldowns(channelId int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
cc.Channels[channelId].CooldownsTime = time.Now().Unix() + int64(common.RetryCooldownSeconds)
|
||||
cc.Channels[channelId].CooldownsTime = time.Now().Unix() + int64(config.RetryCooldownSeconds)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ var ChannelGroup = ChannelsChooser{}
|
||||
|
||||
func (cc *ChannelsChooser) Load() {
|
||||
var channels []*Channel
|
||||
DB.Where("status = ?", common.ChannelStatusEnabled).Find(&channels)
|
||||
DB.Where("status = ?", config.ChannelStatusEnabled).Find(&channels)
|
||||
|
||||
abilities, err := GetAbilityChannelGroup()
|
||||
if err != nil {
|
||||
@@ -173,7 +173,7 @@ func (cc *ChannelsChooser) Load() {
|
||||
|
||||
for _, channel := range channels {
|
||||
if *channel.Weight == 0 {
|
||||
channel.Weight = &common.DefaultChannelWeight
|
||||
channel.Weight = &config.DefaultChannelWeight
|
||||
}
|
||||
newChannels[channel.Id] = &ChannelChoice{
|
||||
Channel: channel,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"one-api/common/utils"
|
||||
"strings"
|
||||
@@ -270,11 +270,11 @@ func (channel *Channel) Delete() error {
|
||||
|
||||
func (channel *Channel) StatusToStr() string {
|
||||
switch channel.Status {
|
||||
case common.ChannelStatusEnabled:
|
||||
case config.ChannelStatusEnabled:
|
||||
return "启用"
|
||||
case common.ChannelStatusAutoDisabled:
|
||||
case config.ChannelStatusAutoDisabled:
|
||||
return "自动禁用"
|
||||
case common.ChannelStatusManuallyDisabled:
|
||||
case config.ChannelStatusManuallyDisabled:
|
||||
return "手动禁用"
|
||||
}
|
||||
|
||||
@@ -282,7 +282,7 @@ func (channel *Channel) StatusToStr() string {
|
||||
}
|
||||
|
||||
func UpdateChannelStatusById(id int, status int) {
|
||||
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
|
||||
err := UpdateAbilityStatus(id, status == config.ChannelStatusEnabled)
|
||||
if err != nil {
|
||||
logger.SysError("failed to update ability status: " + err.Error())
|
||||
}
|
||||
@@ -298,7 +298,7 @@ func UpdateChannelStatusById(id int, status int) {
|
||||
}
|
||||
|
||||
func UpdateChannelUsedQuota(id int, quota int) {
|
||||
if common.BatchUpdateEnabled {
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeChannelUsedQuota, id, quota)
|
||||
return
|
||||
}
|
||||
@@ -318,7 +318,7 @@ func DeleteChannelByStatus(status int64) (int64, error) {
|
||||
}
|
||||
|
||||
func DeleteDisabledChannel() (int64, error) {
|
||||
result := DB.Where("status = ? or status = ?", common.ChannelStatusAutoDisabled, common.ChannelStatusManuallyDisabled).Delete(&Channel{})
|
||||
result := DB.Where("status = ? or status = ?", config.ChannelStatusAutoDisabled, config.ChannelStatusManuallyDisabled).Delete(&Channel{})
|
||||
// 同时删除Ability
|
||||
DB.Where("enabled = ?", false).Delete(&Ability{})
|
||||
return result.RowsAffected, result.Error
|
||||
|
||||
@@ -3,6 +3,7 @@ package model
|
||||
import (
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -43,11 +44,11 @@ func PaginateAndOrder[T modelable](db *gorm.DB, params *PaginationParams, result
|
||||
params.Page = 1
|
||||
}
|
||||
if params.Size < 1 {
|
||||
params.Size = common.ItemsPerPage
|
||||
params.Size = config.ItemsPerPage
|
||||
}
|
||||
|
||||
if params.Size > common.MaxRecentItems {
|
||||
return nil, fmt.Errorf("size 参数不能超过 %d", common.MaxRecentItems)
|
||||
if params.Size > config.MaxRecentItems {
|
||||
return nil, fmt.Errorf("size 参数不能超过 %d", config.MaxRecentItems)
|
||||
}
|
||||
|
||||
offset := (params.Page - 1) * params.Size
|
||||
|
||||
10
model/log.go
10
model/log.go
@@ -3,7 +3,7 @@ package model
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"one-api/common/utils"
|
||||
|
||||
@@ -37,7 +37,7 @@ const (
|
||||
)
|
||||
|
||||
func RecordLog(userId int, logType int, content string) {
|
||||
if logType == LogTypeConsume && !common.LogConsumeEnabled {
|
||||
if logType == LogTypeConsume && !config.LogConsumeEnabled {
|
||||
return
|
||||
}
|
||||
log := &Log{
|
||||
@@ -55,7 +55,7 @@ func RecordLog(userId int, logType int, content string) {
|
||||
|
||||
func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string, requestTime int) {
|
||||
logger.LogInfo(ctx, fmt.Sprintf("record consume log: userId=%d, channelId=%d, promptTokens=%d, completionTokens=%d, modelName=%s, tokenName=%s, quota=%d, content=%s", userId, channelId, promptTokens, completionTokens, modelName, tokenName, quota, content))
|
||||
if !common.LogConsumeEnabled {
|
||||
if !config.LogConsumeEnabled {
|
||||
return
|
||||
}
|
||||
log := &Log{
|
||||
@@ -156,12 +156,12 @@ func GetUserLogsList(userId int, params *LogsListParams) (*DataResult[Log], erro
|
||||
}
|
||||
|
||||
func SearchAllLogs(keyword string) (logs []*Log, err error) {
|
||||
err = DB.Where("type = ? or content LIKE ?", keyword, keyword+"%").Order("id desc").Limit(common.MaxRecentItems).Find(&logs).Error
|
||||
err = DB.Where("type = ? or content LIKE ?", keyword, keyword+"%").Order("id desc").Limit(config.MaxRecentItems).Find(&logs).Error
|
||||
return logs, err
|
||||
}
|
||||
|
||||
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(config.MaxRecentItems).Omit("id").Find(&logs).Error
|
||||
return logs, err
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package model
|
||||
import (
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"one-api/common/utils"
|
||||
"strconv"
|
||||
@@ -24,12 +25,12 @@ func SetupDB() {
|
||||
logger.FatalLog("failed to initialize database: " + err.Error())
|
||||
}
|
||||
ChannelGroup.Load()
|
||||
common.RootUserEmail = GetRootUserEmail()
|
||||
config.RootUserEmail = GetRootUserEmail()
|
||||
|
||||
if viper.GetBool("batch_update_enabled") {
|
||||
common.BatchUpdateEnabled = true
|
||||
common.BatchUpdateInterval = utils.GetOrDefault("batch_update_interval", 5)
|
||||
logger.SysLog("batch update enabled with interval " + strconv.Itoa(common.BatchUpdateInterval) + "s")
|
||||
config.BatchUpdateEnabled = true
|
||||
config.BatchUpdateInterval = utils.GetOrDefault("batch_update_interval", 5)
|
||||
logger.SysLog("batch update enabled with interval " + strconv.Itoa(config.BatchUpdateInterval) + "s")
|
||||
InitBatchUpdater()
|
||||
}
|
||||
}
|
||||
@@ -46,8 +47,8 @@ func createRootAccountIfNeed() error {
|
||||
rootUser := User{
|
||||
Username: "root",
|
||||
Password: hashedPassword,
|
||||
Role: common.RoleRootUser,
|
||||
Status: common.UserStatusEnabled,
|
||||
Role: config.RoleRootUser,
|
||||
Status: config.UserStatusEnabled,
|
||||
DisplayName: "Root User",
|
||||
AccessToken: utils.GetUUID(),
|
||||
Quota: 100000000,
|
||||
@@ -102,7 +103,7 @@ func InitDB() (err error) {
|
||||
sqlDB.SetMaxOpenConns(utils.GetOrDefault("SQL_MAX_OPEN_CONNS", 1000))
|
||||
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(utils.GetOrDefault("SQL_MAX_LIFETIME", 60)))
|
||||
|
||||
if !common.IsMasterNode {
|
||||
if !config.IsMasterNode {
|
||||
return nil
|
||||
}
|
||||
logger.SysLog("database migration started")
|
||||
|
||||
213
model/option.go
213
model/option.go
@@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -26,63 +27,63 @@ func GetOption(key string) (option Option, err error) {
|
||||
}
|
||||
|
||||
func InitOptionMap() {
|
||||
common.OptionMapRWMutex.Lock()
|
||||
common.OptionMap = make(map[string]string)
|
||||
common.OptionMap["PasswordLoginEnabled"] = strconv.FormatBool(common.PasswordLoginEnabled)
|
||||
common.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(common.PasswordRegisterEnabled)
|
||||
common.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(common.EmailVerificationEnabled)
|
||||
common.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(common.GitHubOAuthEnabled)
|
||||
common.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(common.WeChatAuthEnabled)
|
||||
common.OptionMap["LarkAuthEnabled"] = strconv.FormatBool(common.LarkAuthEnabled)
|
||||
common.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(common.TurnstileCheckEnabled)
|
||||
common.OptionMap["RegisterEnabled"] = strconv.FormatBool(common.RegisterEnabled)
|
||||
common.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(common.AutomaticDisableChannelEnabled)
|
||||
common.OptionMap["AutomaticEnableChannelEnabled"] = strconv.FormatBool(common.AutomaticEnableChannelEnabled)
|
||||
common.OptionMap["ApproximateTokenEnabled"] = strconv.FormatBool(common.ApproximateTokenEnabled)
|
||||
common.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(common.LogConsumeEnabled)
|
||||
common.OptionMap["DisplayInCurrencyEnabled"] = strconv.FormatBool(common.DisplayInCurrencyEnabled)
|
||||
common.OptionMap["DisplayTokenStatEnabled"] = strconv.FormatBool(common.DisplayTokenStatEnabled)
|
||||
common.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(common.ChannelDisableThreshold, 'f', -1, 64)
|
||||
common.OptionMap["EmailDomainRestrictionEnabled"] = strconv.FormatBool(common.EmailDomainRestrictionEnabled)
|
||||
common.OptionMap["EmailDomainWhitelist"] = strings.Join(common.EmailDomainWhitelist, ",")
|
||||
common.OptionMap["SMTPServer"] = ""
|
||||
common.OptionMap["SMTPFrom"] = ""
|
||||
common.OptionMap["SMTPPort"] = strconv.Itoa(common.SMTPPort)
|
||||
common.OptionMap["SMTPAccount"] = ""
|
||||
common.OptionMap["SMTPToken"] = ""
|
||||
common.OptionMap["Notice"] = ""
|
||||
common.OptionMap["About"] = ""
|
||||
common.OptionMap["HomePageContent"] = ""
|
||||
common.OptionMap["Footer"] = common.Footer
|
||||
common.OptionMap["SystemName"] = common.SystemName
|
||||
common.OptionMap["Logo"] = common.Logo
|
||||
common.OptionMap["ServerAddress"] = ""
|
||||
common.OptionMap["GitHubClientId"] = ""
|
||||
common.OptionMap["GitHubClientSecret"] = ""
|
||||
common.OptionMap["WeChatServerAddress"] = ""
|
||||
common.OptionMap["WeChatServerToken"] = ""
|
||||
common.OptionMap["WeChatAccountQRCodeImageURL"] = ""
|
||||
common.OptionMap["TurnstileSiteKey"] = ""
|
||||
common.OptionMap["TurnstileSecretKey"] = ""
|
||||
common.OptionMap["QuotaForNewUser"] = strconv.Itoa(common.QuotaForNewUser)
|
||||
common.OptionMap["QuotaForInviter"] = strconv.Itoa(common.QuotaForInviter)
|
||||
common.OptionMap["QuotaForInvitee"] = strconv.Itoa(common.QuotaForInvitee)
|
||||
common.OptionMap["QuotaRemindThreshold"] = strconv.Itoa(common.QuotaRemindThreshold)
|
||||
common.OptionMap["PreConsumedQuota"] = strconv.Itoa(common.PreConsumedQuota)
|
||||
common.OptionMap["GroupRatio"] = common.GroupRatio2JSONString()
|
||||
common.OptionMap["TopUpLink"] = common.TopUpLink
|
||||
common.OptionMap["ChatLink"] = common.ChatLink
|
||||
common.OptionMap["ChatLinks"] = common.ChatLinks
|
||||
common.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(common.QuotaPerUnit, 'f', -1, 64)
|
||||
common.OptionMap["RetryTimes"] = strconv.Itoa(common.RetryTimes)
|
||||
common.OptionMap["RetryCooldownSeconds"] = strconv.Itoa(common.RetryCooldownSeconds)
|
||||
config.OptionMapRWMutex.Lock()
|
||||
config.OptionMap = make(map[string]string)
|
||||
config.OptionMap["PasswordLoginEnabled"] = strconv.FormatBool(config.PasswordLoginEnabled)
|
||||
config.OptionMap["PasswordRegisterEnabled"] = strconv.FormatBool(config.PasswordRegisterEnabled)
|
||||
config.OptionMap["EmailVerificationEnabled"] = strconv.FormatBool(config.EmailVerificationEnabled)
|
||||
config.OptionMap["GitHubOAuthEnabled"] = strconv.FormatBool(config.GitHubOAuthEnabled)
|
||||
config.OptionMap["WeChatAuthEnabled"] = strconv.FormatBool(config.WeChatAuthEnabled)
|
||||
config.OptionMap["LarkAuthEnabled"] = strconv.FormatBool(config.LarkAuthEnabled)
|
||||
config.OptionMap["TurnstileCheckEnabled"] = strconv.FormatBool(config.TurnstileCheckEnabled)
|
||||
config.OptionMap["RegisterEnabled"] = strconv.FormatBool(config.RegisterEnabled)
|
||||
config.OptionMap["AutomaticDisableChannelEnabled"] = strconv.FormatBool(config.AutomaticDisableChannelEnabled)
|
||||
config.OptionMap["AutomaticEnableChannelEnabled"] = strconv.FormatBool(config.AutomaticEnableChannelEnabled)
|
||||
config.OptionMap["ApproximateTokenEnabled"] = strconv.FormatBool(config.ApproximateTokenEnabled)
|
||||
config.OptionMap["LogConsumeEnabled"] = strconv.FormatBool(config.LogConsumeEnabled)
|
||||
config.OptionMap["DisplayInCurrencyEnabled"] = strconv.FormatBool(config.DisplayInCurrencyEnabled)
|
||||
config.OptionMap["DisplayTokenStatEnabled"] = strconv.FormatBool(config.DisplayTokenStatEnabled)
|
||||
config.OptionMap["ChannelDisableThreshold"] = strconv.FormatFloat(config.ChannelDisableThreshold, 'f', -1, 64)
|
||||
config.OptionMap["EmailDomainRestrictionEnabled"] = strconv.FormatBool(config.EmailDomainRestrictionEnabled)
|
||||
config.OptionMap["EmailDomainWhitelist"] = strings.Join(config.EmailDomainWhitelist, ",")
|
||||
config.OptionMap["SMTPServer"] = ""
|
||||
config.OptionMap["SMTPFrom"] = ""
|
||||
config.OptionMap["SMTPPort"] = strconv.Itoa(config.SMTPPort)
|
||||
config.OptionMap["SMTPAccount"] = ""
|
||||
config.OptionMap["SMTPToken"] = ""
|
||||
config.OptionMap["Notice"] = ""
|
||||
config.OptionMap["About"] = ""
|
||||
config.OptionMap["HomePageContent"] = ""
|
||||
config.OptionMap["Footer"] = config.Footer
|
||||
config.OptionMap["SystemName"] = config.SystemName
|
||||
config.OptionMap["Logo"] = config.Logo
|
||||
config.OptionMap["ServerAddress"] = ""
|
||||
config.OptionMap["GitHubClientId"] = ""
|
||||
config.OptionMap["GitHubClientSecret"] = ""
|
||||
config.OptionMap["WeChatServerAddress"] = ""
|
||||
config.OptionMap["WeChatServerToken"] = ""
|
||||
config.OptionMap["WeChatAccountQRCodeImageURL"] = ""
|
||||
config.OptionMap["TurnstileSiteKey"] = ""
|
||||
config.OptionMap["TurnstileSecretKey"] = ""
|
||||
config.OptionMap["QuotaForNewUser"] = strconv.Itoa(config.QuotaForNewUser)
|
||||
config.OptionMap["QuotaForInviter"] = strconv.Itoa(config.QuotaForInviter)
|
||||
config.OptionMap["QuotaForInvitee"] = strconv.Itoa(config.QuotaForInvitee)
|
||||
config.OptionMap["QuotaRemindThreshold"] = strconv.Itoa(config.QuotaRemindThreshold)
|
||||
config.OptionMap["PreConsumedQuota"] = strconv.Itoa(config.PreConsumedQuota)
|
||||
config.OptionMap["GroupRatio"] = common.GroupRatio2JSONString()
|
||||
config.OptionMap["TopUpLink"] = config.TopUpLink
|
||||
config.OptionMap["ChatLink"] = config.ChatLink
|
||||
config.OptionMap["ChatLinks"] = config.ChatLinks
|
||||
config.OptionMap["QuotaPerUnit"] = strconv.FormatFloat(config.QuotaPerUnit, 'f', -1, 64)
|
||||
config.OptionMap["RetryTimes"] = strconv.Itoa(config.RetryTimes)
|
||||
config.OptionMap["RetryCooldownSeconds"] = strconv.Itoa(config.RetryCooldownSeconds)
|
||||
|
||||
common.OptionMap["MjNotifyEnabled"] = strconv.FormatBool(common.MjNotifyEnabled)
|
||||
config.OptionMap["MjNotifyEnabled"] = strconv.FormatBool(config.MjNotifyEnabled)
|
||||
|
||||
common.OptionMap["ChatCacheEnabled"] = strconv.FormatBool(common.ChatCacheEnabled)
|
||||
common.OptionMap["ChatCacheExpireMinute"] = strconv.Itoa(common.ChatCacheExpireMinute)
|
||||
config.OptionMap["ChatCacheEnabled"] = strconv.FormatBool(config.ChatCacheEnabled)
|
||||
config.OptionMap["ChatCacheExpireMinute"] = strconv.Itoa(config.ChatCacheExpireMinute)
|
||||
|
||||
common.OptionMapRWMutex.Unlock()
|
||||
config.OptionMapRWMutex.Unlock()
|
||||
loadOptionsFromDatabase()
|
||||
}
|
||||
|
||||
@@ -121,64 +122,64 @@ func UpdateOption(key string, value string) error {
|
||||
}
|
||||
|
||||
var optionIntMap = map[string]*int{
|
||||
"SMTPPort": &common.SMTPPort,
|
||||
"QuotaForNewUser": &common.QuotaForNewUser,
|
||||
"QuotaForInviter": &common.QuotaForInviter,
|
||||
"QuotaForInvitee": &common.QuotaForInvitee,
|
||||
"QuotaRemindThreshold": &common.QuotaRemindThreshold,
|
||||
"PreConsumedQuota": &common.PreConsumedQuota,
|
||||
"RetryTimes": &common.RetryTimes,
|
||||
"RetryCooldownSeconds": &common.RetryCooldownSeconds,
|
||||
"ChatCacheExpireMinute": &common.ChatCacheExpireMinute,
|
||||
"SMTPPort": &config.SMTPPort,
|
||||
"QuotaForNewUser": &config.QuotaForNewUser,
|
||||
"QuotaForInviter": &config.QuotaForInviter,
|
||||
"QuotaForInvitee": &config.QuotaForInvitee,
|
||||
"QuotaRemindThreshold": &config.QuotaRemindThreshold,
|
||||
"PreConsumedQuota": &config.PreConsumedQuota,
|
||||
"RetryTimes": &config.RetryTimes,
|
||||
"RetryCooldownSeconds": &config.RetryCooldownSeconds,
|
||||
"ChatCacheExpireMinute": &config.ChatCacheExpireMinute,
|
||||
}
|
||||
|
||||
var optionBoolMap = map[string]*bool{
|
||||
"PasswordRegisterEnabled": &common.PasswordRegisterEnabled,
|
||||
"PasswordLoginEnabled": &common.PasswordLoginEnabled,
|
||||
"EmailVerificationEnabled": &common.EmailVerificationEnabled,
|
||||
"GitHubOAuthEnabled": &common.GitHubOAuthEnabled,
|
||||
"WeChatAuthEnabled": &common.WeChatAuthEnabled,
|
||||
"LarkAuthEnabled": &common.LarkAuthEnabled,
|
||||
"TurnstileCheckEnabled": &common.TurnstileCheckEnabled,
|
||||
"RegisterEnabled": &common.RegisterEnabled,
|
||||
"EmailDomainRestrictionEnabled": &common.EmailDomainRestrictionEnabled,
|
||||
"AutomaticDisableChannelEnabled": &common.AutomaticDisableChannelEnabled,
|
||||
"AutomaticEnableChannelEnabled": &common.AutomaticEnableChannelEnabled,
|
||||
"ApproximateTokenEnabled": &common.ApproximateTokenEnabled,
|
||||
"LogConsumeEnabled": &common.LogConsumeEnabled,
|
||||
"DisplayInCurrencyEnabled": &common.DisplayInCurrencyEnabled,
|
||||
"DisplayTokenStatEnabled": &common.DisplayTokenStatEnabled,
|
||||
"MjNotifyEnabled": &common.MjNotifyEnabled,
|
||||
"ChatCacheEnabled": &common.ChatCacheEnabled,
|
||||
"PasswordRegisterEnabled": &config.PasswordRegisterEnabled,
|
||||
"PasswordLoginEnabled": &config.PasswordLoginEnabled,
|
||||
"EmailVerificationEnabled": &config.EmailVerificationEnabled,
|
||||
"GitHubOAuthEnabled": &config.GitHubOAuthEnabled,
|
||||
"WeChatAuthEnabled": &config.WeChatAuthEnabled,
|
||||
"LarkAuthEnabled": &config.LarkAuthEnabled,
|
||||
"TurnstileCheckEnabled": &config.TurnstileCheckEnabled,
|
||||
"RegisterEnabled": &config.RegisterEnabled,
|
||||
"EmailDomainRestrictionEnabled": &config.EmailDomainRestrictionEnabled,
|
||||
"AutomaticDisableChannelEnabled": &config.AutomaticDisableChannelEnabled,
|
||||
"AutomaticEnableChannelEnabled": &config.AutomaticEnableChannelEnabled,
|
||||
"ApproximateTokenEnabled": &config.ApproximateTokenEnabled,
|
||||
"LogConsumeEnabled": &config.LogConsumeEnabled,
|
||||
"DisplayInCurrencyEnabled": &config.DisplayInCurrencyEnabled,
|
||||
"DisplayTokenStatEnabled": &config.DisplayTokenStatEnabled,
|
||||
"MjNotifyEnabled": &config.MjNotifyEnabled,
|
||||
"ChatCacheEnabled": &config.ChatCacheEnabled,
|
||||
}
|
||||
|
||||
var optionStringMap = map[string]*string{
|
||||
"SMTPServer": &common.SMTPServer,
|
||||
"SMTPAccount": &common.SMTPAccount,
|
||||
"SMTPFrom": &common.SMTPFrom,
|
||||
"SMTPToken": &common.SMTPToken,
|
||||
"ServerAddress": &common.ServerAddress,
|
||||
"GitHubClientId": &common.GitHubClientId,
|
||||
"GitHubClientSecret": &common.GitHubClientSecret,
|
||||
"Footer": &common.Footer,
|
||||
"SystemName": &common.SystemName,
|
||||
"Logo": &common.Logo,
|
||||
"WeChatServerAddress": &common.WeChatServerAddress,
|
||||
"WeChatServerToken": &common.WeChatServerToken,
|
||||
"WeChatAccountQRCodeImageURL": &common.WeChatAccountQRCodeImageURL,
|
||||
"TurnstileSiteKey": &common.TurnstileSiteKey,
|
||||
"TurnstileSecretKey": &common.TurnstileSecretKey,
|
||||
"TopUpLink": &common.TopUpLink,
|
||||
"ChatLink": &common.ChatLink,
|
||||
"ChatLinks": &common.ChatLinks,
|
||||
"LarkClientId": &common.LarkClientId,
|
||||
"LarkClientSecret": &common.LarkClientSecret,
|
||||
"SMTPServer": &config.SMTPServer,
|
||||
"SMTPAccount": &config.SMTPAccount,
|
||||
"SMTPFrom": &config.SMTPFrom,
|
||||
"SMTPToken": &config.SMTPToken,
|
||||
"ServerAddress": &config.ServerAddress,
|
||||
"GitHubClientId": &config.GitHubClientId,
|
||||
"GitHubClientSecret": &config.GitHubClientSecret,
|
||||
"Footer": &config.Footer,
|
||||
"SystemName": &config.SystemName,
|
||||
"Logo": &config.Logo,
|
||||
"WeChatServerAddress": &config.WeChatServerAddress,
|
||||
"WeChatServerToken": &config.WeChatServerToken,
|
||||
"WeChatAccountQRCodeImageURL": &config.WeChatAccountQRCodeImageURL,
|
||||
"TurnstileSiteKey": &config.TurnstileSiteKey,
|
||||
"TurnstileSecretKey": &config.TurnstileSecretKey,
|
||||
"TopUpLink": &config.TopUpLink,
|
||||
"ChatLink": &config.ChatLink,
|
||||
"ChatLinks": &config.ChatLinks,
|
||||
"LarkClientId": &config.LarkClientId,
|
||||
"LarkClientSecret": &config.LarkClientSecret,
|
||||
}
|
||||
|
||||
func updateOptionMap(key string, value string) (err error) {
|
||||
common.OptionMapRWMutex.Lock()
|
||||
defer common.OptionMapRWMutex.Unlock()
|
||||
common.OptionMap[key] = value
|
||||
config.OptionMapRWMutex.Lock()
|
||||
defer config.OptionMapRWMutex.Unlock()
|
||||
config.OptionMap[key] = value
|
||||
if ptr, ok := optionIntMap[key]; ok {
|
||||
*ptr, _ = strconv.Atoi(value)
|
||||
return
|
||||
@@ -196,13 +197,13 @@ func updateOptionMap(key string, value string) (err error) {
|
||||
|
||||
switch key {
|
||||
case "EmailDomainWhitelist":
|
||||
common.EmailDomainWhitelist = strings.Split(value, ",")
|
||||
config.EmailDomainWhitelist = strings.Split(value, ",")
|
||||
case "GroupRatio":
|
||||
err = common.UpdateGroupRatioByJSONString(value)
|
||||
case "ChannelDisableThreshold":
|
||||
common.ChannelDisableThreshold, _ = strconv.ParseFloat(value, 64)
|
||||
config.ChannelDisableThreshold, _ = strconv.ParseFloat(value, 64)
|
||||
case "QuotaPerUnit":
|
||||
common.QuotaPerUnit, _ = strconv.ParseFloat(value, 64)
|
||||
config.QuotaPerUnit, _ = strconv.ParseFloat(value, 64)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
260
model/price.go
260
model/price.go
@@ -1,7 +1,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
@@ -114,211 +114,211 @@ type ModelType struct {
|
||||
func GetDefaultPrice() []*Price {
|
||||
ModelTypes := map[string]ModelType{
|
||||
// $0.03 / 1K tokens $0.06 / 1K tokens
|
||||
"gpt-4": {[]float64{15, 30}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-0314": {[]float64{15, 30}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-0613": {[]float64{15, 30}, common.ChannelTypeOpenAI},
|
||||
"gpt-4": {[]float64{15, 30}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-0314": {[]float64{15, 30}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-0613": {[]float64{15, 30}, config.ChannelTypeOpenAI},
|
||||
// $0.06 / 1K tokens $0.12 / 1K tokens
|
||||
"gpt-4-32k": {[]float64{30, 60}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-32k-0314": {[]float64{30, 60}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-32k-0613": {[]float64{30, 60}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-32k": {[]float64{30, 60}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-32k-0314": {[]float64{30, 60}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-32k-0613": {[]float64{30, 60}, config.ChannelTypeOpenAI},
|
||||
// $0.01 / 1K tokens $0.03 / 1K tokens
|
||||
"gpt-4-preview": {[]float64{5, 15}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-turbo": {[]float64{5, 15}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-turbo-2024-04-09": {[]float64{5, 15}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-1106-preview": {[]float64{5, 15}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-0125-preview": {[]float64{5, 15}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-turbo-preview": {[]float64{5, 15}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-vision-preview": {[]float64{5, 15}, common.ChannelTypeOpenAI},
|
||||
"gpt-4-preview": {[]float64{5, 15}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-turbo": {[]float64{5, 15}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-turbo-2024-04-09": {[]float64{5, 15}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-1106-preview": {[]float64{5, 15}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-0125-preview": {[]float64{5, 15}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-turbo-preview": {[]float64{5, 15}, config.ChannelTypeOpenAI},
|
||||
"gpt-4-vision-preview": {[]float64{5, 15}, config.ChannelTypeOpenAI},
|
||||
// $0.005 / 1K tokens $0.015 / 1K tokens
|
||||
"gpt-4o": {[]float64{2.5, 7.5}, common.ChannelTypeOpenAI},
|
||||
"gpt-4o": {[]float64{2.5, 7.5}, config.ChannelTypeOpenAI},
|
||||
// $0.0005 / 1K tokens $0.0015 / 1K tokens
|
||||
"gpt-3.5-turbo": {[]float64{0.25, 0.75}, common.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-0125": {[]float64{0.25, 0.75}, common.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo": {[]float64{0.25, 0.75}, config.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-0125": {[]float64{0.25, 0.75}, config.ChannelTypeOpenAI},
|
||||
// $0.0015 / 1K tokens $0.002 / 1K tokens
|
||||
"gpt-3.5-turbo-0301": {[]float64{0.75, 1}, common.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-0613": {[]float64{0.75, 1}, common.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-instruct": {[]float64{0.75, 1}, common.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-0301": {[]float64{0.75, 1}, config.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-0613": {[]float64{0.75, 1}, config.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-instruct": {[]float64{0.75, 1}, config.ChannelTypeOpenAI},
|
||||
// $0.003 / 1K tokens $0.004 / 1K tokens
|
||||
"gpt-3.5-turbo-16k": {[]float64{1.5, 2}, common.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-16k-0613": {[]float64{1.5, 2}, common.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-16k": {[]float64{1.5, 2}, config.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-16k-0613": {[]float64{1.5, 2}, config.ChannelTypeOpenAI},
|
||||
// $0.001 / 1K tokens $0.002 / 1K tokens
|
||||
"gpt-3.5-turbo-1106": {[]float64{0.5, 1}, common.ChannelTypeOpenAI},
|
||||
"gpt-3.5-turbo-1106": {[]float64{0.5, 1}, config.ChannelTypeOpenAI},
|
||||
// $0.0020 / 1K tokens
|
||||
"davinci-002": {[]float64{1, 1}, common.ChannelTypeOpenAI},
|
||||
"davinci-002": {[]float64{1, 1}, config.ChannelTypeOpenAI},
|
||||
// $0.0004 / 1K tokens
|
||||
"babbage-002": {[]float64{0.2, 0.2}, common.ChannelTypeOpenAI},
|
||||
"babbage-002": {[]float64{0.2, 0.2}, config.ChannelTypeOpenAI},
|
||||
// $0.006 / minute -> $0.006 / 150 words -> $0.006 / 200 tokens -> $0.03 / 1k tokens
|
||||
"whisper-1": {[]float64{15, 15}, common.ChannelTypeOpenAI},
|
||||
"whisper-1": {[]float64{15, 15}, config.ChannelTypeOpenAI},
|
||||
// $0.015 / 1K characters
|
||||
"tts-1": {[]float64{7.5, 7.5}, common.ChannelTypeOpenAI},
|
||||
"tts-1-1106": {[]float64{7.5, 7.5}, common.ChannelTypeOpenAI},
|
||||
"tts-1": {[]float64{7.5, 7.5}, config.ChannelTypeOpenAI},
|
||||
"tts-1-1106": {[]float64{7.5, 7.5}, config.ChannelTypeOpenAI},
|
||||
// $0.030 / 1K characters
|
||||
"tts-1-hd": {[]float64{15, 15}, common.ChannelTypeOpenAI},
|
||||
"tts-1-hd-1106": {[]float64{15, 15}, common.ChannelTypeOpenAI},
|
||||
"text-embedding-ada-002": {[]float64{0.05, 0.05}, common.ChannelTypeOpenAI},
|
||||
"tts-1-hd": {[]float64{15, 15}, config.ChannelTypeOpenAI},
|
||||
"tts-1-hd-1106": {[]float64{15, 15}, config.ChannelTypeOpenAI},
|
||||
"text-embedding-ada-002": {[]float64{0.05, 0.05}, config.ChannelTypeOpenAI},
|
||||
// $0.00002 / 1K tokens
|
||||
"text-embedding-3-small": {[]float64{0.01, 0.01}, common.ChannelTypeOpenAI},
|
||||
"text-embedding-3-small": {[]float64{0.01, 0.01}, config.ChannelTypeOpenAI},
|
||||
// $0.00013 / 1K tokens
|
||||
"text-embedding-3-large": {[]float64{0.065, 0.065}, common.ChannelTypeOpenAI},
|
||||
"text-moderation-stable": {[]float64{0.1, 0.1}, common.ChannelTypeOpenAI},
|
||||
"text-moderation-latest": {[]float64{0.1, 0.1}, common.ChannelTypeOpenAI},
|
||||
"text-embedding-3-large": {[]float64{0.065, 0.065}, config.ChannelTypeOpenAI},
|
||||
"text-moderation-stable": {[]float64{0.1, 0.1}, config.ChannelTypeOpenAI},
|
||||
"text-moderation-latest": {[]float64{0.1, 0.1}, config.ChannelTypeOpenAI},
|
||||
// $0.016 - $0.020 / image
|
||||
"dall-e-2": {[]float64{8, 8}, common.ChannelTypeOpenAI},
|
||||
"dall-e-2": {[]float64{8, 8}, config.ChannelTypeOpenAI},
|
||||
// $0.040 - $0.120 / image
|
||||
"dall-e-3": {[]float64{20, 20}, common.ChannelTypeOpenAI},
|
||||
"dall-e-3": {[]float64{20, 20}, config.ChannelTypeOpenAI},
|
||||
|
||||
// $0.80/million tokens $2.40/million tokens
|
||||
"claude-instant-1.2": {[]float64{0.4, 1.2}, common.ChannelTypeAnthropic},
|
||||
"claude-instant-1.2": {[]float64{0.4, 1.2}, config.ChannelTypeAnthropic},
|
||||
// $8.00/million tokens $24.00/million tokens
|
||||
"claude-2.0": {[]float64{4, 12}, common.ChannelTypeAnthropic},
|
||||
"claude-2.1": {[]float64{4, 12}, common.ChannelTypeAnthropic},
|
||||
"claude-2.0": {[]float64{4, 12}, config.ChannelTypeAnthropic},
|
||||
"claude-2.1": {[]float64{4, 12}, config.ChannelTypeAnthropic},
|
||||
// $15 / M $75 / M
|
||||
"claude-3-opus-20240229": {[]float64{7.5, 22.5}, common.ChannelTypeAnthropic},
|
||||
"claude-3-opus-20240229": {[]float64{7.5, 22.5}, config.ChannelTypeAnthropic},
|
||||
// $3 / M $15 / M
|
||||
"claude-3-sonnet-20240229": {[]float64{1.3, 3.9}, common.ChannelTypeAnthropic},
|
||||
"claude-3-sonnet-20240229": {[]float64{1.3, 3.9}, config.ChannelTypeAnthropic},
|
||||
// $0.25 / M $1.25 / M 0.00025$ / 1k tokens 0.00125$ / 1k tokens
|
||||
"claude-3-haiku-20240307": {[]float64{0.125, 0.625}, common.ChannelTypeAnthropic},
|
||||
"claude-3-haiku-20240307": {[]float64{0.125, 0.625}, config.ChannelTypeAnthropic},
|
||||
|
||||
// ¥0.004 / 1k tokens ¥0.008 / 1k tokens
|
||||
"ERNIE-Speed": {[]float64{0.2857, 0.5714}, common.ChannelTypeBaidu},
|
||||
"ERNIE-Speed": {[]float64{0.2857, 0.5714}, config.ChannelTypeBaidu},
|
||||
// ¥0.012 / 1k tokens ¥0.012 / 1k tokens
|
||||
"ERNIE-Bot": {[]float64{0.8572, 0.8572}, common.ChannelTypeBaidu},
|
||||
"ERNIE-3.5-8K": {[]float64{0.8572, 0.8572}, common.ChannelTypeBaidu},
|
||||
"ERNIE-Bot": {[]float64{0.8572, 0.8572}, config.ChannelTypeBaidu},
|
||||
"ERNIE-3.5-8K": {[]float64{0.8572, 0.8572}, config.ChannelTypeBaidu},
|
||||
// 0.024元/千tokens 0.048元/千tokens
|
||||
"ERNIE-Bot-8k": {[]float64{1.7143, 3.4286}, common.ChannelTypeBaidu},
|
||||
"ERNIE-Bot-8k": {[]float64{1.7143, 3.4286}, config.ChannelTypeBaidu},
|
||||
// ¥0.008 / 1k tokens ¥0.008 / 1k tokens
|
||||
"ERNIE-Bot-turbo": {[]float64{0.5715, 0.5715}, common.ChannelTypeBaidu},
|
||||
"ERNIE-Bot-turbo": {[]float64{0.5715, 0.5715}, config.ChannelTypeBaidu},
|
||||
// ¥0.12 / 1k tokens ¥0.12 / 1k tokens
|
||||
"ERNIE-Bot-4": {[]float64{8.572, 8.572}, common.ChannelTypeBaidu},
|
||||
"ERNIE-4.0": {[]float64{8.572, 8.572}, common.ChannelTypeBaidu},
|
||||
"ERNIE-Bot-4": {[]float64{8.572, 8.572}, config.ChannelTypeBaidu},
|
||||
"ERNIE-4.0": {[]float64{8.572, 8.572}, config.ChannelTypeBaidu},
|
||||
// ¥0.002 / 1k tokens
|
||||
"Embedding-V1": {[]float64{0.1429, 0.1429}, common.ChannelTypeBaidu},
|
||||
"Embedding-V1": {[]float64{0.1429, 0.1429}, config.ChannelTypeBaidu},
|
||||
// ¥0.004 / 1k tokens
|
||||
"BLOOMZ-7B": {[]float64{0.2857, 0.2857}, common.ChannelTypeBaidu},
|
||||
"BLOOMZ-7B": {[]float64{0.2857, 0.2857}, config.ChannelTypeBaidu},
|
||||
|
||||
"PaLM-2": {[]float64{1, 1}, common.ChannelTypePaLM},
|
||||
"PaLM-2": {[]float64{1, 1}, config.ChannelTypePaLM},
|
||||
// $0.50 / 1 million tokens $1.50 / 1 million tokens
|
||||
// 0.0005$ / 1k tokens 0.0015$ / 1k tokens
|
||||
"gemini-pro": {[]float64{0.25, 0.75}, common.ChannelTypeGemini},
|
||||
"gemini-pro-vision": {[]float64{0.25, 0.75}, common.ChannelTypeGemini},
|
||||
"gemini-1.0-pro": {[]float64{0.25, 0.75}, common.ChannelTypeGemini},
|
||||
"gemini-pro": {[]float64{0.25, 0.75}, config.ChannelTypeGemini},
|
||||
"gemini-pro-vision": {[]float64{0.25, 0.75}, config.ChannelTypeGemini},
|
||||
"gemini-1.0-pro": {[]float64{0.25, 0.75}, config.ChannelTypeGemini},
|
||||
// $7 / 1 million tokens $21 / 1 million tokens
|
||||
"gemini-1.5-pro": {[]float64{1.75, 5.25}, common.ChannelTypeGemini},
|
||||
"gemini-1.5-pro-latest": {[]float64{1.75, 5.25}, common.ChannelTypeGemini},
|
||||
"gemini-1.5-flash": {[]float64{0.175, 0.265}, common.ChannelTypeGemini},
|
||||
"gemini-1.5-flash-latest": {[]float64{0.175, 0.265}, common.ChannelTypeGemini},
|
||||
"gemini-ultra": {[]float64{1, 1}, common.ChannelTypeGemini},
|
||||
"gemini-1.5-pro": {[]float64{1.75, 5.25}, config.ChannelTypeGemini},
|
||||
"gemini-1.5-pro-latest": {[]float64{1.75, 5.25}, config.ChannelTypeGemini},
|
||||
"gemini-1.5-flash": {[]float64{0.175, 0.265}, config.ChannelTypeGemini},
|
||||
"gemini-1.5-flash-latest": {[]float64{0.175, 0.265}, config.ChannelTypeGemini},
|
||||
"gemini-ultra": {[]float64{1, 1}, config.ChannelTypeGemini},
|
||||
|
||||
// ¥0.005 / 1k tokens
|
||||
"glm-3-turbo": {[]float64{0.3572, 0.3572}, common.ChannelTypeZhipu},
|
||||
"glm-3-turbo": {[]float64{0.3572, 0.3572}, config.ChannelTypeZhipu},
|
||||
// ¥0.1 / 1k tokens
|
||||
"glm-4": {[]float64{7.143, 7.143}, common.ChannelTypeZhipu},
|
||||
"glm-4v": {[]float64{7.143, 7.143}, common.ChannelTypeZhipu},
|
||||
"glm-4": {[]float64{7.143, 7.143}, config.ChannelTypeZhipu},
|
||||
"glm-4v": {[]float64{7.143, 7.143}, config.ChannelTypeZhipu},
|
||||
// ¥0.0005 / 1k tokens
|
||||
"embedding-2": {[]float64{0.0357, 0.0357}, common.ChannelTypeZhipu},
|
||||
"embedding-2": {[]float64{0.0357, 0.0357}, config.ChannelTypeZhipu},
|
||||
// ¥0.25 / 1张图片
|
||||
"cogview-3": {[]float64{17.8571, 17.8571}, common.ChannelTypeZhipu},
|
||||
"cogview-3": {[]float64{17.8571, 17.8571}, config.ChannelTypeZhipu},
|
||||
|
||||
// ¥0.008 / 1k tokens
|
||||
"qwen-turbo": {[]float64{0.5715, 0.5715}, common.ChannelTypeAli},
|
||||
"qwen-turbo": {[]float64{0.5715, 0.5715}, config.ChannelTypeAli},
|
||||
// ¥0.02 / 1k tokens
|
||||
"qwen-plus": {[]float64{1.4286, 1.4286}, common.ChannelTypeAli},
|
||||
"qwen-vl-max": {[]float64{1.4286, 1.4286}, common.ChannelTypeAli},
|
||||
"qwen-plus": {[]float64{1.4286, 1.4286}, config.ChannelTypeAli},
|
||||
"qwen-vl-max": {[]float64{1.4286, 1.4286}, config.ChannelTypeAli},
|
||||
// 0.12元/1,000tokens
|
||||
"qwen-max": {[]float64{8.5714, 8.5714}, common.ChannelTypeAli},
|
||||
"qwen-max-longcontext": {[]float64{8.5714, 8.5714}, common.ChannelTypeAli},
|
||||
"qwen-max": {[]float64{8.5714, 8.5714}, config.ChannelTypeAli},
|
||||
"qwen-max-longcontext": {[]float64{8.5714, 8.5714}, config.ChannelTypeAli},
|
||||
// 0.008元/1,000tokens
|
||||
"qwen-vl-plus": {[]float64{0.5715, 0.5715}, common.ChannelTypeAli},
|
||||
"qwen-vl-plus": {[]float64{0.5715, 0.5715}, config.ChannelTypeAli},
|
||||
// ¥0.0007 / 1k tokens
|
||||
"text-embedding-v1": {[]float64{0.05, 0.05}, common.ChannelTypeAli},
|
||||
"text-embedding-v1": {[]float64{0.05, 0.05}, config.ChannelTypeAli},
|
||||
|
||||
// ¥0.018 / 1k tokens
|
||||
"SparkDesk": {[]float64{1.2858, 1.2858}, common.ChannelTypeXunfei},
|
||||
"SparkDesk-v1.1": {[]float64{1.2858, 1.2858}, common.ChannelTypeXunfei},
|
||||
"SparkDesk-v2.1": {[]float64{1.2858, 1.2858}, common.ChannelTypeXunfei},
|
||||
"SparkDesk-v3.1": {[]float64{1.2858, 1.2858}, common.ChannelTypeXunfei},
|
||||
"SparkDesk-v3.5": {[]float64{1.2858, 1.2858}, common.ChannelTypeXunfei},
|
||||
"SparkDesk": {[]float64{1.2858, 1.2858}, config.ChannelTypeXunfei},
|
||||
"SparkDesk-v1.1": {[]float64{1.2858, 1.2858}, config.ChannelTypeXunfei},
|
||||
"SparkDesk-v2.1": {[]float64{1.2858, 1.2858}, config.ChannelTypeXunfei},
|
||||
"SparkDesk-v3.1": {[]float64{1.2858, 1.2858}, config.ChannelTypeXunfei},
|
||||
"SparkDesk-v3.5": {[]float64{1.2858, 1.2858}, config.ChannelTypeXunfei},
|
||||
|
||||
// ¥0.012 / 1k tokens
|
||||
"360GPT_S2_V9": {[]float64{0.8572, 0.8572}, common.ChannelType360},
|
||||
"360GPT_S2_V9": {[]float64{0.8572, 0.8572}, config.ChannelType360},
|
||||
// ¥0.001 / 1k tokens
|
||||
"embedding-bert-512-v1": {[]float64{0.0715, 0.0715}, common.ChannelType360},
|
||||
"embedding_s1_v1": {[]float64{0.0715, 0.0715}, common.ChannelType360},
|
||||
"semantic_similarity_s1_v1": {[]float64{0.0715, 0.0715}, common.ChannelType360},
|
||||
"embedding-bert-512-v1": {[]float64{0.0715, 0.0715}, config.ChannelType360},
|
||||
"embedding_s1_v1": {[]float64{0.0715, 0.0715}, config.ChannelType360},
|
||||
"semantic_similarity_s1_v1": {[]float64{0.0715, 0.0715}, config.ChannelType360},
|
||||
|
||||
// ¥0.1 / 1k tokens // https://cloud.tencent.com/document/product/1729/97731#e0e6be58-60c8-469f-bdeb-6c264ce3b4d0
|
||||
"hunyuan": {[]float64{7.143, 7.143}, common.ChannelTypeTencent},
|
||||
"hunyuan": {[]float64{7.143, 7.143}, config.ChannelTypeTencent},
|
||||
// https://cloud.tencent.com/document/product/1729/97731#e0e6be58-60c8-469f-bdeb-6c264ce3b4d0
|
||||
// ¥0.01 / 1k tokens
|
||||
"ChatStd": {[]float64{0.7143, 0.7143}, common.ChannelTypeTencent},
|
||||
"ChatStd": {[]float64{0.7143, 0.7143}, config.ChannelTypeTencent},
|
||||
//¥0.1 / 1k tokens
|
||||
"ChatPro": {[]float64{7.143, 7.143}, common.ChannelTypeTencent},
|
||||
"ChatPro": {[]float64{7.143, 7.143}, config.ChannelTypeTencent},
|
||||
|
||||
"Baichuan2-Turbo": {[]float64{0.5715, 0.5715}, common.ChannelTypeBaichuan}, // ¥0.008 / 1k tokens
|
||||
"Baichuan2-Turbo-192k": {[]float64{1.143, 1.143}, common.ChannelTypeBaichuan}, // ¥0.016 / 1k tokens
|
||||
"Baichuan2-53B": {[]float64{1.4286, 1.4286}, common.ChannelTypeBaichuan}, // ¥0.02 / 1k tokens
|
||||
"Baichuan-Text-Embedding": {[]float64{0.0357, 0.0357}, common.ChannelTypeBaichuan}, // ¥0.0005 / 1k tokens
|
||||
"Baichuan2-Turbo": {[]float64{0.5715, 0.5715}, config.ChannelTypeBaichuan}, // ¥0.008 / 1k tokens
|
||||
"Baichuan2-Turbo-192k": {[]float64{1.143, 1.143}, config.ChannelTypeBaichuan}, // ¥0.016 / 1k tokens
|
||||
"Baichuan2-53B": {[]float64{1.4286, 1.4286}, config.ChannelTypeBaichuan}, // ¥0.02 / 1k tokens
|
||||
"Baichuan-Text-Embedding": {[]float64{0.0357, 0.0357}, config.ChannelTypeBaichuan}, // ¥0.0005 / 1k tokens
|
||||
|
||||
"abab5.5s-chat": {[]float64{0.3572, 0.3572}, common.ChannelTypeMiniMax}, // ¥0.005 / 1k tokens
|
||||
"abab5.5-chat": {[]float64{1.0714, 1.0714}, common.ChannelTypeMiniMax}, // ¥0.015 / 1k tokens
|
||||
"abab6-chat": {[]float64{14.2857, 14.2857}, common.ChannelTypeMiniMax}, // ¥0.2 / 1k tokens
|
||||
"embo-01": {[]float64{0.0357, 0.0357}, common.ChannelTypeMiniMax}, // ¥0.0005 / 1k tokens
|
||||
"abab5.5s-chat": {[]float64{0.3572, 0.3572}, config.ChannelTypeMiniMax}, // ¥0.005 / 1k tokens
|
||||
"abab5.5-chat": {[]float64{1.0714, 1.0714}, config.ChannelTypeMiniMax}, // ¥0.015 / 1k tokens
|
||||
"abab6-chat": {[]float64{14.2857, 14.2857}, config.ChannelTypeMiniMax}, // ¥0.2 / 1k tokens
|
||||
"embo-01": {[]float64{0.0357, 0.0357}, config.ChannelTypeMiniMax}, // ¥0.0005 / 1k tokens
|
||||
|
||||
"deepseek-coder": {[]float64{0.75, 0.75}, common.ChannelTypeDeepseek}, // 暂定 $0.0015 / 1K tokens
|
||||
"deepseek-chat": {[]float64{0.75, 0.75}, common.ChannelTypeDeepseek}, // 暂定 $0.0015 / 1K tokens
|
||||
"deepseek-coder": {[]float64{0.75, 0.75}, config.ChannelTypeDeepseek}, // 暂定 $0.0015 / 1K tokens
|
||||
"deepseek-chat": {[]float64{0.75, 0.75}, config.ChannelTypeDeepseek}, // 暂定 $0.0015 / 1K tokens
|
||||
|
||||
"moonshot-v1-8k": {[]float64{0.8572, 0.8572}, common.ChannelTypeMoonshot}, // ¥0.012 / 1K tokens
|
||||
"moonshot-v1-32k": {[]float64{1.7143, 1.7143}, common.ChannelTypeMoonshot}, // ¥0.024 / 1K tokens
|
||||
"moonshot-v1-128k": {[]float64{4.2857, 4.2857}, common.ChannelTypeMoonshot}, // ¥0.06 / 1K tokens
|
||||
"moonshot-v1-8k": {[]float64{0.8572, 0.8572}, config.ChannelTypeMoonshot}, // ¥0.012 / 1K tokens
|
||||
"moonshot-v1-32k": {[]float64{1.7143, 1.7143}, config.ChannelTypeMoonshot}, // ¥0.024 / 1K tokens
|
||||
"moonshot-v1-128k": {[]float64{4.2857, 4.2857}, config.ChannelTypeMoonshot}, // ¥0.06 / 1K tokens
|
||||
|
||||
"open-mistral-7b": {[]float64{0.125, 0.125}, common.ChannelTypeMistral}, // 0.25$ / 1M tokens 0.25$ / 1M tokens 0.00025$ / 1k tokens
|
||||
"open-mixtral-8x7b": {[]float64{0.35, 0.35}, common.ChannelTypeMistral}, // 0.7$ / 1M tokens 0.7$ / 1M tokens 0.0007$ / 1k tokens
|
||||
"mistral-small-latest": {[]float64{1, 3}, common.ChannelTypeMistral}, // 2$ / 1M tokens 6$ / 1M tokens 0.002$ / 1k tokens
|
||||
"mistral-medium-latest": {[]float64{1.35, 4.05}, common.ChannelTypeMistral}, // 2.7$ / 1M tokens 8.1$ / 1M tokens 0.0027$ / 1k tokens
|
||||
"mistral-large-latest": {[]float64{4, 12}, common.ChannelTypeMistral}, // 8$ / 1M tokens 24$ / 1M tokens 0.008$ / 1k tokens
|
||||
"mistral-embed": {[]float64{0.05, 0.05}, common.ChannelTypeMistral}, // 0.1$ / 1M tokens 0.1$ / 1M tokens 0.0001$ / 1k tokens
|
||||
"open-mistral-7b": {[]float64{0.125, 0.125}, config.ChannelTypeMistral}, // 0.25$ / 1M tokens 0.25$ / 1M tokens 0.00025$ / 1k tokens
|
||||
"open-mixtral-8x7b": {[]float64{0.35, 0.35}, config.ChannelTypeMistral}, // 0.7$ / 1M tokens 0.7$ / 1M tokens 0.0007$ / 1k tokens
|
||||
"mistral-small-latest": {[]float64{1, 3}, config.ChannelTypeMistral}, // 2$ / 1M tokens 6$ / 1M tokens 0.002$ / 1k tokens
|
||||
"mistral-medium-latest": {[]float64{1.35, 4.05}, config.ChannelTypeMistral}, // 2.7$ / 1M tokens 8.1$ / 1M tokens 0.0027$ / 1k tokens
|
||||
"mistral-large-latest": {[]float64{4, 12}, config.ChannelTypeMistral}, // 8$ / 1M tokens 24$ / 1M tokens 0.008$ / 1k tokens
|
||||
"mistral-embed": {[]float64{0.05, 0.05}, config.ChannelTypeMistral}, // 0.1$ / 1M tokens 0.1$ / 1M tokens 0.0001$ / 1k tokens
|
||||
|
||||
// $0.70/$0.80 /1M Tokens 0.0007$ / 1k tokens
|
||||
"llama2-70b-4096": {[]float64{0.35, 0.4}, common.ChannelTypeGroq},
|
||||
"llama2-70b-4096": {[]float64{0.35, 0.4}, config.ChannelTypeGroq},
|
||||
// $0.10/$0.10 /1M Tokens 0.0001$ / 1k tokens
|
||||
"llama2-7b-2048": {[]float64{0.05, 0.05}, common.ChannelTypeGroq},
|
||||
"gemma-7b-it": {[]float64{0.05, 0.05}, common.ChannelTypeGroq},
|
||||
"llama2-7b-2048": {[]float64{0.05, 0.05}, config.ChannelTypeGroq},
|
||||
"gemma-7b-it": {[]float64{0.05, 0.05}, config.ChannelTypeGroq},
|
||||
// $0.27/$0.27 /1M Tokens 0.00027$ / 1k tokens
|
||||
"mixtral-8x7b-32768": {[]float64{0.135, 0.135}, common.ChannelTypeGroq},
|
||||
"mixtral-8x7b-32768": {[]float64{0.135, 0.135}, config.ChannelTypeGroq},
|
||||
|
||||
// 2.5 元 / 1M tokens 0.0025 / 1k tokens
|
||||
"yi-34b-chat-0205": {[]float64{0.1786, 0.1786}, common.ChannelTypeLingyi},
|
||||
"yi-34b-chat-0205": {[]float64{0.1786, 0.1786}, config.ChannelTypeLingyi},
|
||||
// 12 元 / 1M tokens 0.012 / 1k tokens
|
||||
"yi-34b-chat-200k": {[]float64{0.8571, 0.8571}, common.ChannelTypeLingyi},
|
||||
"yi-34b-chat-200k": {[]float64{0.8571, 0.8571}, config.ChannelTypeLingyi},
|
||||
// 6 元 / 1M tokens 0.006 / 1k tokens
|
||||
"yi-vl-plus": {[]float64{0.4286, 0.4286}, common.ChannelTypeLingyi},
|
||||
"yi-vl-plus": {[]float64{0.4286, 0.4286}, config.ChannelTypeLingyi},
|
||||
|
||||
"@cf/stabilityai/stable-diffusion-xl-base-1.0": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@cf/lykon/dreamshaper-8-lcm": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@cf/bytedance/stable-diffusion-xl-lightning": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@cf/qwen/qwen1.5-7b-chat-awq": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@cf/qwen/qwen1.5-14b-chat-awq": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@hf/thebloke/deepseek-coder-6.7b-base-awq": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@hf/google/gemma-7b-it": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@hf/thebloke/llama-2-13b-chat-awq": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@cf/openai/whisper": {[]float64{0, 0}, common.ChannelTypeCloudflareAI},
|
||||
"@cf/stabilityai/stable-diffusion-xl-base-1.0": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
"@cf/lykon/dreamshaper-8-lcm": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
"@cf/bytedance/stable-diffusion-xl-lightning": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
"@cf/qwen/qwen1.5-7b-chat-awq": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
"@cf/qwen/qwen1.5-14b-chat-awq": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
"@hf/thebloke/deepseek-coder-6.7b-base-awq": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
"@hf/google/gemma-7b-it": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
"@hf/thebloke/llama-2-13b-chat-awq": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
"@cf/openai/whisper": {[]float64{0, 0}, config.ChannelTypeCloudflareAI},
|
||||
//$0.50 /1M TOKENS $1.50/1M TOKENS
|
||||
"command-r": {[]float64{0.25, 0.75}, common.ChannelTypeCohere},
|
||||
"command-r": {[]float64{0.25, 0.75}, config.ChannelTypeCohere},
|
||||
//$3 /1M TOKENS $15/1M TOKENS
|
||||
"command-r-plus": {[]float64{1.5, 7.5}, common.ChannelTypeCohere},
|
||||
"command-r-plus": {[]float64{1.5, 7.5}, config.ChannelTypeCohere},
|
||||
|
||||
// 0.065
|
||||
"sd3": {[]float64{32.5, 32.5}, common.ChannelTypeStabilityAI},
|
||||
"sd3": {[]float64{32.5, 32.5}, config.ChannelTypeStabilityAI},
|
||||
// 0.04
|
||||
"sd3-turbo": {[]float64{20, 20}, common.ChannelTypeStabilityAI},
|
||||
"sd3-turbo": {[]float64{20, 20}, config.ChannelTypeStabilityAI},
|
||||
// 0.03
|
||||
"stable-image-core": {[]float64{15, 15}, common.ChannelTypeStabilityAI},
|
||||
"stable-image-core": {[]float64{15, 15}, config.ChannelTypeStabilityAI},
|
||||
|
||||
// hunyuan
|
||||
"hunyuan-lite": {[]float64{0, 0}, common.ChannelTypeHunyuan},
|
||||
"hunyuan-standard": {[]float64{0.3214, 0.3571}, common.ChannelTypeHunyuan},
|
||||
"hunyuan-standard-256k": {[]float64{1.0714, 4.2857}, common.ChannelTypeHunyuan},
|
||||
"hunyuan-pro": {[]float64{2.1429, 7.1429}, common.ChannelTypeHunyuan},
|
||||
"hunyuan-lite": {[]float64{0, 0}, config.ChannelTypeHunyuan},
|
||||
"hunyuan-standard": {[]float64{0.3214, 0.3571}, config.ChannelTypeHunyuan},
|
||||
"hunyuan-standard-256k": {[]float64{1.0714, 4.2857}, config.ChannelTypeHunyuan},
|
||||
"hunyuan-pro": {[]float64{2.1429, 7.1429}, config.ChannelTypeHunyuan},
|
||||
}
|
||||
|
||||
var prices []*Price
|
||||
@@ -355,7 +355,7 @@ func GetDefaultPrice() []*Price {
|
||||
prices = append(prices, &Price{
|
||||
Model: model,
|
||||
Type: TimesPriceType,
|
||||
ChannelType: common.ChannelTypeMidjourney,
|
||||
ChannelType: config.ChannelTypeMidjourney,
|
||||
Input: mjPrice,
|
||||
Output: mjPrice,
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/utils"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -69,7 +70,7 @@ func Redeem(key string, userId int) (quota int, err error) {
|
||||
if err != nil {
|
||||
return errors.New("无效的兑换码")
|
||||
}
|
||||
if redemption.Status != common.RedemptionCodeStatusEnabled {
|
||||
if redemption.Status != config.RedemptionCodeStatusEnabled {
|
||||
return errors.New("该兑换码已被使用")
|
||||
}
|
||||
err = tx.Model(&User{}).Where("id = ?", userId).Update("quota", gorm.Expr("quota + ?", redemption.Quota)).Error
|
||||
@@ -77,7 +78,7 @@ func Redeem(key string, userId int) (quota int, err error) {
|
||||
return err
|
||||
}
|
||||
redemption.RedeemedTime = utils.GetTimestamp()
|
||||
redemption.Status = common.RedemptionCodeStatusUsed
|
||||
redemption.Status = config.RedemptionCodeStatusUsed
|
||||
err = tx.Save(redemption).Error
|
||||
return err
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"one-api/common/stmp"
|
||||
"one-api/common/utils"
|
||||
@@ -49,7 +50,7 @@ func GetUserTokensList(userId int, params *GenericParams) (*DataResult[Token], e
|
||||
|
||||
// 获取状态为可用的令牌
|
||||
func GetUserEnabledTokens(userId int) (tokens []*Token, err error) {
|
||||
err = DB.Where("user_id = ? and status = ?", userId, common.TokenStatusEnabled).Find(&tokens).Error
|
||||
err = DB.Where("user_id = ? and status = ?", userId, config.TokenStatusEnabled).Find(&tokens).Error
|
||||
return tokens, err
|
||||
}
|
||||
|
||||
@@ -65,17 +66,17 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
}
|
||||
return nil, errors.New("令牌验证失败")
|
||||
}
|
||||
if token.Status == common.TokenStatusExhausted {
|
||||
if token.Status == config.TokenStatusExhausted {
|
||||
return nil, errors.New("该令牌额度已用尽")
|
||||
} else if token.Status == common.TokenStatusExpired {
|
||||
} else if token.Status == config.TokenStatusExpired {
|
||||
return nil, errors.New("该令牌已过期")
|
||||
}
|
||||
if token.Status != common.TokenStatusEnabled {
|
||||
if token.Status != config.TokenStatusEnabled {
|
||||
return nil, errors.New("该令牌状态不可用")
|
||||
}
|
||||
if token.ExpiredTime != -1 && token.ExpiredTime < utils.GetTimestamp() {
|
||||
if !common.RedisEnabled {
|
||||
token.Status = common.TokenStatusExpired
|
||||
token.Status = config.TokenStatusExpired
|
||||
err := token.SelectUpdate()
|
||||
if err != nil {
|
||||
logger.SysError("failed to update token status" + err.Error())
|
||||
@@ -86,7 +87,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
if !token.UnlimitedQuota && token.RemainQuota <= 0 {
|
||||
if !common.RedisEnabled {
|
||||
// in this case, we can make sure the token is exhausted
|
||||
token.Status = common.TokenStatusExhausted
|
||||
token.Status = config.TokenStatusExhausted
|
||||
err := token.SelectUpdate()
|
||||
if err != nil {
|
||||
logger.SysError("failed to update token status" + err.Error())
|
||||
@@ -128,7 +129,7 @@ func GetTokenByName(name string, user_id int) (*Token, error) {
|
||||
}
|
||||
|
||||
func (token *Token) Insert() error {
|
||||
if token.ChatCache && !common.ChatCacheEnabled {
|
||||
if token.ChatCache && !config.ChatCacheEnabled {
|
||||
token.ChatCache = false
|
||||
}
|
||||
|
||||
@@ -138,7 +139,7 @@ func (token *Token) Insert() error {
|
||||
|
||||
// Update Make sure your token's fields is completed, because this will update non-zero values
|
||||
func (token *Token) Update() error {
|
||||
if token.ChatCache && !common.ChatCacheEnabled {
|
||||
if token.ChatCache && !config.ChatCacheEnabled {
|
||||
token.ChatCache = false
|
||||
}
|
||||
|
||||
@@ -178,7 +179,7 @@ func IncreaseTokenQuota(id int, quota int) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
}
|
||||
if common.BatchUpdateEnabled {
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeTokenQuota, id, quota)
|
||||
return nil
|
||||
}
|
||||
@@ -200,7 +201,7 @@ func DecreaseTokenQuota(id int, quota int) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
}
|
||||
if common.BatchUpdateEnabled {
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeTokenQuota, id, -quota)
|
||||
return nil
|
||||
}
|
||||
@@ -236,7 +237,7 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
|
||||
if userQuota < quota {
|
||||
return errors.New("用户额度不足")
|
||||
}
|
||||
quotaTooLow := userQuota >= common.QuotaRemindThreshold && userQuota-quota < common.QuotaRemindThreshold
|
||||
quotaTooLow := userQuota >= config.QuotaRemindThreshold && userQuota-quota < config.QuotaRemindThreshold
|
||||
noMoreQuota := userQuota-quota <= 0
|
||||
if quotaTooLow || noMoreQuota {
|
||||
go sendQuotaWarningEmail(token.UserId, userQuota, noMoreQuota)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"one-api/common/utils"
|
||||
"strings"
|
||||
@@ -116,7 +117,7 @@ func (user *User) Insert(inviterId int) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
user.Quota = common.QuotaForNewUser
|
||||
user.Quota = config.QuotaForNewUser
|
||||
user.AccessToken = utils.GetUUID()
|
||||
user.AffCode = utils.GetRandomString(4)
|
||||
user.CreatedTime = utils.GetTimestamp()
|
||||
@@ -124,17 +125,17 @@ func (user *User) Insert(inviterId int) error {
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if common.QuotaForNewUser > 0 {
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("新用户注册赠送 %s", common.LogQuota(common.QuotaForNewUser)))
|
||||
if config.QuotaForNewUser > 0 {
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("新用户注册赠送 %s", common.LogQuota(config.QuotaForNewUser)))
|
||||
}
|
||||
if inviterId != 0 {
|
||||
if common.QuotaForInvitee > 0 {
|
||||
_ = IncreaseUserQuota(user.Id, common.QuotaForInvitee)
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("使用邀请码赠送 %s", common.LogQuota(common.QuotaForInvitee)))
|
||||
if config.QuotaForInvitee > 0 {
|
||||
_ = IncreaseUserQuota(user.Id, config.QuotaForInvitee)
|
||||
RecordLog(user.Id, LogTypeSystem, fmt.Sprintf("使用邀请码赠送 %s", common.LogQuota(config.QuotaForInvitee)))
|
||||
}
|
||||
if common.QuotaForInviter > 0 {
|
||||
_ = IncreaseUserQuota(inviterId, common.QuotaForInviter)
|
||||
RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("邀请用户赠送 %s", common.LogQuota(common.QuotaForInviter)))
|
||||
if config.QuotaForInviter > 0 {
|
||||
_ = IncreaseUserQuota(inviterId, config.QuotaForInviter)
|
||||
RecordLog(inviterId, LogTypeSystem, fmt.Sprintf("邀请用户赠送 %s", common.LogQuota(config.QuotaForInviter)))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -150,8 +151,8 @@ func (user *User) Update(updatePassword bool) error {
|
||||
}
|
||||
err = DB.Model(user).Updates(user).Error
|
||||
|
||||
if err == nil && user.Role == common.RoleRootUser {
|
||||
common.RootUserEmail = user.Email
|
||||
if err == nil && user.Role == config.RoleRootUser {
|
||||
config.RootUserEmail = user.Email
|
||||
}
|
||||
|
||||
return err
|
||||
@@ -196,7 +197,7 @@ func (user *User) ValidateAndFill() (err error) {
|
||||
}
|
||||
}
|
||||
okay := common.ValidatePasswordAndHash(password, user.Password)
|
||||
if !okay || user.Status != common.UserStatusEnabled {
|
||||
if !okay || user.Status != config.UserStatusEnabled {
|
||||
return errors.New("用户名或密码错误,或用户已被封禁")
|
||||
}
|
||||
return nil
|
||||
@@ -310,7 +311,7 @@ func IsAdmin(userId int) bool {
|
||||
logger.SysError("no such user " + err.Error())
|
||||
return false
|
||||
}
|
||||
return user.Role >= common.RoleAdminUser
|
||||
return user.Role >= config.RoleAdminUser
|
||||
}
|
||||
|
||||
func IsUserEnabled(userId int) (bool, error) {
|
||||
@@ -322,7 +323,7 @@ func IsUserEnabled(userId int) (bool, error) {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return user.Status == common.UserStatusEnabled, nil
|
||||
return user.Status == config.UserStatusEnabled, nil
|
||||
}
|
||||
|
||||
func ValidateAccessToken(token string) (user *User) {
|
||||
@@ -366,7 +367,7 @@ func IncreaseUserQuota(id int, quota int) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
}
|
||||
if common.BatchUpdateEnabled {
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeUserQuota, id, quota)
|
||||
return nil
|
||||
}
|
||||
@@ -382,7 +383,7 @@ func DecreaseUserQuota(id int, quota int) (err error) {
|
||||
if quota < 0 {
|
||||
return errors.New("quota 不能为负数!")
|
||||
}
|
||||
if common.BatchUpdateEnabled {
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeUserQuota, id, -quota)
|
||||
return nil
|
||||
}
|
||||
@@ -395,12 +396,12 @@ func decreaseUserQuota(id int, quota int) (err error) {
|
||||
}
|
||||
|
||||
func GetRootUserEmail() (email string) {
|
||||
DB.Model(&User{}).Where("role = ?", common.RoleRootUser).Select("email").Find(&email)
|
||||
DB.Model(&User{}).Where("role = ?", config.RoleRootUser).Select("email").Find(&email)
|
||||
return email
|
||||
}
|
||||
|
||||
func UpdateUserUsedQuotaAndRequestCount(id int, quota int) {
|
||||
if common.BatchUpdateEnabled {
|
||||
if config.BatchUpdateEnabled {
|
||||
addNewRecord(BatchUpdateTypeUsedQuota, id, quota)
|
||||
addNewRecord(BatchUpdateTypeRequestCount, id, 1)
|
||||
return
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/common/config"
|
||||
"one-api/common/logger"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -29,7 +29,7 @@ func init() {
|
||||
func InitBatchUpdater() {
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Duration(common.BatchUpdateInterval) * time.Second)
|
||||
time.Sleep(time.Duration(config.BatchUpdateInterval) * time.Second)
|
||||
batchUpdate()
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user