2025-02-20 16:41:46 +08:00
package helper
import (
2025-02-28 19:17:15 +08:00
"fmt"
2025-02-20 16:41:46 +08:00
"github.com/gin-gonic/gin"
"one-api/common"
relaycommon "one-api/relay/common"
"one-api/setting"
2025-03-08 01:30:50 +08:00
"one-api/setting/operation_setting"
2025-02-20 16:41:46 +08:00
)
type PriceData struct {
ModelPrice float64
ModelRatio float64
2025-03-02 15:47:12 +08:00
CompletionRatio float64
2025-03-08 01:30:50 +08:00
CacheRatio float64
2025-02-20 16:41:46 +08:00
GroupRatio float64
UsePrice bool
2025-03-12 21:31:46 +08:00
CacheCreationRatio float64
2025-02-20 16:41:46 +08:00
ShouldPreConsumedQuota int
}
2025-02-28 19:17:15 +08:00
func ModelPriceHelper ( c * gin . Context , info * relaycommon . RelayInfo , promptTokens int , maxTokens int ) ( PriceData , error ) {
2025-03-08 01:30:50 +08:00
modelPrice , usePrice := operation_setting . GetModelPrice ( info . OriginModelName , false )
2025-02-20 16:41:46 +08:00
groupRatio := setting . GetGroupRatio ( info . Group )
var preConsumedQuota int
var modelRatio float64
2025-03-02 15:47:12 +08:00
var completionRatio float64
2025-03-08 01:30:50 +08:00
var cacheRatio float64
2025-03-12 21:31:46 +08:00
var cacheCreationRatio float64
2025-02-20 16:41:46 +08:00
if ! usePrice {
preConsumedTokens := common . PreConsumedQuota
if maxTokens != 0 {
preConsumedTokens = promptTokens + maxTokens
}
2025-02-28 20:28:44 +08:00
var success bool
2025-03-08 01:30:50 +08:00
modelRatio , success = operation_setting . GetModelRatio ( info . OriginModelName )
2025-02-28 19:17:15 +08:00
if ! success {
2025-03-01 21:13:48 +08:00
if info . UserId == 1 {
return PriceData { } , fmt . Errorf ( "模型 %s 倍率或价格未配置, 请设置或开始自用模式; Model %s ratio or price not set, please set or start self-use mode" , info . OriginModelName , info . OriginModelName )
} else {
return PriceData { } , fmt . Errorf ( "模型 %s 倍率或价格未配置, 请联系管理员设置; Model %s ratio or price not set, please contact administrator to set" , info . OriginModelName , info . OriginModelName )
}
2025-02-28 19:17:15 +08:00
}
2025-03-08 01:30:50 +08:00
completionRatio = operation_setting . GetCompletionRatio ( info . OriginModelName )
cacheRatio , _ = operation_setting . GetCacheRatio ( info . OriginModelName )
2025-03-12 21:31:46 +08:00
cacheCreationRatio , _ = operation_setting . GetCreateCacheRatio ( info . OriginModelName )
2025-02-20 16:41:46 +08:00
ratio := modelRatio * groupRatio
preConsumedQuota = int ( float64 ( preConsumedTokens ) * ratio )
} else {
preConsumedQuota = int ( modelPrice * common . QuotaPerUnit * groupRatio )
}
return PriceData {
ModelPrice : modelPrice ,
ModelRatio : modelRatio ,
2025-03-02 15:47:12 +08:00
CompletionRatio : completionRatio ,
2025-02-20 16:41:46 +08:00
GroupRatio : groupRatio ,
UsePrice : usePrice ,
2025-03-08 01:30:50 +08:00
CacheRatio : cacheRatio ,
2025-03-12 21:31:46 +08:00
CacheCreationRatio : cacheCreationRatio ,
2025-02-20 16:41:46 +08:00
ShouldPreConsumedQuota : preConsumedQuota ,
2025-02-28 19:17:15 +08:00
} , nil
2025-02-20 16:41:46 +08:00
}