chore: 添加计费处理调试日志
This commit is contained in:
@@ -30,10 +30,12 @@ func CalculateBilling(config map[string]any, billingData map[string]any) map[str
|
||||
|
||||
func calculateInferenceTierBilling(config map[string]any, data map[string]any) map[string]any {
|
||||
promptTokens := gconv.Int64(data["prompt_tokens"])
|
||||
fmt.Printf("calculateInferenceTierBilling promptTokens=%v\n", promptTokens)
|
||||
completionTokens := gconv.Int64(data["completion_tokens"])
|
||||
fmt.Printf("calculateInferenceTierBilling completionTokens=%v\n", completionTokens)
|
||||
hasAudio := gconv.Bool(data["has_audio"])
|
||||
inputK := promptTokens / 1000
|
||||
|
||||
fmt.Printf("calculateInferenceTierBilling inputK=%v\n", inputK)
|
||||
tiers := config["pricing"].(map[string]any)["tiers"].([]any)
|
||||
var matched map[string]any
|
||||
for _, t := range tiers {
|
||||
@@ -53,11 +55,13 @@ func calculateInferenceTierBilling(config map[string]any, data map[string]any) m
|
||||
} else {
|
||||
inputPrice = gconv.Float64(matched["input_price"])
|
||||
}
|
||||
fmt.Printf("calculateInferenceTierBilling inputPrice=%v\n", inputPrice)
|
||||
outputPrice := gconv.Float64(matched["output_price"])
|
||||
|
||||
fmt.Printf("calculateInferenceTierBilling outputPrice=%v\n", outputPrice)
|
||||
inputCost := float64(promptTokens) * inputPrice / 1000000
|
||||
outputCost := float64(completionTokens) * outputPrice / 1000000
|
||||
|
||||
fmt.Printf("calculateBilling inputCost=%v, outputCost=%v\n", inputCost, outputCost)
|
||||
// 推理模型
|
||||
return map[string]any{
|
||||
"model_name": data["model_name"],
|
||||
|
||||
@@ -107,7 +107,6 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa
|
||||
|
||||
// 计费处理
|
||||
if len(model.BillingConfig) > 0 && len(task.BillingData) > 0 {
|
||||
fmt.Printf("[handleOne] 计费处理 BillingConfig=%s, BillingData=%s", model.BillingConfig, task.BillingData)
|
||||
// 取请求阶段数据作为基础
|
||||
billingInput := make(map[string]any)
|
||||
for k, v := range task.BillingData[0] {
|
||||
@@ -123,12 +122,12 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa
|
||||
if billingResult != nil {
|
||||
task.BillingData[0] = billingResult
|
||||
}
|
||||
fmt.Printf("[handleOne] 计费处理 BillingResult=%s", billingResult)
|
||||
if billingResult != nil {
|
||||
task.BillingData[0] = billingResult
|
||||
totalFee := gconv.Float64(billingResult["total_fee"])
|
||||
fmt.Printf("[handleOne] 计费处理 totalFee=%f", totalFee)
|
||||
fmt.Printf("[handleOne] 计费处理 totalFee1=%f", totalFee)
|
||||
if totalFee > 0 {
|
||||
fmt.Printf("[handleOne] 计费处理 totalFee2=%f", totalFee)
|
||||
_ = gateway.DeductBalance(util.AsyncCtx(ctx), model.TenantId, -totalFee)
|
||||
}
|
||||
}
|
||||
@@ -326,8 +325,6 @@ func (w *asyncWorker) parseAndRetry(ctx context.Context, body map[string]any, mo
|
||||
|
||||
// 计费
|
||||
if len(model.BillingConfig) > 0 && len(task.BillingData) > 0 {
|
||||
fmt.Println("task.BillingData", task.BillingData)
|
||||
fmt.Println("task.BillingData", task.BillingData)
|
||||
requestData := task.BillingData[0]
|
||||
retryData := make(map[string]any)
|
||||
for k, v := range requestData {
|
||||
@@ -338,12 +335,12 @@ func (w *asyncWorker) parseAndRetry(ctx context.Context, body map[string]any, mo
|
||||
retryData[k] = v
|
||||
}
|
||||
billingResult := util.CalculateBilling(model.BillingConfig, retryData)
|
||||
fmt.Println("billingResult", billingResult)
|
||||
if billingResult != nil {
|
||||
task.BillingData = append(task.BillingData, billingResult)
|
||||
totalFee := gconv.Float64(billingResult["total_fee"])
|
||||
fmt.Println("totalFee", totalFee)
|
||||
fmt.Println("totalFee==================", totalFee)
|
||||
if totalFee > 0 {
|
||||
fmt.Println("deduct balance================", totalFee)
|
||||
_ = gateway.DeductBalance(util.AsyncCtx(ctx), task.TenantId, -totalFee)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user