fix(channel-test): support tiered billing model tests (#4145)
Pre-fill BillingRequestInput from dto.Request before ModelPriceHelper, so tiered_expr billing resolves param() from the structured request instead of reading HTTP body (which is empty in channel-test context). - attachTestBillingRequestInput: marshal dto.Request → RequestInput - ResolveIncomingBillingExprRequestInput: early-return when pre-filled - settleTestQuota / buildTestLogOther: align test settlement & logging with production TryTieredSettle / InjectTieredBillingInfo paths
This commit is contained in:
+56
-12
@@ -20,6 +20,7 @@ import (
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
"github.com/QuantumNous/new-api/middleware"
|
||||
"github.com/QuantumNous/new-api/model"
|
||||
"github.com/QuantumNous/new-api/pkg/billingexpr"
|
||||
"github.com/QuantumNous/new-api/relay"
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
relayconstant "github.com/QuantumNous/new-api/relay/constant"
|
||||
@@ -232,6 +233,15 @@ func testChannel(channel *model.Channel, testModel string, endpointType string,
|
||||
info.IsChannelTest = true
|
||||
info.InitChannelMeta(c)
|
||||
|
||||
err = attachTestBillingRequestInput(info, request)
|
||||
if err != nil {
|
||||
return testResult{
|
||||
context: c,
|
||||
localErr: err,
|
||||
newAPIError: types.NewError(err, types.ErrorCodeJsonMarshalFailed),
|
||||
}
|
||||
}
|
||||
|
||||
err = helper.ModelMappedHelper(c, info, request)
|
||||
if err != nil {
|
||||
return testResult{
|
||||
@@ -468,21 +478,11 @@ func testChannel(channel *model.Channel, testModel string, endpointType string,
|
||||
}
|
||||
info.SetEstimatePromptTokens(usage.PromptTokens)
|
||||
|
||||
quota := 0
|
||||
if !priceData.UsePrice {
|
||||
quota = usage.PromptTokens + int(math.Round(float64(usage.CompletionTokens)*priceData.CompletionRatio))
|
||||
quota = int(math.Round(float64(quota) * priceData.ModelRatio))
|
||||
if priceData.ModelRatio != 0 && quota <= 0 {
|
||||
quota = 1
|
||||
}
|
||||
} else {
|
||||
quota = int(priceData.ModelPrice * common.QuotaPerUnit)
|
||||
}
|
||||
quota, tieredResult := settleTestQuota(info, priceData, usage)
|
||||
tok := time.Now()
|
||||
milliseconds := tok.Sub(tik).Milliseconds()
|
||||
consumedTime := float64(milliseconds) / 1000.0
|
||||
other := service.GenerateTextOtherInfo(c, info, priceData.ModelRatio, priceData.GroupRatioInfo.GroupRatio, priceData.CompletionRatio,
|
||||
usage.PromptTokensDetails.CachedTokens, priceData.CacheRatio, priceData.ModelPrice, priceData.GroupRatioInfo.GroupSpecialRatio)
|
||||
other := buildTestLogOther(c, info, priceData, usage, tieredResult)
|
||||
model.RecordConsumeLog(c, 1, model.RecordConsumeLogParams{
|
||||
ChannelId: channel.Id,
|
||||
PromptTokens: usage.PromptTokens,
|
||||
@@ -504,6 +504,50 @@ func testChannel(channel *model.Channel, testModel string, endpointType string,
|
||||
}
|
||||
}
|
||||
|
||||
func attachTestBillingRequestInput(info *relaycommon.RelayInfo, request dto.Request) error {
|
||||
if info == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
input, err := helper.BuildBillingExprRequestInputFromRequest(request, info.RequestHeaders)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info.BillingRequestInput = &input
|
||||
return nil
|
||||
}
|
||||
|
||||
func settleTestQuota(info *relaycommon.RelayInfo, priceData types.PriceData, usage *dto.Usage) (int, *billingexpr.TieredResult) {
|
||||
if usage != nil && info != nil && info.TieredBillingSnapshot != nil {
|
||||
isClaudeUsageSemantic := usage.UsageSemantic == "anthropic" || info.GetFinalRequestRelayFormat() == types.RelayFormatClaude
|
||||
usedVars := billingexpr.UsedVars(info.TieredBillingSnapshot.ExprString)
|
||||
if ok, quota, result := service.TryTieredSettle(info, service.BuildTieredTokenParams(usage, isClaudeUsageSemantic, usedVars)); ok {
|
||||
return quota, result
|
||||
}
|
||||
}
|
||||
|
||||
quota := 0
|
||||
if !priceData.UsePrice {
|
||||
quota = usage.PromptTokens + int(math.Round(float64(usage.CompletionTokens)*priceData.CompletionRatio))
|
||||
quota = int(math.Round(float64(quota) * priceData.ModelRatio))
|
||||
if priceData.ModelRatio != 0 && quota <= 0 {
|
||||
quota = 1
|
||||
}
|
||||
return quota, nil
|
||||
}
|
||||
|
||||
return int(priceData.ModelPrice * common.QuotaPerUnit), nil
|
||||
}
|
||||
|
||||
func buildTestLogOther(c *gin.Context, info *relaycommon.RelayInfo, priceData types.PriceData, usage *dto.Usage, tieredResult *billingexpr.TieredResult) map[string]interface{} {
|
||||
other := service.GenerateTextOtherInfo(c, info, priceData.ModelRatio, priceData.GroupRatioInfo.GroupRatio, priceData.CompletionRatio,
|
||||
usage.PromptTokensDetails.CachedTokens, priceData.CacheRatio, priceData.ModelPrice, priceData.GroupRatioInfo.GroupSpecialRatio)
|
||||
if tieredResult != nil {
|
||||
service.InjectTieredBillingInfo(other, info, tieredResult)
|
||||
}
|
||||
return other
|
||||
}
|
||||
|
||||
func coerceTestUsage(usageAny any, isStream bool, estimatePromptTokens int) (*dto.Usage, error) {
|
||||
switch u := usageAny.(type) {
|
||||
case *dto.Usage:
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
"github.com/QuantumNous/new-api/dto"
|
||||
"github.com/QuantumNous/new-api/pkg/billingexpr"
|
||||
relaycommon "github.com/QuantumNous/new-api/relay/common"
|
||||
"github.com/QuantumNous/new-api/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSettleTestQuotaUsesTieredBilling(t *testing.T) {
|
||||
info := &relaycommon.RelayInfo{
|
||||
TieredBillingSnapshot: &billingexpr.BillingSnapshot{
|
||||
BillingMode: "tiered_expr",
|
||||
ExprString: `param("stream") == true ? tier("stream", p * 3) : tier("base", p * 2)`,
|
||||
ExprHash: billingexpr.ExprHashString(`param("stream") == true ? tier("stream", p * 3) : tier("base", p * 2)`),
|
||||
GroupRatio: 1,
|
||||
EstimatedTier: "stream",
|
||||
QuotaPerUnit: common.QuotaPerUnit,
|
||||
ExprVersion: 1,
|
||||
},
|
||||
BillingRequestInput: &billingexpr.RequestInput{
|
||||
Body: []byte(`{"stream":true}`),
|
||||
},
|
||||
}
|
||||
|
||||
quota, result := settleTestQuota(info, types.PriceData{
|
||||
ModelRatio: 1,
|
||||
CompletionRatio: 2,
|
||||
}, &dto.Usage{
|
||||
PromptTokens: 1000,
|
||||
})
|
||||
|
||||
require.Equal(t, 1500, quota)
|
||||
require.NotNil(t, result)
|
||||
require.Equal(t, "stream", result.MatchedTier)
|
||||
}
|
||||
|
||||
func TestBuildTestLogOtherInjectsTieredInfo(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
|
||||
info := &relaycommon.RelayInfo{
|
||||
TieredBillingSnapshot: &billingexpr.BillingSnapshot{
|
||||
BillingMode: "tiered_expr",
|
||||
ExprString: `tier("base", p * 2)`,
|
||||
},
|
||||
ChannelMeta: &relaycommon.ChannelMeta{},
|
||||
}
|
||||
priceData := types.PriceData{
|
||||
GroupRatioInfo: types.GroupRatioInfo{GroupRatio: 1},
|
||||
}
|
||||
usage := &dto.Usage{
|
||||
PromptTokensDetails: dto.InputTokenDetails{
|
||||
CachedTokens: 12,
|
||||
},
|
||||
}
|
||||
|
||||
other := buildTestLogOther(ctx, info, priceData, usage, &billingexpr.TieredResult{
|
||||
MatchedTier: "base",
|
||||
})
|
||||
|
||||
require.Equal(t, "tiered_expr", other["billing_mode"])
|
||||
require.Equal(t, "base", other["matched_tier"])
|
||||
require.NotEmpty(t, other["expr_b64"])
|
||||
}
|
||||
Reference in New Issue
Block a user