Skip to content

Commit

Permalink
feat: impl disable billing (#5431)
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 authored Mar 2, 2025
1 parent 050a861 commit 2d34319
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 26 deletions.
8 changes: 6 additions & 2 deletions service/aiproxy/model/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
14 changes: 10 additions & 4 deletions service/aiproxy/relay/adaptor/openai/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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")
}
5 changes: 5 additions & 0 deletions service/aiproxy/relay/controller/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions service/aiproxy/relay/controller/price.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
11 changes: 8 additions & 3 deletions service/aiproxy/relay/controller/rerank.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ 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"
)

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)
}
Expand All @@ -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
})
}
Expand Down
13 changes: 9 additions & 4 deletions service/aiproxy/relay/controller/stt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ 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"
)

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)
}
Expand All @@ -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
})
}
Expand Down Expand Up @@ -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)
}
Expand Down
11 changes: 8 additions & 3 deletions service/aiproxy/relay/controller/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand All @@ -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
})
}
11 changes: 8 additions & 3 deletions service/aiproxy/relay/controller/tts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand All @@ -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
})
}

0 comments on commit 2d34319

Please sign in to comment.