diff --git a/model/log.go b/model/log.go index 0ff348fae589..921b2fd0ad60 100644 --- a/model/log.go +++ b/model/log.go @@ -407,16 +407,18 @@ func RecordConsumeLog(c *gin.Context, userId int, params RecordConsumeLogParams) } type RecordTaskBillingLogParams struct { - UserId int - LogType int - Content string - ChannelId int - ModelName string - Quota int - TokenId int - Group string - Other map[string]interface{} - NodeName string // 任务发起节点;为空时回退当前节点 + UserId int + LogType int + Content string + ChannelId int + ModelName string + Quota int + TokenId int + Group string + PromptTokens int + CompletionTokens int + Other map[string]interface{} + NodeName string // 任务发起节点;为空时回退当前节点 } func RecordTaskBillingLog(params RecordTaskBillingLogParams) { @@ -437,13 +439,15 @@ func RecordTaskBillingLog(params RecordTaskBillingLogParams) { CreatedAt: createdAt, Type: params.LogType, Content: params.Content, - TokenName: tokenName, - ModelName: params.ModelName, - Quota: params.Quota, - ChannelId: params.ChannelId, - TokenId: params.TokenId, - Group: params.Group, - Other: common.MapToJsonStr(params.Other), + TokenName: tokenName, + ModelName: params.ModelName, + Quota: params.Quota, + PromptTokens: params.PromptTokens, + CompletionTokens: params.CompletionTokens, + ChannelId: params.ChannelId, + TokenId: params.TokenId, + Group: params.Group, + Other: common.MapToJsonStr(params.Other), } err := createLog(log) if err != nil { @@ -461,6 +465,7 @@ func RecordTaskBillingLog(params RecordTaskBillingLogParams) { ModelName: params.ModelName, Quota: params.Quota, CreatedAt: createdAt, + TokenUsed: params.PromptTokens + params.CompletionTokens, UseGroup: params.Group, TokenID: params.TokenId, ChannelID: params.ChannelId, diff --git a/service/task_billing.go b/service/task_billing.go index 31e29e32eeef..4d0bc7639d74 100644 --- a/service/task_billing.go +++ b/service/task_billing.go @@ -185,6 +185,11 @@ func RefundTaskQuota(ctx context.Context, task *model.Task, reason string) { // actualQuota 是任务完成后的实际应扣额度,与预扣额度 (task.Quota) 做差额结算。 // reason 用于日志记录(例如 "token重算" 或 "adaptor调整")。 func RecalculateTaskQuota(ctx context.Context, task *model.Task, actualQuota int, reason string) { + RecalculateTaskQuotaWithUsage(ctx, task, actualQuota, reason, 0, 0) +} + +// RecalculateTaskQuotaWithUsage records optional token usage when settling an async task. +func RecalculateTaskQuotaWithUsage(ctx context.Context, task *model.Task, actualQuota int, reason string, promptTokens int, completionTokens int) { if actualQuota <= 0 { return } @@ -240,6 +245,8 @@ func RecalculateTaskQuota(ctx context.Context, task *model.Task, actualQuota int Quota: logQuota, TokenId: task.PrivateData.TokenId, Group: task.Group, + PromptTokens: promptTokens, + CompletionTokens: completionTokens, Other: other, NodeName: task.PrivateData.NodeName, }) @@ -298,5 +305,5 @@ func RecalculateTaskQuotaByTokens(ctx context.Context, task *model.Task, totalTo actualQuota := int(float64(totalTokens) * modelRatio * finalGroupRatio * otherMultiplier) reason := fmt.Sprintf("token重算:tokens=%d, modelRatio=%.2f, groupRatio=%.2f, otherMultiplier=%.4f", totalTokens, modelRatio, finalGroupRatio, otherMultiplier) - RecalculateTaskQuota(ctx, task, actualQuota, reason) + RecalculateTaskQuotaWithUsage(ctx, task, actualQuota, reason, 0, totalTokens) }