mirror of
https://github.com/linux-do/new-api.git
synced 2025-11-13 09:33:43 +08:00
feat: add pricing page
This commit is contained in:
@@ -29,6 +29,13 @@ func GetGroupModels(group string) []string {
|
||||
return models
|
||||
}
|
||||
|
||||
func GetEnabledModels() []string {
|
||||
var models []string
|
||||
// Find distinct models
|
||||
DB.Table("abilities").Where("enabled = ?", true).Distinct("model").Pluck("model", &models)
|
||||
return models
|
||||
}
|
||||
|
||||
func getPriority(group string, model string, retry int) (int, error) {
|
||||
groupCol := "`group`"
|
||||
trueVal := "1"
|
||||
|
||||
72
model/pricing.go
Normal file
72
model/pricing.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/dto"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
pricingMap []dto.ModelPricing
|
||||
lastGetPricingTime time.Time
|
||||
updatePricingLock sync.Mutex
|
||||
)
|
||||
|
||||
func GetPricing(user *User, openAIModels []dto.OpenAIModels) []dto.ModelPricing {
|
||||
updatePricingLock.Lock()
|
||||
defer updatePricingLock.Unlock()
|
||||
|
||||
if time.Since(lastGetPricingTime) > time.Minute*1 || len(pricingMap) == 0 {
|
||||
updatePricing(openAIModels)
|
||||
}
|
||||
if user != nil {
|
||||
userPricingMap := make([]dto.ModelPricing, 0)
|
||||
models := GetGroupModels(user.Group)
|
||||
for _, pricing := range pricingMap {
|
||||
if !common.StringsContains(models, pricing.ModelName) {
|
||||
pricing.Available = false
|
||||
}
|
||||
userPricingMap = append(userPricingMap, pricing)
|
||||
}
|
||||
return userPricingMap
|
||||
}
|
||||
return pricingMap
|
||||
}
|
||||
|
||||
func updatePricing(openAIModels []dto.OpenAIModels) {
|
||||
modelRatios := common.GetModelRatios()
|
||||
enabledModels := GetEnabledModels()
|
||||
allModels := make(map[string]string)
|
||||
for _, openAIModel := range openAIModels {
|
||||
if common.StringsContains(enabledModels, openAIModel.Id) {
|
||||
allModels[openAIModel.Id] = openAIModel.OwnedBy
|
||||
}
|
||||
}
|
||||
for model, _ := range modelRatios {
|
||||
if common.StringsContains(enabledModels, model) {
|
||||
if _, ok := allModels[model]; !ok {
|
||||
allModels[model] = "custom"
|
||||
}
|
||||
}
|
||||
}
|
||||
pricingMap = make([]dto.ModelPricing, 0)
|
||||
for model, ownerBy := range allModels {
|
||||
pricing := dto.ModelPricing{
|
||||
Available: true,
|
||||
ModelName: model,
|
||||
OwnerBy: ownerBy,
|
||||
}
|
||||
modelPrice, findPrice := common.GetModelPrice(model, false)
|
||||
if findPrice {
|
||||
pricing.ModelPrice = modelPrice
|
||||
pricing.QuotaType = 1
|
||||
} else {
|
||||
pricing.ModelRatio = common.GetModelRatio(model)
|
||||
pricing.CompletionRatio = common.GetCompletionRatio(model)
|
||||
pricing.QuotaType = 0
|
||||
}
|
||||
pricingMap = append(pricingMap, pricing)
|
||||
}
|
||||
lastGetPricingTime = time.Now()
|
||||
}
|
||||
@@ -45,6 +45,7 @@ func logQuotaDataCache(userId int, username string, modelName string, quota int,
|
||||
if ok {
|
||||
quotaData.Count += 1
|
||||
quotaData.Quota += quota
|
||||
quotaData.TokenUsed += tokenUsed
|
||||
} else {
|
||||
quotaData = &QuotaData{
|
||||
UserID: userId,
|
||||
|
||||
Reference in New Issue
Block a user