diff --git a/common/util/billing.go b/common/util/billing.go index c3a09ca..59fac22 100644 --- a/common/util/billing.go +++ b/common/util/billing.go @@ -35,7 +35,6 @@ func calculateInferenceTierBilling(config map[string]any, data map[string]any) m completionTokens := gconv.Int64(data["completion_tokens"]) hasAudio := gconv.Bool(data["has_audio"]) inputK := promptTokens / 1000 - tiers := config["pricing"].(map[string]any)["tiers"].([]any) var matched map[string]any for _, t := range tiers { @@ -56,7 +55,6 @@ func calculateInferenceTierBilling(config map[string]any, data map[string]any) m inputPrice = gconv.Float64(matched["input_price"]) } outputPrice := gconv.Float64(matched["output_price"]) - inputCost := float64(promptTokens) * inputPrice / 1000000 outputCost := float64(completionTokens) * outputPrice / 1000000 diff --git a/service/gateway/gateway_http_service.go b/service/gateway/gateway_http_service.go index 0ad2635..9df0638 100644 --- a/service/gateway/gateway_http_service.go +++ b/service/gateway/gateway_http_service.go @@ -257,7 +257,9 @@ func DeductBalance(ctx context.Context, tenantId uint64, amount float64) error { // TenantSurplusResp 租户余额返回 type TenantSurplusResp struct { - Surplus float64 `json:"surplus"` + Tenant struct { + Surplus float64 `json:"surplus"` + } `json:"tenant"` } // GetTenantSurplus 获取租户余额 @@ -278,7 +280,7 @@ func GetTenantSurplus(ctx context.Context, tenantId uint64) (float64, error) { g.Log().Warningf(ctx, "[获取余额] 失败 tenantId=%d err=%v", tenantId, err) return 0, err } - return resp.Surplus, nil + return resp.Tenant.Surplus, nil } //// callback 向回调地址 POST 任务结果(与查询接口 GetTaskRes 出参一致) diff --git a/service/task/worker.go b/service/task/worker.go index 79d7b2e..5f466c8 100644 --- a/service/task/worker.go +++ b/service/task/worker.go @@ -46,11 +46,11 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa // 1) 查询余额 // ============================================ surplus, _ = gateway.GetTenantSurplus(ctx, model.TenantId) - if surplus <= 0 { + if surplus <= 200 { w.failTask(ctx, task, startTime, "租户余额不足") return } - g.Log().Infof(ctx, "[handleOne] 当前余额 tenantId=%d surplus=%.2f", task.TenantId, surplus) + g.Log().Infof(ctx, "[handleOne] 当前余额 tenantId=%d surplus=%.2f", model.TenantId, surplus) // ============================================ // 2) 调用模型 @@ -122,12 +122,11 @@ func (w *asyncWorker) handleOne(ctx context.Context, task *entity.ModelGatewayTa if billingResult != nil { task.BillingData[0] = billingResult } - if billingResult != nil { task.BillingData[0] = billingResult totalFee := gconv.Float64(billingResult["total_fee"]) if totalFee > 0 { - _ = gateway.DeductBalance(util.AsyncCtx(ctx), task.TenantId, -totalFee) + _ = gateway.DeductBalance(util.AsyncCtx(ctx), model.TenantId, -totalFee) } } }