feat: extend ModelHasConfiguredPricing to include channel-level pricing (ChannelModelRatio/ChannelModelPrice) via prebuilt index set

This commit is contained in:
xiezhouwei 2026-06-23 10:51:46 +08:00
parent a7759c5767
commit 4a7cb9e0f0
2 changed files with 40 additions and 4 deletions

View File

@ -198,7 +198,11 @@ func ChannelModelPrice2JSONString() string {
} }
func UpdateChannelModelPriceByJSONString(jsonStr string) error { func UpdateChannelModelPriceByJSONString(jsonStr string) error {
return types.LoadFromJsonString(channelModelPriceMap, jsonStr) err := types.LoadFromJsonString(channelModelPriceMap, jsonStr)
if err == nil {
rebuildChannelPricedModelSet()
}
return err
} }
func GetChannelModelPriceCopy() map[string]map[string]float64 { func GetChannelModelPriceCopy() map[string]map[string]float64 {
@ -227,7 +231,11 @@ func ChannelModelRatio2JSONString() string {
} }
func UpdateChannelModelRatioByJSONString(jsonStr string) error { func UpdateChannelModelRatioByJSONString(jsonStr string) error {
return types.LoadFromJsonString(channelModelRatioMap, jsonStr) err := types.LoadFromJsonString(channelModelRatioMap, jsonStr)
if err == nil {
rebuildChannelPricedModelSet()
}
return err
} }
func GetChannelModelRatioCopy() map[string]map[string]float64 { func GetChannelModelRatioCopy() map[string]map[string]float64 {

View File

@ -2,6 +2,7 @@ package ratio_setting
import ( import (
"strings" "strings"
"sync"
"github.com/QuantumNous/new-api/common" "github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/setting/operation_setting" "github.com/QuantumNous/new-api/setting/operation_setting"
@ -342,6 +343,11 @@ var modelPriceMap = types.NewRWMap[string, float64]()
var modelRatioMap = types.NewRWMap[string, float64]() var modelRatioMap = types.NewRWMap[string, float64]()
var completionRatioMap = types.NewRWMap[string, float64]() var completionRatioMap = types.NewRWMap[string, float64]()
// channelPricedModelSet 缓存所有在渠道级定价ChannelModelRatio / ChannelModelPrice中配置了价格的模型名已 FormatMatchingModelName
// 用于 ModelHasConfiguredPricing 快速判断,避免每次遍历 channelModelRatioMap / channelModelPriceMap。
var channelPricedModelSetMu sync.RWMutex
var channelPricedModelSet = make(map[string]bool)
var defaultCompletionRatio = map[string]float64{ var defaultCompletionRatio = map[string]float64{
"gpt-4-gizmo-*": 2, "gpt-4-gizmo-*": 2,
"gpt-4o-gizmo-*": 3, "gpt-4o-gizmo-*": 3,
@ -365,6 +371,25 @@ func InitRatioSettings() {
imagePriceMap.AddAll(defaultImagePrice) imagePriceMap.AddAll(defaultImagePrice)
} }
// rebuildChannelPricedModelSet 从 channelModelRatioMap 和 channelModelPriceMap 重建 channelPricedModelSet。
// 在渠道级定价数据变更DB Option 同步 / 管理后台更新)后调用,确保 ModelHasConfiguredPricing 能感知渠道级定价。
func rebuildChannelPricedModelSet() {
set := make(map[string]bool)
for _, ratios := range channelModelRatioMap.ReadAll() {
for model := range ratios {
set[FormatMatchingModelName(model)] = true
}
}
for _, prices := range channelModelPriceMap.ReadAll() {
for model := range prices {
set[FormatMatchingModelName(model)] = true
}
}
channelPricedModelSetMu.Lock()
channelPricedModelSet = set
channelPricedModelSetMu.Unlock()
}
func GetModelPriceMap() map[string]float64 { func GetModelPriceMap() map[string]float64 {
return modelPriceMap.ReadAll() return modelPriceMap.ReadAll()
} }
@ -433,7 +458,7 @@ func GetModelRatio(name string) (float64, bool, string) {
return ratio, true, name return ratio, true, name
} }
// ModelHasConfiguredPricing 表示模型在价格表或倍率表中存在显式配置(含 compact 通配)。 // ModelHasConfiguredPricing 表示模型在价格表或倍率表中存在显式配置(含 compact 通配、渠道级定价)。
// 未命中表键时 GetModelRatio 不再提供可用倍率(非自用为 success=false自用为占位倍率此类模型不应出现在定价接口。 // 未命中表键时 GetModelRatio 不再提供可用倍率(非自用为 success=false自用为占位倍率此类模型不应出现在定价接口。
func ModelHasConfiguredPricing(model string) bool { func ModelHasConfiguredPricing(model string) bool {
if _, ok := GetModelPrice(model, false); ok { if _, ok := GetModelPrice(model, false); ok {
@ -448,7 +473,10 @@ func ModelHasConfiguredPricing(model string) bool {
return true return true
} }
} }
return false channelPricedModelSetMu.RLock()
ok := channelPricedModelSet[name]
channelPricedModelSetMu.RUnlock()
return ok
} }
func DefaultModelRatio2JSONString() string { func DefaultModelRatio2JSONString() string {