-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40a2365
commit cc30b33
Showing
8 changed files
with
213 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package consume | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/labring/sealos/service/aiproxy/common/balance" | ||
"github.com/labring/sealos/service/aiproxy/model" | ||
"github.com/labring/sealos/service/aiproxy/relay/meta" | ||
relaymodel "github.com/labring/sealos/service/aiproxy/relay/model" | ||
"github.com/shopspring/decimal" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
var consumeWaitGroup sync.WaitGroup | ||
|
||
func Wait() { | ||
consumeWaitGroup.Wait() | ||
} | ||
|
||
func AsyncConsume( | ||
ctx context.Context, | ||
postGroupConsumer balance.PostGroupConsumer, | ||
code int, | ||
usage *relaymodel.Usage, | ||
meta *meta.Meta, | ||
inputPrice, | ||
outputPrice float64, | ||
content string, | ||
requestDetail *model.RequestDetail, | ||
) { | ||
if meta.IsChannelTest { | ||
return | ||
} | ||
|
||
consumeWaitGroup.Add(1) | ||
defer func() { | ||
consumeWaitGroup.Done() | ||
if r := recover(); r != nil { | ||
log.Errorf("panic in consume: %v", r) | ||
} | ||
}() | ||
|
||
go Consume(ctx, postGroupConsumer, code, usage, meta, inputPrice, outputPrice, content, requestDetail) | ||
} | ||
|
||
func Consume( | ||
ctx context.Context, | ||
postGroupConsumer balance.PostGroupConsumer, | ||
code int, | ||
usage *relaymodel.Usage, | ||
meta *meta.Meta, | ||
inputPrice, | ||
outputPrice float64, | ||
content string, | ||
requestDetail *model.RequestDetail, | ||
) { | ||
if meta.IsChannelTest { | ||
return | ||
} | ||
|
||
amount := calculateAmount(ctx, usage, inputPrice, outputPrice, postGroupConsumer, meta) | ||
|
||
err := recordConsume(meta, code, usage, inputPrice, outputPrice, content, requestDetail, amount) | ||
if err != nil { | ||
log.Error("error batch record consume: " + err.Error()) | ||
} | ||
} | ||
|
||
func calculateAmount( | ||
ctx context.Context, | ||
usage *relaymodel.Usage, | ||
inputPrice, outputPrice float64, | ||
postGroupConsumer balance.PostGroupConsumer, | ||
meta *meta.Meta, | ||
) float64 { | ||
if usage == nil { | ||
return 0 | ||
} | ||
|
||
promptTokens := usage.PromptTokens | ||
completionTokens := usage.CompletionTokens | ||
totalTokens := promptTokens + completionTokens | ||
|
||
if totalTokens == 0 { | ||
return 0 | ||
} | ||
|
||
promptAmount := decimal.NewFromInt(int64(promptTokens)). | ||
Mul(decimal.NewFromFloat(inputPrice)). | ||
Div(decimal.NewFromInt(model.PriceUnit)) | ||
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 | ||
} | ||
|
||
func processGroupConsume( | ||
ctx context.Context, | ||
amount float64, | ||
postGroupConsumer balance.PostGroupConsumer, | ||
meta *meta.Meta, | ||
) float64 { | ||
consumedAmount, err := postGroupConsumer.PostGroupConsume(ctx, meta.Token.Name, amount) | ||
if err != nil { | ||
log.Error("error consuming token remain amount: " + err.Error()) | ||
if err := model.CreateConsumeError( | ||
meta.RequestID, | ||
meta.RequestAt, | ||
meta.Group.ID, | ||
meta.Token.Name, | ||
meta.OriginModel, | ||
err.Error(), | ||
amount, | ||
meta.Token.ID, | ||
); err != nil { | ||
log.Error("failed to create consume error: " + err.Error()) | ||
} | ||
return amount | ||
} | ||
return consumedAmount | ||
} | ||
|
||
func recordConsume(meta *meta.Meta, code int, usage *relaymodel.Usage, inputPrice, outputPrice float64, content string, requestDetail *model.RequestDetail, amount float64) error { | ||
promptTokens := 0 | ||
completionTokens := 0 | ||
if usage != nil { | ||
promptTokens = usage.PromptTokens | ||
completionTokens = usage.CompletionTokens | ||
} | ||
|
||
var channelID int | ||
if meta.Channel != nil { | ||
channelID = meta.Channel.ID | ||
} | ||
|
||
return model.BatchRecordConsume( | ||
meta.RequestID, | ||
meta.RequestAt, | ||
meta.Group.ID, | ||
code, | ||
channelID, | ||
promptTokens, | ||
completionTokens, | ||
meta.OriginModel, | ||
meta.Token.ID, | ||
meta.Token.Name, | ||
amount, | ||
inputPrice, | ||
outputPrice, | ||
meta.Endpoint, | ||
content, | ||
meta.Mode, | ||
requestDetail, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.