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"
2025-04-03 17:32:48 +08:00
constant2 "one-api/constant"
2025-02-20 16:41:46 +08:00
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-04-24 19:25:08 +08:00
CacheCreationRatio float64
ImageRatio float64
2025-02-20 16:41:46 +08:00
GroupRatio float64
UsePrice bool
ShouldPreConsumedQuota int
}
2025-03-17 17:52:54 +08:00
func ( p PriceData ) ToSetting ( ) string {
2025-05-09 17:11:25 +08:00
return fmt . Sprintf ( "ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f, ShouldPreConsumedQuota: %d, ImageRatio: %f" , p . ModelPrice , p . ModelRatio , p . CompletionRatio , p . CacheRatio , p . GroupRatio , p . UsePrice , p . CacheCreationRatio , p . ShouldPreConsumedQuota , p . ImageRatio )
2025-03-17 17:52:54 +08:00
}
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-04-24 19:25:08 +08:00
var imageRatio 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-04-03 17:32:48 +08:00
acceptUnsetRatio := false
if accept , ok := info . UserSetting [ constant2 . UserAcceptUnsetRatioModel ] ; ok {
b , ok := accept . ( bool )
if ok {
acceptUnsetRatio = b
}
}
if ! acceptUnsetRatio {
2025-04-18 19:36:18 +08:00
return PriceData { } , fmt . Errorf ( "模型 %s 倍率或价格未配置, 请联系管理员设置或开始自用模式; Model %s ratio or price not set, please set or start self-use mode" , info . OriginModelName , info . OriginModelName )
2025-03-01 21:13:48 +08:00
}
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-04-24 19:25:08 +08:00
imageRatio , _ = operation_setting . GetImageRatio ( 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 )
}
2025-03-17 17:52:54 +08:00
priceData := PriceData {
2025-02-20 16:41:46 +08:00
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-04-24 19:25:08 +08:00
ImageRatio : imageRatio ,
2025-03-12 21:31:46 +08:00
CacheCreationRatio : cacheCreationRatio ,
2025-02-20 16:41:46 +08:00
ShouldPreConsumedQuota : preConsumedQuota ,
2025-03-17 17:52:54 +08:00
}
if common . DebugEnabled {
println ( fmt . Sprintf ( "model_price_helper result: %s" , priceData . ToSetting ( ) ) )
}
return priceData , nil
2025-02-20 16:41:46 +08:00
}
2025-04-18 19:36:18 +08:00
func ContainPriceOrRatio ( modelName string ) bool {
_ , ok := operation_setting . GetModelPrice ( modelName , false )
if ok {
return true
}
_ , ok = operation_setting . GetModelRatio ( modelName )
if ok {
return true
}
return false
}