From 2d3431901a256034c1d8fcf2f2c74e6a840a43cd Mon Sep 17 00:00:00 2001 From: zijiren <84728412+zijiren233@users.noreply.github.com> Date: Sun, 2 Mar 2025 22:48:55 +0800 Subject: [PATCH] feat: impl disable billing (#5431) --- service/aiproxy/model/main.go | 8 ++++++-- service/aiproxy/relay/adaptor/openai/token.go | 14 ++++++++++---- service/aiproxy/relay/controller/image.go | 5 +++++ service/aiproxy/relay/controller/price.go | 7 ------- service/aiproxy/relay/controller/rerank.go | 11 ++++++++--- service/aiproxy/relay/controller/stt.go | 13 +++++++++---- service/aiproxy/relay/controller/text.go | 11 ++++++++--- service/aiproxy/relay/controller/tts.go | 11 ++++++++--- 8 files changed, 54 insertions(+), 26 deletions(-) diff --git a/service/aiproxy/model/main.go b/service/aiproxy/model/main.go index 717936535..66dd8d3c0 100644 --- a/service/aiproxy/model/main.go +++ b/service/aiproxy/model/main.go @@ -42,9 +42,13 @@ func chooseDB(envName string) (*gorm.DB, error) { return OpenMySQL(dsn) default: // Use SQLite - log.Info("SQL_DSN not set, using SQLite as database: ", common.SQLitePath) + absPath, err := filepath.Abs(common.SQLitePath) + if err != nil { + return nil, fmt.Errorf("failed to get absolute path of SQLite database: %w", err) + } + log.Info("SQL_DSN not set, using SQLite as database: ", absPath) common.UsingSQLite = true - return OpenSQLite(common.SQLitePath) + return OpenSQLite(absPath) } } diff --git a/service/aiproxy/relay/adaptor/openai/token.go b/service/aiproxy/relay/adaptor/openai/token.go index 19c0e7fb1..60ece2172 100644 --- a/service/aiproxy/relay/adaptor/openai/token.go +++ b/service/aiproxy/relay/adaptor/openai/token.go @@ -7,6 +7,7 @@ import ( "sync" "unicode/utf8" + "github.com/labring/sealos/service/aiproxy/common/config" "github.com/labring/sealos/service/aiproxy/common/image" "github.com/labring/sealos/service/aiproxy/relay/model" "github.com/pkoukk/tiktoken-go" @@ -56,6 +57,9 @@ func getTokenNum(tokenEncoder *tiktoken.Tiktoken, text string) int { } func CountTokenMessages(messages []*model.Message, model string) int { + if !config.GetBillingEnabled() { + return 0 + } tokenEncoder := getTokenEncoder(model) // Reference: // https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb @@ -203,6 +207,9 @@ func countImageTokens(url string, detail string, model string) (_ int, err error } func CountTokenInput(input any, model string) int { + if !config.GetBillingEnabled() { + return 0 + } switch v := input.(type) { case string: return CountTokenText(v, model) @@ -223,12 +230,11 @@ func CountTokenInput(input any, model string) int { } func CountTokenText(text string, model string) int { + if !config.GetBillingEnabled() { + return 0 + } if strings.HasPrefix(model, "tts") { return utf8.RuneCountInString(text) } return getTokenNum(getTokenEncoder(model), text) } - -func CountToken(text string) int { - return CountTokenInput(text, "gpt-3.5-turbo") -} diff --git a/service/aiproxy/relay/controller/image.go b/service/aiproxy/relay/controller/image.go index 919d17faa..3acaec388 100644 --- a/service/aiproxy/relay/controller/image.go +++ b/service/aiproxy/relay/controller/image.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/gin-gonic/gin" + "github.com/labring/sealos/service/aiproxy/common/config" "github.com/labring/sealos/service/aiproxy/model" "github.com/labring/sealos/service/aiproxy/relay/meta" relaymodel "github.com/labring/sealos/service/aiproxy/relay/model" @@ -46,6 +47,10 @@ func getImageRequest(meta *meta.Meta, c *gin.Context) (*relaymodel.ImageRequest, func RelayImageHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatusCode { return Handle(meta, c, func() (*PreCheckGroupBalanceReq, error) { + if !config.GetBillingEnabled() { + return &PreCheckGroupBalanceReq{}, nil + } + imageRequest, err := getImageRequest(meta, c) if err != nil { return nil, err diff --git a/service/aiproxy/relay/controller/price.go b/service/aiproxy/relay/controller/price.go index b36b2a293..91ae7974e 100644 --- a/service/aiproxy/relay/controller/price.go +++ b/service/aiproxy/relay/controller/price.go @@ -1,21 +1,14 @@ package controller import ( - "github.com/labring/sealos/service/aiproxy/common/config" "github.com/labring/sealos/service/aiproxy/model" ) func GetModelPrice(modelConfig *model.ModelConfig) (float64, float64, bool) { - if !config.GetBillingEnabled() { - return 0, 0, true - } return modelConfig.InputPrice, modelConfig.OutputPrice, true } func GetImageSizePrice(modelConfig *model.ModelConfig, size string) (float64, bool) { - if !config.GetBillingEnabled() { - return 0, false - } if len(modelConfig.ImagePrices) == 0 { return 0, true } diff --git a/service/aiproxy/relay/controller/rerank.go b/service/aiproxy/relay/controller/rerank.go index d377b08e9..b1325d772 100644 --- a/service/aiproxy/relay/controller/rerank.go +++ b/service/aiproxy/relay/controller/rerank.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/labring/sealos/service/aiproxy/common/config" "github.com/labring/sealos/service/aiproxy/relay/meta" relaymodel "github.com/labring/sealos/service/aiproxy/relay/model" "github.com/labring/sealos/service/aiproxy/relay/utils" @@ -13,7 +14,11 @@ import ( func RerankHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatusCode { return Handle(meta, c, func() (*PreCheckGroupBalanceReq, error) { - price, completionPrice, ok := GetModelPrice(meta.ModelConfig) + if !config.GetBillingEnabled() { + return &PreCheckGroupBalanceReq{}, nil + } + + inputPrice, outputPrice, ok := GetModelPrice(meta.ModelConfig) if !ok { return nil, fmt.Errorf("model price not found: %s", meta.OriginModel) } @@ -25,8 +30,8 @@ func RerankHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatusCo return &PreCheckGroupBalanceReq{ InputTokens: rerankPromptTokens(rerankRequest), - InputPrice: price, - OutputPrice: completionPrice, + InputPrice: inputPrice, + OutputPrice: outputPrice, }, nil }) } diff --git a/service/aiproxy/relay/controller/stt.go b/service/aiproxy/relay/controller/stt.go index c30d9dc03..c93eb0ad6 100644 --- a/service/aiproxy/relay/controller/stt.go +++ b/service/aiproxy/relay/controller/stt.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" "github.com/labring/sealos/service/aiproxy/common/audio" + "github.com/labring/sealos/service/aiproxy/common/config" "github.com/labring/sealos/service/aiproxy/middleware" "github.com/labring/sealos/service/aiproxy/relay/meta" relaymodel "github.com/labring/sealos/service/aiproxy/relay/model" @@ -16,7 +17,11 @@ import ( func RelaySTTHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatusCode { return Handle(meta, c, func() (*PreCheckGroupBalanceReq, error) { - price, completionPrice, ok := GetModelPrice(meta.ModelConfig) + if !config.GetBillingEnabled() { + return &PreCheckGroupBalanceReq{}, nil + } + + inputPrice, outputPrice, ok := GetModelPrice(meta.ModelConfig) if !ok { return nil, fmt.Errorf("model price not found: %s", meta.OriginModel) } @@ -37,8 +42,8 @@ func RelaySTTHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatus return &PreCheckGroupBalanceReq{ InputTokens: durationInt, - InputPrice: price, - OutputPrice: completionPrice, + InputPrice: inputPrice, + OutputPrice: outputPrice, }, nil }) } @@ -75,7 +80,7 @@ func getAudioDuration(audioFile *multipart.FileHeader) (float64, error) { } func getDurationFromTempFile(audioFile *multipart.FileHeader) (float64, error) { - tempFile, err := os.CreateTemp("", "audio.wav") + tempFile, err := os.CreateTemp("", "audio") if err != nil { return 0, fmt.Errorf("failed to create temp file: %w", err) } diff --git a/service/aiproxy/relay/controller/text.go b/service/aiproxy/relay/controller/text.go index d260f0c8d..d94b74474 100644 --- a/service/aiproxy/relay/controller/text.go +++ b/service/aiproxy/relay/controller/text.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/gin-gonic/gin" + "github.com/labring/sealos/service/aiproxy/common/config" "github.com/labring/sealos/service/aiproxy/relay/adaptor/openai" "github.com/labring/sealos/service/aiproxy/relay/meta" relaymodel "github.com/labring/sealos/service/aiproxy/relay/model" @@ -12,7 +13,11 @@ import ( func RelayTextHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatusCode { return Handle(meta, c, func() (*PreCheckGroupBalanceReq, error) { - price, completionPrice, ok := GetModelPrice(meta.ModelConfig) + if !config.GetBillingEnabled() { + return &PreCheckGroupBalanceReq{}, nil + } + + inputPrice, outputPrice, ok := GetModelPrice(meta.ModelConfig) if !ok { return nil, fmt.Errorf("model price not found: %s", meta.OriginModel) } @@ -25,8 +30,8 @@ func RelayTextHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatu return &PreCheckGroupBalanceReq{ InputTokens: openai.GetPromptTokens(meta, textRequest), MaxTokens: textRequest.MaxTokens, - InputPrice: price, - OutputPrice: completionPrice, + InputPrice: inputPrice, + OutputPrice: outputPrice, }, nil }) } diff --git a/service/aiproxy/relay/controller/tts.go b/service/aiproxy/relay/controller/tts.go index 8afcf22b8..2298ab923 100644 --- a/service/aiproxy/relay/controller/tts.go +++ b/service/aiproxy/relay/controller/tts.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/gin-gonic/gin" + "github.com/labring/sealos/service/aiproxy/common/config" "github.com/labring/sealos/service/aiproxy/relay/adaptor/openai" "github.com/labring/sealos/service/aiproxy/relay/meta" relaymodel "github.com/labring/sealos/service/aiproxy/relay/model" @@ -12,7 +13,11 @@ import ( func RelayTTSHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatusCode { return Handle(meta, c, func() (*PreCheckGroupBalanceReq, error) { - price, completionPrice, ok := GetModelPrice(meta.ModelConfig) + if !config.GetBillingEnabled() { + return &PreCheckGroupBalanceReq{}, nil + } + + inputPrice, outputPrice, ok := GetModelPrice(meta.ModelConfig) if !ok { return nil, fmt.Errorf("model price not found: %s", meta.OriginModel) } @@ -24,8 +29,8 @@ func RelayTTSHelper(meta *meta.Meta, c *gin.Context) *relaymodel.ErrorWithStatus return &PreCheckGroupBalanceReq{ InputTokens: openai.CountTokenText(ttsRequest.Input, meta.ActualModel), - InputPrice: price, - OutputPrice: completionPrice, + InputPrice: inputPrice, + OutputPrice: outputPrice, }, nil }) }