feat: impl disable billing (#5431)

This commit is contained in:
zijiren
2025-03-02 22:48:55 +08:00
committed by GitHub
parent 050a861b99
commit 2d3431901a
8 changed files with 54 additions and 26 deletions
+6 -2
View File
@@ -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)
}
}
+10 -4
View File
@@ -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")
}
@@ -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
@@ -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
}
+8 -3
View File
@@ -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
})
}
+9 -4
View File
@@ -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)
}
+8 -3
View File
@@ -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
})
}
+8 -3
View File
@@ -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
})
}