Skip to content

Commit

Permalink
feat: amount log
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Jan 23, 2025
1 parent 20990dd commit 00b5fb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
26 changes: 16 additions & 10 deletions service/aiproxy/common/consume/consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,31 @@ func Consume(
return
}

amount := calculateAmount(ctx, usage, inputPrice, outputPrice, postGroupConsumer, meta)
amount := CalculateAmount(usage, inputPrice, outputPrice)

amount = consumeAmount(ctx, amount, postGroupConsumer, meta)

err := recordConsume(meta, code, usage, inputPrice, outputPrice, content, ip, requestDetail, amount)
if err != nil {
log.Error("error batch record consume: " + err.Error())
}
}

func calculateAmount(
func consumeAmount(
ctx context.Context,
usage *relaymodel.Usage,
inputPrice, outputPrice float64,
amount float64,
postGroupConsumer balance.PostGroupConsumer,
meta *meta.Meta,
) float64 {
if amount > 0 {
return processGroupConsume(ctx, amount, postGroupConsumer, meta)
}
return 0
}

func CalculateAmount(
usage *relaymodel.Usage,
inputPrice, outputPrice float64,
) float64 {
if usage == nil {
return 0
Expand All @@ -104,13 +115,8 @@ func calculateAmount(
completionAmount := decimal.NewFromInt(int64(completionTokens)).
Mul(decimal.NewFromFloat(outputPrice)).
Div(decimal.NewFromInt(model.PriceUnit))
amount := promptAmount.Add(completionAmount).InexactFloat64()

if amount > 0 {
return processGroupConsume(ctx, amount, postGroupConsumer, meta)
}

return 0
return promptAmount.Add(completionAmount).InexactFloat64()
}

func processGroupConsume(
Expand Down
5 changes: 5 additions & 0 deletions service/aiproxy/relay/controller/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func Handle(meta *meta.Meta, c *gin.Context, preProcess func() (*PreCheckGroupBa
return respErr
}

amount := consume.CalculateAmount(usage, preCheckReq.InputPrice, preCheckReq.OutputPrice)
if amount > 0 {
log.Data["amount"] = amount
}

// 6. Post consume
consume.AsyncConsume(
postGroupConsumer,
Expand Down

0 comments on commit 00b5fb7

Please sign in to comment.