Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: price and usage typed #55

Merged
merged 9 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 16 additions & 93 deletions common/consume/consume.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ func Wait() {
func AsyncConsume(
postGroupConsumer balance.PostGroupConsumer,
code int,
usage *relaymodel.Usage,
meta *meta.Meta,
inputPrice,
outputPrice float64,
cachedPrice float64,
cacheCreationPrice float64,
usage relaymodel.Usage,
modelPrice model.Price,
content string,
ip string,
retryTimes int,
Expand All @@ -47,12 +44,9 @@ func AsyncConsume(
context.Background(),
postGroupConsumer,
code,
usage,
meta,
inputPrice,
outputPrice,
cachedPrice,
cacheCreationPrice,
usage,
modelPrice,
content,
ip,
retryTimes,
Expand All @@ -65,29 +59,23 @@ func Consume(
ctx context.Context,
postGroupConsumer balance.PostGroupConsumer,
code int,
usage *relaymodel.Usage,
meta *meta.Meta,
inputPrice,
outputPrice float64,
cachedPrice float64,
cacheCreationPrice float64,
usage relaymodel.Usage,
modelPrice model.Price,
content string,
ip string,
retryTimes int,
requestDetail *model.RequestDetail,
downstreamResult bool,
) {
amount := CalculateAmount(usage, inputPrice, outputPrice, cachedPrice, cacheCreationPrice)
amount := CalculateAmount(usage, modelPrice)

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

err := recordConsume(meta,
code,
usage,
inputPrice,
outputPrice,
cachedPrice,
cacheCreationPrice,
modelPrice,
content,
ip,
requestDetail,
Expand All @@ -114,13 +102,9 @@ func consumeAmount(
}

func CalculateAmount(
usage *relaymodel.Usage,
inputPrice, outputPrice, cachedPrice, cacheCreationPrice float64,
usage relaymodel.Usage,
modelPrice model.Price,
) float64 {
if usage == nil {
return 0
}

promptTokens := usage.PromptTokens
completionTokens := usage.CompletionTokens
var cachedTokens int
Expand All @@ -130,24 +114,24 @@ func CalculateAmount(
cacheCreationTokens = usage.PromptTokensDetails.CacheCreationTokens
}

if cachedPrice > 0 {
if modelPrice.CachedPrice > 0 {
promptTokens -= cachedTokens
}
if cacheCreationPrice > 0 {
if modelPrice.CacheCreationPrice > 0 {
promptTokens -= cacheCreationTokens
}

promptAmount := decimal.NewFromInt(int64(promptTokens)).
Mul(decimal.NewFromFloat(inputPrice)).
Mul(decimal.NewFromFloat(modelPrice.InputPrice)).
Div(decimal.NewFromInt(model.PriceUnit))
completionAmount := decimal.NewFromInt(int64(completionTokens)).
Mul(decimal.NewFromFloat(outputPrice)).
Mul(decimal.NewFromFloat(modelPrice.OutputPrice)).
Div(decimal.NewFromInt(model.PriceUnit))
cachedAmount := decimal.NewFromInt(int64(cachedTokens)).
Mul(decimal.NewFromFloat(cachedPrice)).
Mul(decimal.NewFromFloat(modelPrice.CachedPrice)).
Div(decimal.NewFromInt(model.PriceUnit))
cacheCreationAmount := decimal.NewFromInt(int64(cacheCreationTokens)).
Mul(decimal.NewFromFloat(cacheCreationPrice)).
Mul(decimal.NewFromFloat(modelPrice.CacheCreationPrice)).
Div(decimal.NewFromInt(model.PriceUnit))

return promptAmount.
Expand Down Expand Up @@ -182,64 +166,3 @@ func processGroupConsume(
}
return consumedAmount
}

func recordConsume(
meta *meta.Meta,
code int,
usage *relaymodel.Usage,
inputPrice,
outputPrice float64,
cachedPrice float64,
cacheCreationPrice float64,
content string,
ip string,
requestDetail *model.RequestDetail,
amount float64,
retryTimes int,
downstreamResult bool,
) error {
promptTokens := 0
completionTokens := 0
cachedTokens := 0
cacheCreationTokens := 0
if usage != nil {
promptTokens = usage.PromptTokens
completionTokens = usage.CompletionTokens
if usage.PromptTokensDetails != nil {
cachedTokens = usage.PromptTokensDetails.CachedTokens
cacheCreationTokens = usage.PromptTokensDetails.CacheCreationTokens
}
}

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,
cachedTokens,
cacheCreationTokens,
meta.OriginModel,
meta.Token.ID,
meta.Token.Name,
amount,
inputPrice,
outputPrice,
cachedPrice,
cacheCreationPrice,
meta.Endpoint,
content,
int(meta.Mode),
ip,
retryTimes,
requestDetail,
downstreamResult,
)
}
56 changes: 56 additions & 0 deletions common/consume/record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package consume

import (
"github.com/labring/aiproxy/model"
"github.com/labring/aiproxy/relay/meta"
relaymodel "github.com/labring/aiproxy/relay/model"
)

func recordConsume(
meta *meta.Meta,
code int,
usage relaymodel.Usage,
modelPrice model.Price,
content string,
ip string,
requestDetail *model.RequestDetail,
amount float64,
retryTimes int,
downstreamResult bool,
) error {
us := model.Usage{
InputTokens: usage.PromptTokens,
OutputTokens: usage.CompletionTokens,
TotalTokens: usage.PromptTokens + usage.CompletionTokens,
}
if usage.PromptTokensDetails != nil {
us.CachedTokens = usage.PromptTokensDetails.CachedTokens
us.CacheCreationTokens = usage.PromptTokensDetails.CacheCreationTokens
}

var channelID int
if meta.Channel != nil {
channelID = meta.Channel.ID
}

return model.BatchRecordConsume(
meta.RequestID,
meta.RequestAt,
meta.Group.ID,
code,
channelID,
meta.OriginModel,
meta.Token.ID,
meta.Token.Name,
meta.Endpoint,
content,
int(meta.Mode),
ip,
retryTimes,
requestDetail,
downstreamResult,
us,
modelPrice,
amount,
)
}
6 changes: 1 addition & 5 deletions controller/channel-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ func testSingleModel(mc *model.ModelCaches, channel *model.Channel, modelName st
modelConfig,
meta.WithRequestID(channelTestRequestID),
)
relayController, ok := relayController(m)
if !ok {
return nil, fmt.Errorf("relay mode %d not implemented", m)
}
result := relayController(meta, newc)
result := relayHandler(meta, newc)
success := result.Error == nil
var respStr string
var code int
Expand Down
8 changes: 4 additions & 4 deletions controller/modelconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} middleware.APIResponse{data=map[string]any{configs=[]model.ModelConfig,total=int}}
// @Router /api/modelconfigs [get]
// @Router /api/model_configs [get]
func GetModelConfigs(c *gin.Context) {
page, perPage := parsePageParams(c)
_model := c.Query("model")
Expand All @@ -39,7 +39,7 @@ func GetModelConfigs(c *gin.Context) {
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} middleware.APIResponse{data=[]model.ModelConfig}
// @Router /api/modelconfigs/all [get]
// @Router /api/model_configs/all [get]
func GetAllModelConfigs(c *gin.Context) {
configs, err := model.GetAllModelConfigs()
if err != nil {
Expand All @@ -61,7 +61,7 @@ type GetModelConfigsByModelsContainsRequest struct {
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} middleware.APIResponse{data=[]model.ModelConfig}
// @Router /api/modelconfigs/contains [post]
// @Router /api/model_configs/contains [post]
func GetModelConfigsByModelsContains(c *gin.Context) {
request := GetModelConfigsByModelsContainsRequest{}
err := c.ShouldBindJSON(&request)
Expand All @@ -85,7 +85,7 @@ func GetModelConfigsByModelsContains(c *gin.Context) {
// @Produce json
// @Security ApiKeyAuth
// @Success 200 {object} middleware.APIResponse{data=map[string]any{configs=[]model.ModelConfig,total=int}}
// @Router /api/modelconfigs/search [get]
// @Router /api/model_configs/search [get]
func SearchModelConfigs(c *gin.Context) {
keyword := c.Query("keyword")
page, perPage := parsePageParams(c)
Expand Down
Loading