refactor(price): improve handling of other ratios in PriceData

This commit is contained in:
CaIon
2026-07-07 21:22:19 +08:00
parent 394b023dbf
commit fc1259f583
8 changed files with 256 additions and 56 deletions
+18 -10
View File
@@ -11,6 +11,7 @@ import (
"github.com/QuantumNous/new-api/model"
relaycommon "github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
)
@@ -23,9 +24,9 @@ func LogTaskConsumption(c *gin.Context, info *relaycommon.RelayInfo) {
if common.StringsContains(constant.TaskPricePatches, info.OriginModelName) {
logContent = fmt.Sprintf("%s,按次计费", logContent)
} else {
if len(info.PriceData.OtherRatios) > 0 {
if otherRatios := info.PriceData.OtherRatios(); len(otherRatios) > 0 {
var contents []string
for key, ra := range info.PriceData.OtherRatios {
for key, ra := range otherRatios {
if 1.0 != ra {
contents = append(contents, fmt.Sprintf("%s: %.2f", key, ra))
}
@@ -126,8 +127,8 @@ func taskBillingOther(task *model.Task) map[string]interface{} {
other["model_ratio"] = bc.ModelRatio
}
other["group_ratio"] = bc.GroupRatio
if len(bc.OtherRatios) > 0 {
for k, v := range bc.OtherRatios {
if priceData := taskBillingContextPriceData(bc); priceData != nil {
for k, v := range priceData.OtherRatios() {
other[k] = v
}
}
@@ -140,6 +141,17 @@ func taskBillingOther(task *model.Task) map[string]interface{} {
return other
}
func taskBillingContextPriceData(bc *model.TaskBillingContext) *types.PriceData {
if bc == nil || len(bc.OtherRatios) == 0 {
return nil
}
priceData := &types.PriceData{}
if !priceData.ReplaceOtherRatios(bc.OtherRatios) {
return nil
}
return priceData
}
// taskModelName 从 BillingContext 或 Properties 中获取模型名称。
func taskModelName(task *model.Task) string {
if bc := task.PrivateData.BillingContext; bc != nil && bc.OriginModelName != "" {
@@ -294,12 +306,8 @@ func RecalculateTaskQuotaByTokens(ctx context.Context, task *model.Task, totalTo
// 计算 OtherRatios 乘积(视频折扣、时长等)
otherMultiplier := 1.0
if bc := task.PrivateData.BillingContext; bc != nil {
for _, r := range bc.OtherRatios {
if r != 1.0 && r > 0 {
otherMultiplier *= r
}
}
if priceData := taskBillingContextPriceData(task.PrivateData.BillingContext); priceData != nil {
otherMultiplier = priceData.OtherRatioMultiplier()
}
// 计算实际应扣费额度: totalTokens * modelRatio * groupRatio * otherMultiplier(饱和转换,防止溢出成负数)