mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-12-06 00:03:11 +00:00
81 lines
2.2 KiB
Go
81 lines
2.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/QuantumNous/new-api/common"
|
|
"github.com/QuantumNous/new-api/constant"
|
|
"github.com/QuantumNous/new-api/model"
|
|
"github.com/QuantumNous/new-api/service"
|
|
"github.com/QuantumNous/new-api/setting/ratio_setting"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func filterPricingByUsableGroups(pricing []model.Pricing, usableGroup map[string]string) []model.Pricing {
|
|
if len(pricing) == 0 {
|
|
return pricing
|
|
}
|
|
if len(usableGroup) == 0 {
|
|
return []model.Pricing{}
|
|
}
|
|
|
|
filtered := make([]model.Pricing, 0, len(pricing))
|
|
for _, item := range pricing {
|
|
// The pricing cache is shared by all viewers.
|
|
groups := make([]string, 0, len(item.EnableGroup))
|
|
for _, group := range item.EnableGroup {
|
|
if _, ok := usableGroup[group]; ok || group == "all" {
|
|
groups = append(groups, group)
|
|
}
|
|
}
|
|
if len(groups) > 0 {
|
|
item.EnableGroup = groups
|
|
filtered = append(filtered, item)
|
|
}
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
func GetPricing(c *gin.Context) {
|
|
userGroup := common.GetContextKeyString(c, constant.ContextKeyUserGroup)
|
|
usableGroup := service.GetUserUsableGroups(userGroup)
|
|
pricing := filterPricingByUsableGroups(model.GetPricing(), usableGroup)
|
|
groupRatio := ratio_setting.GetGroupRatioCopy()
|
|
for group := range groupRatio {
|
|
if _, ok := usableGroup[group]; !ok {
|
|
delete(groupRatio, group)
|
|
continue
|
|
}
|
|
if userGroup != "" {
|
|
if ratio, ok := ratio_setting.GetGroupGroupRatio(userGroup, group); ok {
|
|
groupRatio[group] = ratio
|
|
}
|
|
}
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
|
"success": true,
|
|
"data": pricing,
|
|
"vendors": model.GetVendors(),
|
|
"group_ratio": groupRatio,
|
|
"usable_group": usableGroup,
|
|
"supported_endpoint": model.GetSupportedEndpointMap(),
|
|
"auto_groups": service.GetUserAutoGroup(userGroup),
|
|
"pricing_version": "a42d372ccf0b5dd13ecf71203521f9d2",
|
|
})
|
|
}
|
|
|
|
func ResetModelRatio(c *gin.Context) {
|
|
defaultStr := ratio_setting.DefaultModelRatio2JSONString()
|
|
err := model.UpdateOption("ModelRatio", defaultStr)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
err = ratio_setting.UpdateModelRatioByJSONString(defaultStr)
|
|
if err != nil {
|
|
common.ApiError(c, err)
|
|
return
|
|
}
|
|
common.ApiSuccessT(c, "Model ratios reset", nil)
|
|
}
|