Commit f6c0852d by CaIon

refactor: update billing calculations to use quota per unit

- Adjusted billing calculations in tests and core logic to incorporate a new QuotaPerUnit field.
- Modified estimated quota calculations to reflect changes in tiered billing logic.
- Updated related tests to ensure accuracy with the new quota calculations.
- Enhanced dynamic pricing components to align with updated billing expressions.
parent f0589cc4
...@@ -12,11 +12,12 @@ func ComputeTieredQuotaWithRequest(snap *BillingSnapshot, params TokenParams, re ...@@ -12,11 +12,12 @@ func ComputeTieredQuotaWithRequest(snap *BillingSnapshot, params TokenParams, re
return TieredResult{}, err return TieredResult{}, err
} }
afterGroup := QuotaRound(cost * snap.GroupRatio) quotaBeforeGroup := cost / 1_000_000 * snap.QuotaPerUnit
afterGroup := QuotaRound(quotaBeforeGroup * snap.GroupRatio)
crossed := trace.MatchedTier != snap.EstimatedTier crossed := trace.MatchedTier != snap.EstimatedTier
return TieredResult{ return TieredResult{
ActualQuotaBeforeGroup: cost, ActualQuotaBeforeGroup: quotaBeforeGroup,
ActualQuotaAfterGroup: afterGroup, ActualQuotaAfterGroup: afterGroup,
MatchedTier: trace.MatchedTier, MatchedTier: trace.MatchedTier,
CrossedTier: crossed, CrossedTier: crossed,
......
...@@ -45,6 +45,7 @@ type BillingSnapshot struct { ...@@ -45,6 +45,7 @@ type BillingSnapshot struct {
EstimatedQuotaBeforeGroup float64 `json:"estimated_quota_before_group"` EstimatedQuotaBeforeGroup float64 `json:"estimated_quota_before_group"`
EstimatedQuotaAfterGroup int `json:"estimated_quota_after_group"` EstimatedQuotaAfterGroup int `json:"estimated_quota_after_group"`
EstimatedTier string `json:"estimated_tier"` EstimatedTier string `json:"estimated_tier"`
QuotaPerUnit float64 `json:"quota_per_unit"`
} }
// TieredResult holds everything needed after running tiered settlement. // TieredResult holds everything needed after running tiered settlement.
......
...@@ -225,7 +225,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT ...@@ -225,7 +225,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
return types.PriceData{}, err return types.PriceData{}, err
} }
rawQuota, trace, err := billingexpr.RunExprWithRequest(exprStr, billingexpr.TokenParams{ rawCost, trace, err := billingexpr.RunExprWithRequest(exprStr, billingexpr.TokenParams{
P: float64(promptTokens), P: float64(promptTokens),
C: float64(estimatedCompletionTokens), C: float64(estimatedCompletionTokens),
}, requestInput) }, requestInput)
...@@ -233,11 +233,13 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT ...@@ -233,11 +233,13 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
return types.PriceData{}, fmt.Errorf("model %s tiered expr run failed: %w", info.OriginModelName, err) return types.PriceData{}, fmt.Errorf("model %s tiered expr run failed: %w", info.OriginModelName, err)
} }
preConsumedQuota := billingexpr.QuotaRound(rawQuota * groupRatioInfo.GroupRatio) // Expression coefficients are $/1M tokens prices; convert to quota the same way per-call billing does.
quotaBeforeGroup := rawCost / 1_000_000 * common.QuotaPerUnit
preConsumedQuota := billingexpr.QuotaRound(quotaBeforeGroup * groupRatioInfo.GroupRatio)
freeModel := false freeModel := false
if !operation_setting.GetQuotaSetting().EnableFreeModelPreConsume { if !operation_setting.GetQuotaSetting().EnableFreeModelPreConsume {
if groupRatioInfo.GroupRatio == 0 || rawQuota == 0 { if groupRatioInfo.GroupRatio == 0 || quotaBeforeGroup == 0 {
preConsumedQuota = 0 preConsumedQuota = 0
freeModel = true freeModel = true
} }
...@@ -252,9 +254,10 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT ...@@ -252,9 +254,10 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
GroupRatio: groupRatioInfo.GroupRatio, GroupRatio: groupRatioInfo.GroupRatio,
EstimatedPromptTokens: promptTokens, EstimatedPromptTokens: promptTokens,
EstimatedCompletionTokens: estimatedCompletionTokens, EstimatedCompletionTokens: estimatedCompletionTokens,
EstimatedQuotaBeforeGroup: rawQuota, EstimatedQuotaBeforeGroup: quotaBeforeGroup,
EstimatedQuotaAfterGroup: preConsumedQuota, EstimatedQuotaAfterGroup: preConsumedQuota,
EstimatedTier: trace.MatchedTier, EstimatedTier: trace.MatchedTier,
QuotaPerUnit: common.QuotaPerUnit,
} }
info.TieredBillingSnapshot = snapshot info.TieredBillingSnapshot = snapshot
info.BillingRequestInput = &requestInput info.BillingRequestInput = &requestInput
...@@ -266,7 +269,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT ...@@ -266,7 +269,7 @@ func modelPriceHelperTiered(c *gin.Context, info *relaycommon.RelayInfo, promptT
} }
if common.DebugEnabled { if common.DebugEnabled {
println(fmt.Sprintf("model_price_helper_tiered result: model=%s preConsume=%d rawQuota=%.2f groupRatio=%.2f tier=%s", info.OriginModelName, preConsumedQuota, rawQuota, groupRatioInfo.GroupRatio, trace.MatchedTier)) println(fmt.Sprintf("model_price_helper_tiered result: model=%s preConsume=%d quotaBeforeGroup=%.2f groupRatio=%.2f tier=%s", info.OriginModelName, preConsumedQuota, quotaBeforeGroup, groupRatioInfo.GroupRatio, trace.MatchedTier))
} }
info.PriceData = priceData info.PriceData = priceData
......
...@@ -19,6 +19,8 @@ const cacheExpr = `tier("default", p * 2 + c * 10 + cr * 0.2 + cc * 2.5 + cc1h * ...@@ -19,6 +19,8 @@ const cacheExpr = `tier("default", p * 2 + c * 10 + cr * 0.2 + cc * 2.5 + cc1h *
// Expression with request probes // Expression with request probes
const probeExpr = `param("service_tier") == "fast" ? tier("fast", p * 4 + c * 20) : tier("normal", p * 2 + c * 10)` const probeExpr = `param("service_tier") == "fast" ? tier("fast", p * 4 + c * 20) : tier("normal", p * 2 + c * 10)`
const testQuotaPerUnit = 500_000.0
func makeSnapshot(expr string, groupRatio float64, estPrompt, estCompletion int) *billingexpr.BillingSnapshot { func makeSnapshot(expr string, groupRatio float64, estPrompt, estCompletion int) *billingexpr.BillingSnapshot {
return &billingexpr.BillingSnapshot{ return &billingexpr.BillingSnapshot{
BillingMode: "tiered_expr", BillingMode: "tiered_expr",
...@@ -27,14 +29,16 @@ func makeSnapshot(expr string, groupRatio float64, estPrompt, estCompletion int) ...@@ -27,14 +29,16 @@ func makeSnapshot(expr string, groupRatio float64, estPrompt, estCompletion int)
GroupRatio: groupRatio, GroupRatio: groupRatio,
EstimatedPromptTokens: estPrompt, EstimatedPromptTokens: estPrompt,
EstimatedCompletionTokens: estCompletion, EstimatedCompletionTokens: estCompletion,
QuotaPerUnit: testQuotaPerUnit,
} }
} }
func makeRelayInfo(expr string, groupRatio float64, estPrompt, estCompletion int) *relaycommon.RelayInfo { func makeRelayInfo(expr string, groupRatio float64, estPrompt, estCompletion int) *relaycommon.RelayInfo {
snap := makeSnapshot(expr, groupRatio, estPrompt, estCompletion) snap := makeSnapshot(expr, groupRatio, estPrompt, estCompletion)
cost, trace, _ := billingexpr.RunExpr(expr, billingexpr.TokenParams{P: float64(estPrompt), C: float64(estCompletion)}) cost, trace, _ := billingexpr.RunExpr(expr, billingexpr.TokenParams{P: float64(estPrompt), C: float64(estCompletion)})
snap.EstimatedQuotaBeforeGroup = cost quotaBeforeGroup := cost / 1_000_000 * testQuotaPerUnit
snap.EstimatedQuotaAfterGroup = billingexpr.QuotaRound(cost * groupRatio) snap.EstimatedQuotaBeforeGroup = quotaBeforeGroup
snap.EstimatedQuotaAfterGroup = billingexpr.QuotaRound(quotaBeforeGroup * groupRatio)
snap.EstimatedTier = trace.MatchedTier snap.EstimatedTier = trace.MatchedTier
return &relaycommon.RelayInfo{ return &relaycommon.RelayInfo{
TieredBillingSnapshot: snap, TieredBillingSnapshot: snap,
...@@ -56,7 +60,8 @@ func TestTryTieredSettleUsesFrozenRequestInput(t *testing.T) { ...@@ -56,7 +60,8 @@ func TestTryTieredSettleUsesFrozenRequestInput(t *testing.T) {
GroupRatio: 1.0, GroupRatio: 1.0,
EstimatedPromptTokens: 100, EstimatedPromptTokens: 100,
EstimatedCompletionTokens: 0, EstimatedCompletionTokens: 0,
EstimatedQuotaAfterGroup: 100, EstimatedQuotaAfterGroup: 50,
QuotaPerUnit: testQuotaPerUnit,
}, },
BillingRequestInput: &billingexpr.RequestInput{ BillingRequestInput: &billingexpr.RequestInput{
Body: []byte(`{"service_tier":"fast"}`), Body: []byte(`{"service_tier":"fast"}`),
...@@ -67,8 +72,9 @@ func TestTryTieredSettleUsesFrozenRequestInput(t *testing.T) { ...@@ -67,8 +72,9 @@ func TestTryTieredSettleUsesFrozenRequestInput(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle to apply") t.Fatal("expected tiered settle to apply")
} }
if quota != 200 { // fast: p*2 = 200; quota = 200 / 1M * 500K = 100
t.Fatalf("quota = %d, want 200", quota) if quota != 100 {
t.Fatalf("quota = %d, want 100", quota)
} }
if result == nil || result.MatchedTier != "fast" { if result == nil || result.MatchedTier != "fast" {
t.Fatalf("matched tier = %v, want fast", result) t.Fatalf("matched tier = %v, want fast", result)
...@@ -111,9 +117,9 @@ func TestTryTieredSettle_PreConsumeMatchesPostConsume(t *testing.T) { ...@@ -111,9 +117,9 @@ func TestTryTieredSettle_PreConsumeMatchesPostConsume(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// p*2 + c*10 = 2000 + 5000 = 7000 // p*2 + c*10 = 7000; quota = 7000 / 1M * 500K = 3500
if quota != 7000 { if quota != 3500 {
t.Fatalf("quota = %d, want 7000", quota) t.Fatalf("quota = %d, want 3500", quota)
} }
if quota != info.FinalPreConsumedQuota { if quota != info.FinalPreConsumedQuota {
t.Fatalf("pre-consume %d != post-consume %d", info.FinalPreConsumedQuota, quota) t.Fatalf("pre-consume %d != post-consume %d", info.FinalPreConsumedQuota, quota)
...@@ -122,7 +128,7 @@ func TestTryTieredSettle_PreConsumeMatchesPostConsume(t *testing.T) { ...@@ -122,7 +128,7 @@ func TestTryTieredSettle_PreConsumeMatchesPostConsume(t *testing.T) {
func TestTryTieredSettle_PostConsumeOverPreConsume(t *testing.T) { func TestTryTieredSettle_PostConsumeOverPreConsume(t *testing.T) {
info := makeRelayInfo(flatExpr, 1.0, 1000, 500) info := makeRelayInfo(flatExpr, 1.0, 1000, 500)
preConsumed := info.FinalPreConsumedQuota // 7000 preConsumed := info.FinalPreConsumedQuota // 3500
// Actual usage is higher than estimated // Actual usage is higher than estimated
params := billingexpr.TokenParams{P: 2000, C: 1000} params := billingexpr.TokenParams{P: 2000, C: 1000}
...@@ -130,9 +136,9 @@ func TestTryTieredSettle_PostConsumeOverPreConsume(t *testing.T) { ...@@ -130,9 +136,9 @@ func TestTryTieredSettle_PostConsumeOverPreConsume(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// p*2 + c*10 = 4000 + 10000 = 14000 // p*2 + c*10 = 14000; quota = 14000 / 1M * 500K = 7000
if quota != 14000 { if quota != 7000 {
t.Fatalf("quota = %d, want 14000", quota) t.Fatalf("quota = %d, want 7000", quota)
} }
if quota <= preConsumed { if quota <= preConsumed {
t.Fatalf("expected supplement: actual %d should > pre-consumed %d", quota, preConsumed) t.Fatalf("expected supplement: actual %d should > pre-consumed %d", quota, preConsumed)
...@@ -141,7 +147,7 @@ func TestTryTieredSettle_PostConsumeOverPreConsume(t *testing.T) { ...@@ -141,7 +147,7 @@ func TestTryTieredSettle_PostConsumeOverPreConsume(t *testing.T) {
func TestTryTieredSettle_PostConsumeUnderPreConsume(t *testing.T) { func TestTryTieredSettle_PostConsumeUnderPreConsume(t *testing.T) {
info := makeRelayInfo(flatExpr, 1.0, 1000, 500) info := makeRelayInfo(flatExpr, 1.0, 1000, 500)
preConsumed := info.FinalPreConsumedQuota // 7000 preConsumed := info.FinalPreConsumedQuota // 3500
// Actual usage is lower than estimated // Actual usage is lower than estimated
params := billingexpr.TokenParams{P: 100, C: 50} params := billingexpr.TokenParams{P: 100, C: 50}
...@@ -149,9 +155,9 @@ func TestTryTieredSettle_PostConsumeUnderPreConsume(t *testing.T) { ...@@ -149,9 +155,9 @@ func TestTryTieredSettle_PostConsumeUnderPreConsume(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// p*2 + c*10 = 200 + 500 = 700 // p*2 + c*10 = 700; quota = 700 / 1M * 500K = 350
if quota != 700 { if quota != 350 {
t.Fatalf("quota = %d, want 700", quota) t.Fatalf("quota = %d, want 350", quota)
} }
if quota >= preConsumed { if quota >= preConsumed {
t.Fatalf("expected refund: actual %d should < pre-consumed %d", quota, preConsumed) t.Fatalf("expected refund: actual %d should < pre-consumed %d", quota, preConsumed)
...@@ -170,9 +176,9 @@ func TestTryTieredSettle_ExactBoundary(t *testing.T) { ...@@ -170,9 +176,9 @@ func TestTryTieredSettle_ExactBoundary(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// standard: p*1.5 + c*7.5 = 300000 + 7500 = 307500 // standard: p*1.5 + c*7.5 = 307500; quota = 307500 / 1M * 500K = 153750
if quota != 307500 { if quota != 153750 {
t.Fatalf("quota = %d, want 307500", quota) t.Fatalf("quota = %d, want 153750", quota)
} }
if result.MatchedTier != "standard" { if result.MatchedTier != "standard" {
t.Fatalf("tier = %s, want standard", result.MatchedTier) t.Fatalf("tier = %s, want standard", result.MatchedTier)
...@@ -187,9 +193,9 @@ func TestTryTieredSettle_BoundaryPlusOne(t *testing.T) { ...@@ -187,9 +193,9 @@ func TestTryTieredSettle_BoundaryPlusOne(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// long_context: p*3 + c*11.25 = 600003 + 11250 = 611253 // long_context: p*3 + c*11.25 = 611253; quota = round(611253 / 1M * 500K) = 305627
if quota != 611253 { if quota != 305627 {
t.Fatalf("quota = %d, want 611253", quota) t.Fatalf("quota = %d, want 305627", quota)
} }
if result.MatchedTier != "long_context" { if result.MatchedTier != "long_context" {
t.Fatalf("tier = %s, want long_context", result.MatchedTier) t.Fatalf("tier = %s, want long_context", result.MatchedTier)
...@@ -221,9 +227,9 @@ func TestTryTieredSettle_HugeTokens(t *testing.T) { ...@@ -221,9 +227,9 @@ func TestTryTieredSettle_HugeTokens(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// p*2 + c*10 = 20000000 + 50000000 = 70000000 // p*2 + c*10 = 70000000; quota = 70000000 / 1M * 500K = 35000000
if quota != 70000000 { if quota != 35000000 {
t.Fatalf("quota = %d, want 70000000", quota) t.Fatalf("quota = %d, want 35000000", quota)
} }
} }
...@@ -235,23 +241,23 @@ func TestTryTieredSettle_CacheTokensAffectSettlement(t *testing.T) { ...@@ -235,23 +241,23 @@ func TestTryTieredSettle_CacheTokensAffectSettlement(t *testing.T) {
if !ok1 { if !ok1 {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// p*2 + c*10 + cr*0.2 + cc*2.5 + cc1h*4 = 2000 + 5000 + 0 + 0 + 0 = 7000 // p*2 + c*10 = 7000; quota = 7000 / 1M * 500K = 3500
// With cache tokens // With cache tokens
ok2, quota2, _ := TryTieredSettle(info, billingexpr.TokenParams{P: 1000, C: 500, CR: 10000, CC: 5000, CC1h: 2000}) ok2, quota2, _ := TryTieredSettle(info, billingexpr.TokenParams{P: 1000, C: 500, CR: 10000, CC: 5000, CC1h: 2000})
if !ok2 { if !ok2 {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// 2000 + 5000 + 10000*0.2 + 5000*2.5 + 2000*4 = 2000 + 5000 + 2000 + 12500 + 8000 = 29500 // 2000 + 5000 + 2000 + 12500 + 8000 = 29500; quota = 29500 / 1M * 500K = 14750
if quota2 <= quota1 { if quota2 <= quota1 {
t.Fatalf("cache tokens should increase quota: without=%d, with=%d", quota1, quota2) t.Fatalf("cache tokens should increase quota: without=%d, with=%d", quota1, quota2)
} }
if quota1 != 7000 { if quota1 != 3500 {
t.Fatalf("no-cache quota = %d, want 7000", quota1) t.Fatalf("no-cache quota = %d, want 3500", quota1)
} }
if quota2 != 29500 { if quota2 != 14750 {
t.Fatalf("cache quota = %d, want 29500", quota2) t.Fatalf("cache quota = %d, want 14750", quota2)
} }
} }
...@@ -269,9 +275,9 @@ func TestTryTieredSettle_RequestProbeInfluencesBilling(t *testing.T) { ...@@ -269,9 +275,9 @@ func TestTryTieredSettle_RequestProbeInfluencesBilling(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// fast: p*4 + c*20 = 4000 + 10000 = 14000 // fast: p*4 + c*20 = 14000; quota = 14000 / 1M * 500K = 7000
if quota != 14000 { if quota != 7000 {
t.Fatalf("quota = %d, want 14000", quota) t.Fatalf("quota = %d, want 7000", quota)
} }
if result.MatchedTier != "fast" { if result.MatchedTier != "fast" {
t.Fatalf("tier = %s, want fast", result.MatchedTier) t.Fatalf("tier = %s, want fast", result.MatchedTier)
...@@ -286,9 +292,9 @@ func TestTryTieredSettle_NoRequestInput_FallsBackToDefault(t *testing.T) { ...@@ -286,9 +292,9 @@ func TestTryTieredSettle_NoRequestInput_FallsBackToDefault(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// normal: p*2 + c*10 = 2000 + 5000 = 7000 // normal: p*2 + c*10 = 7000; quota = 7000 / 1M * 500K = 3500
if quota != 7000 { if quota != 3500 {
t.Fatalf("quota = %d, want 7000", quota) t.Fatalf("quota = %d, want 3500", quota)
} }
if result.MatchedTier != "normal" { if result.MatchedTier != "normal" {
t.Fatalf("tier = %s, want normal", result.MatchedTier) t.Fatalf("tier = %s, want normal", result.MatchedTier)
...@@ -306,9 +312,9 @@ func TestTryTieredSettle_GroupRatioScaling(t *testing.T) { ...@@ -306,9 +312,9 @@ func TestTryTieredSettle_GroupRatioScaling(t *testing.T) {
if !ok { if !ok {
t.Fatal("expected tiered settle") t.Fatal("expected tiered settle")
} }
// cost = 7000, after group = round(7000 * 1.5) = 10500 // exprCost = 7000, quotaBeforeGroup = 3500, afterGroup = round(3500 * 1.5) = 5250
if quota != 10500 { if quota != 5250 {
t.Fatalf("quota = %d, want 10500", quota) t.Fatalf("quota = %d, want 5250", quota)
} }
} }
......
...@@ -37,7 +37,7 @@ const { Text } = Typography; ...@@ -37,7 +37,7 @@ const { Text } = Typography;
const PRICE_SUFFIX = '$/1M tokens'; const PRICE_SUFFIX = '$/1M tokens';
function unitCostToPrice(uc) { function unitCostToPrice(uc) {
return (Number(uc) || 0) * 2; return Number(uc) || 0;
} }
function formatPrice(uc) { function formatPrice(uc) {
......
...@@ -269,7 +269,7 @@ const PricingCardView = ({ ...@@ -269,7 +269,7 @@ const PricingCardView = ({
</h3> </h3>
<div className='flex flex-col gap-1 text-xs mt-1'> <div className='flex flex-col gap-1 text-xs mt-1'>
{priceData.isDynamicPricing ? ( {priceData.isDynamicPricing ? (
formatDynamicPriceSummary(priceData.billingExpr, t) formatDynamicPriceSummary(priceData.billingExpr, t, priceData.usedGroupRatio)
) : ( ) : (
formatPriceInfo(priceData, t, siteDisplayType) formatPriceInfo(priceData, t, siteDisplayType)
)} )}
......
...@@ -2222,11 +2222,11 @@ function parseTiersFromExpr(exprStr) { ...@@ -2222,11 +2222,11 @@ function parseTiersFromExpr(exprStr) {
while ((m = tierRe.exec(exprStr)) !== null) { while ((m = tierRe.exec(exprStr)) !== null) {
tiers.push({ tiers.push({
label: m[1], label: m[1],
inputPrice: Number(m[2]) * 2, inputPrice: Number(m[2]),
outputPrice: Number(m[3]) * 2, outputPrice: Number(m[3]),
cacheReadPrice: m[4] ? Number(m[4]) * 2 : 0, cacheReadPrice: m[4] ? Number(m[4]) : 0,
cacheCreatePrice: m[5] ? Number(m[5]) * 2 : 0, cacheCreatePrice: m[5] ? Number(m[5]) : 0,
cacheCreate1hPrice: m[6] ? Number(m[6]) * 2 : 0, cacheCreate1hPrice: m[6] ? Number(m[6]) : 0,
}); });
} }
return tiers; return tiers;
......
...@@ -897,9 +897,10 @@ export const getModelPriceItems = ( ...@@ -897,9 +897,10 @@ export const getModelPriceItems = (
}; };
// 格式化动态计费摘要(用于卡片视图,与 formatPriceInfo 风格统一) // 格式化动态计费摘要(用于卡片视图,与 formatPriceInfo 风格统一)
export const formatDynamicPriceSummary = (billingExpr, t) => { export const formatDynamicPriceSummary = (billingExpr, t, groupRatio = 1) => {
if (!billingExpr) return <span style={{ color: 'var(--semi-color-text-1)' }}>{t('动态计费')}</span>; if (!billingExpr) return <span style={{ color: 'var(--semi-color-text-1)' }}>{t('动态计费')}</span>;
const gr = groupRatio || 1;
const tierMatches = billingExpr.match(/tier\(/g) || []; const tierMatches = billingExpr.match(/tier\(/g) || [];
const tierCount = tierMatches.length; const tierCount = tierMatches.length;
...@@ -923,19 +924,19 @@ export const formatDynamicPriceSummary = (billingExpr, t) => { ...@@ -923,19 +924,19 @@ export const formatDynamicPriceSummary = (billingExpr, t) => {
{firstTierMatch && ( {firstTierMatch && (
<> <>
<span style={lineStyle}> <span style={lineStyle}>
{t('输入价格')} ${(Number(firstTierMatch[1]) * 2).toFixed(4)}{unitSuffix} {t('输入价格')} ${(Number(firstTierMatch[1]) * gr).toFixed(4)}{unitSuffix}
</span> </span>
<span style={lineStyle}> <span style={lineStyle}>
{t('输出价格')} ${(Number(firstTierMatch[2]) * 2).toFixed(4)}{unitSuffix} {t('输出价格')} ${(Number(firstTierMatch[2]) * gr).toFixed(4)}{unitSuffix}
</span> </span>
{firstTierMatch[3] && ( {firstTierMatch[3] && (
<span style={lineStyle}> <span style={lineStyle}>
{t('缓存读取价格')} ${(Number(firstTierMatch[3]) * 2).toFixed(4)}{unitSuffix} {t('缓存读取价格')} ${(Number(firstTierMatch[3]) * gr).toFixed(4)}{unitSuffix}
</span> </span>
)} )}
{firstTierMatch[4] && ( {firstTierMatch[4] && (
<span style={lineStyle}> <span style={lineStyle}>
{t('缓存创建价格')} ${(Number(firstTierMatch[4]) * 2).toFixed(4)}{unitSuffix} {t('缓存创建价格')} ${(Number(firstTierMatch[4]) * gr).toFixed(4)}{unitSuffix}
</span> </span>
)} )}
</> </>
......
...@@ -60,10 +60,10 @@ const { Text } = Typography; ...@@ -60,10 +60,10 @@ const { Text } = Typography;
const PRICE_SUFFIX = '$/1M tokens'; const PRICE_SUFFIX = '$/1M tokens';
function unitCostToPrice(uc) { function unitCostToPrice(uc) {
return (Number(uc) || 0) * 2; return Number(uc) || 0;
} }
function priceToUnitCost(price) { function priceToUnitCost(price) {
return (Number(price) || 0) / 2; return Number(price) || 0;
} }
const OPS = ['<', '<=', '>', '>=']; const OPS = ['<', '<=', '>', '>='];
...@@ -762,23 +762,23 @@ const PRESET_GROUPS = [ ...@@ -762,23 +762,23 @@ const PRESET_GROUPS = [
{ {
group: '固定价格', group: '固定价格',
presets: [ presets: [
{ key: 'flat', label: 'Flat', expr: 'tier("base", p * 1 + c * 2)' }, { key: 'flat', label: 'Flat', expr: 'tier("base", p * 2 + c * 4)' },
{ key: 'claude-opus', label: 'Claude Opus 4.6', expr: 'tier("base", p * 2.5 + c * 12.5 + cr * 0.25 + cc * 3.125 + cc1h * 5)' }, { key: 'claude-opus', label: 'Claude Opus 4.6', expr: 'tier("base", p * 5 + c * 25 + cr * 0.5 + cc * 6.25 + cc1h * 10)' },
{ key: 'gpt-5.4', label: 'GPT-5.4', expr: 'tier("base", p * 1.25 + c * 5 + cr * 0.125)' }, { key: 'gpt-5.4', label: 'GPT-5.4', expr: 'tier("base", p * 2.5 + c * 10 + cr * 0.25)' },
], ],
}, },
{ {
group: '阶梯计费', group: '阶梯计费',
presets: [ presets: [
{ key: 'claude-sonnet', label: 'Claude Sonnet 4.5', expr: 'p <= 200000 ? tier("standard", p * 1.5 + c * 7.5 + cr * 0.15 + cc * 1.875 + cc1h * 3) : tier("long_context", p * 3 + c * 11.25 + cr * 0.3 + cc * 3.75 + cc1h * 6)' }, { key: 'claude-sonnet', label: 'Claude Sonnet 4.5', expr: 'p <= 200000 ? tier("standard", p * 3 + c * 15 + cr * 0.3 + cc * 3.75 + cc1h * 6) : tier("long_context", p * 6 + c * 22.5 + cr * 0.6 + cc * 7.5 + cc1h * 12)' },
{ key: 'qwen3-max', label: 'Qwen3-Max', expr: 'p <= 32000 ? tier("short", p * 0.6 + c * 3 + cr * 0.12 + cc * 0.75) : p <= 128000 ? tier("mid", p * 1.2 + c * 6 + cr * 0.24 + cc * 1.5) : tier("long", p * 1.5 + c * 7.5 + cr * 0.3 + cc * 1.875)' }, { key: 'qwen3-max', label: 'Qwen3-Max', expr: 'p <= 32000 ? tier("short", p * 1.2 + c * 6 + cr * 0.24 + cc * 1.5) : p <= 128000 ? tier("mid", p * 2.4 + c * 12 + cr * 0.48 + cc * 3) : tier("long", p * 3 + c * 15 + cr * 0.6 + cc * 3.75)' },
{ key: 'glm-4.5-air', label: 'GLM-4.5-Air', expr: 'p < 32000 && c < 200 ? tier("short_output", p * 0.4 + c * 1 + cr * 0.08) : p < 32000 && c >= 200 ? tier("long_output", p * 0.4 + c * 3 + cr * 0.08) : tier("mid_context", p * 0.6 + c * 4 + cr * 0.12)' }, { key: 'glm-4.5-air', label: 'GLM-4.5-Air', expr: 'p < 32000 && c < 200 ? tier("short_output", p * 0.8 + c * 2 + cr * 0.16) : p < 32000 && c >= 200 ? tier("long_output", p * 0.8 + c * 6 + cr * 0.16) : tier("mid_context", p * 1.2 + c * 8 + cr * 0.24)' },
], ],
}, },
{ {
group: '多模态', group: '多模态',
presets: [ presets: [
{ key: 'qwen3-omni-flash', label: 'Qwen3-Omni-Flash', expr: 'tier("base", p * 0.215 + c * 1.53 + img * 0.39 + ai * 1.905 + ao * 7.555)' }, { key: 'qwen3-omni-flash', label: 'Qwen3-Omni-Flash', expr: 'tier("base", p * 0.43 + c * 3.06 + img * 0.78 + ai * 3.81 + ao * 15.11)' },
], ],
}, },
{ {
...@@ -786,12 +786,12 @@ const PRESET_GROUPS = [ ...@@ -786,12 +786,12 @@ const PRESET_GROUPS = [
presets: [ presets: [
{ {
key: 'claude-opus-fast', label: 'Claude Opus 4.6 Fast', key: 'claude-opus-fast', label: 'Claude Opus 4.6 Fast',
expr: 'tier("base", p * 2.5 + c * 12.5 + cr * 0.25 + cc * 3.125 + cc1h * 5)', expr: 'tier("base", p * 5 + c * 25 + cr * 0.5 + cc * 6.25 + cc1h * 10)',
requestRules: [{ conditions: [{ source: SOURCE_HEADER, path: 'anthropic-beta', mode: MATCH_CONTAINS, value: 'fast-mode-2026-02-01' }], multiplier: '6' }], requestRules: [{ conditions: [{ source: SOURCE_HEADER, path: 'anthropic-beta', mode: MATCH_CONTAINS, value: 'fast-mode-2026-02-01' }], multiplier: '6' }],
}, },
{ {
key: 'gpt-5.4-fast', label: 'GPT-5.4 Fast', key: 'gpt-5.4-fast', label: 'GPT-5.4 Fast',
expr: 'tier("base", p * 1.25 + c * 5 + cr * 0.125)', expr: 'tier("base", p * 2.5 + c * 10 + cr * 0.25)',
requestRules: [{ conditions: [{ source: SOURCE_PARAM, path: 'service_tier', mode: MATCH_EQ, value: 'fast' }], multiplier: '2' }], requestRules: [{ conditions: [{ source: SOURCE_PARAM, path: 'service_tier', mode: MATCH_EQ, value: 'fast' }], multiplier: '2' }],
}, },
], ],
...@@ -801,12 +801,12 @@ const PRESET_GROUPS = [ ...@@ -801,12 +801,12 @@ const PRESET_GROUPS = [
presets: [ presets: [
{ {
key: 'night-discount', label: '夜间半价', key: 'night-discount', label: '夜间半价',
expr: 'tier("base", p * 1.5 + c * 7.5)', expr: 'tier("base", p * 3 + c * 15)',
requestRules: [{ conditions: [{ source: SOURCE_TIME, timeFunc: 'hour', timezone: 'Asia/Shanghai', mode: MATCH_RANGE, rangeStart: '21', rangeEnd: '6' }], multiplier: '0.5' }], requestRules: [{ conditions: [{ source: SOURCE_TIME, timeFunc: 'hour', timezone: 'Asia/Shanghai', mode: MATCH_RANGE, rangeStart: '21', rangeEnd: '6' }], multiplier: '0.5' }],
}, },
{ {
key: 'weekend-discount', label: '周末8折', key: 'weekend-discount', label: '周末8折',
expr: 'tier("base", p * 1.5 + c * 7.5)', expr: 'tier("base", p * 3 + c * 15)',
requestRules: [ requestRules: [
{ conditions: [{ source: SOURCE_TIME, timeFunc: 'weekday', timezone: 'Asia/Shanghai', mode: MATCH_EQ, value: '0' }], multiplier: '0.8' }, { conditions: [{ source: SOURCE_TIME, timeFunc: 'weekday', timezone: 'Asia/Shanghai', mode: MATCH_EQ, value: '0' }], multiplier: '0.8' },
{ conditions: [{ source: SOURCE_TIME, timeFunc: 'weekday', timezone: 'Asia/Shanghai', mode: MATCH_EQ, value: '6' }], multiplier: '0.8' }, { conditions: [{ source: SOURCE_TIME, timeFunc: 'weekday', timezone: 'Asia/Shanghai', mode: MATCH_EQ, value: '6' }], multiplier: '0.8' },
...@@ -814,7 +814,7 @@ const PRESET_GROUPS = [ ...@@ -814,7 +814,7 @@ const PRESET_GROUPS = [
}, },
{ {
key: 'new-year-promo', label: '新年促销', key: 'new-year-promo', label: '新年促销',
expr: 'tier("base", p * 1.5 + c * 7.5)', expr: 'tier("base", p * 3 + c * 15)',
requestRules: [{ conditions: [ requestRules: [{ conditions: [
{ source: SOURCE_TIME, timeFunc: 'month', timezone: 'Asia/Shanghai', mode: MATCH_EQ, value: '1' }, { source: SOURCE_TIME, timeFunc: 'month', timezone: 'Asia/Shanghai', mode: MATCH_EQ, value: '1' },
{ source: SOURCE_TIME, timeFunc: 'day', timezone: 'Asia/Shanghai', mode: MATCH_EQ, value: '1' }, { source: SOURCE_TIME, timeFunc: 'day', timezone: 'Asia/Shanghai', mode: MATCH_EQ, value: '1' },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment