From 4597c5776fb7cb19e5a20727fca1176cf5bb4663 Mon Sep 17 00:00:00 2001 From: "jinqi.guo" Date: Thu, 5 Sep 2024 11:27:26 +0800 Subject: [PATCH 1/2] fix: cannot test mapping model --- controller/channel-test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/controller/channel-test.go b/controller/channel-test.go index f8327284c0..019834d570 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -70,15 +70,14 @@ func testChannel(channel *model.Channel, request *relaymodel.GeneralOpenAIReques } adaptor.Init(meta) modelName := request.Model - modelMap := channel.GetModelMapping() if modelName == "" || !strings.Contains(channel.Models, modelName) { modelNames := strings.Split(channel.Models, ",") if len(modelNames) > 0 { modelName = modelNames[0] } - if modelMap != nil && modelMap[modelName] != "" { - modelName = modelMap[modelName] - } + } + if modelMap := channel.GetModelMapping(); modelMap != nil && modelMap[modelName] != "" { + modelName = modelMap[modelName] } meta.OriginModelName, meta.ActualModelName = request.Model, modelName request.Model = modelName From 17475478d7bcec9632519bb22343b423cbf74986 Mon Sep 17 00:00:00 2001 From: "jinqi.guo" Date: Fri, 6 Sep 2024 18:11:51 +0800 Subject: [PATCH 2/2] fix: postgresl use COALESCE replace ifnull --- model/log.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/model/log.go b/model/log.go index 6fba776a53..58fdd513cd 100644 --- a/model/log.go +++ b/model/log.go @@ -3,6 +3,7 @@ package model import ( "context" "fmt" + "github.com/songquanpeng/one-api/common" "github.com/songquanpeng/one-api/common/config" "github.com/songquanpeng/one-api/common/helper" @@ -152,7 +153,11 @@ func SearchUserLogs(userId int, keyword string) (logs []*Log, err error) { } func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string, channel int) (quota int64) { - tx := LOG_DB.Table("logs").Select("ifnull(sum(quota),0)") + ifnull := "ifnull" + if common.UsingPostgreSQL { + ifnull = "COALESCE" + } + tx := LOG_DB.Table("logs").Select(fmt.Sprintf("%s(sum(quota),0)", ifnull)) if username != "" { tx = tx.Where("username = ?", username) } @@ -176,7 +181,11 @@ func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelNa } func SumUsedToken(logType int, startTimestamp int64, endTimestamp int64, modelName string, username string, tokenName string) (token int) { - tx := LOG_DB.Table("logs").Select("ifnull(sum(prompt_tokens),0) + ifnull(sum(completion_tokens),0)") + ifnull := "ifnull" + if common.UsingPostgreSQL { + ifnull = "COALESCE" + } + tx := LOG_DB.Table("logs").Select(fmt.Sprintf("%s(sum(prompt_tokens),0) + %s(sum(completion_tokens),0)", ifnull, ifnull)) if username != "" { tx = tx.Where("username = ?", username) }