Skip to content

Commit 974df5e

Browse files
authored
Merge pull request #2222 from xyfacai/main
fix: 未设置价格模型不会被拉取,除非设置自用模式
2 parents 4419be9 + 06cd774 commit 974df5e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

controller/model.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/QuantumNous/new-api/relay/channel/moonshot"
1717
relaycommon "github.com/QuantumNous/new-api/relay/common"
1818
"github.com/QuantumNous/new-api/service"
19+
"github.com/QuantumNous/new-api/setting/operation_setting"
20+
"github.com/QuantumNous/new-api/setting/ratio_setting"
1921
"github.com/gin-gonic/gin"
2022
"github.com/samber/lo"
2123
)
@@ -109,6 +111,17 @@ func init() {
109111
func ListModels(c *gin.Context, modelType int) {
110112
userOpenAiModels := make([]dto.OpenAIModels, 0)
111113

114+
acceptUnsetRatioModel := operation_setting.SelfUseModeEnabled
115+
if !acceptUnsetRatioModel {
116+
userId := c.GetInt("id")
117+
if userId > 0 {
118+
userSettings, _ := model.GetUserSetting(userId, false)
119+
if userSettings.AcceptUnsetRatioModel {
120+
acceptUnsetRatioModel = true
121+
}
122+
}
123+
}
124+
112125
modelLimitEnable := common.GetContextKeyBool(c, constant.ContextKeyTokenModelLimitEnabled)
113126
if modelLimitEnable {
114127
s, ok := common.GetContextKey(c, constant.ContextKeyTokenModelLimit)
@@ -119,6 +132,12 @@ func ListModels(c *gin.Context, modelType int) {
119132
tokenModelLimit = map[string]bool{}
120133
}
121134
for allowModel, _ := range tokenModelLimit {
135+
if !acceptUnsetRatioModel {
136+
_, _, exist := ratio_setting.GetModelRatioOrPrice(allowModel)
137+
if !exist {
138+
continue
139+
}
140+
}
122141
if oaiModel, ok := openAIModelsMap[allowModel]; ok {
123142
oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(allowModel)
124143
userOpenAiModels = append(userOpenAiModels, oaiModel)
@@ -161,6 +180,12 @@ func ListModels(c *gin.Context, modelType int) {
161180
models = model.GetGroupEnabledModels(group)
162181
}
163182
for _, modelName := range models {
183+
if !acceptUnsetRatioModel {
184+
_, _, exist := ratio_setting.GetModelRatioOrPrice(modelName)
185+
if !exist {
186+
continue
187+
}
188+
}
164189
if oaiModel, ok := openAIModelsMap[modelName]; ok {
165190
oaiModel.SupportedEndpointTypes = model.GetModelSupportEndpointTypes(modelName)
166191
userOpenAiModels = append(userOpenAiModels, oaiModel)
@@ -175,6 +200,7 @@ func ListModels(c *gin.Context, modelType int) {
175200
}
176201
}
177202
}
203+
178204
switch modelType {
179205
case constant.ChannelTypeAnthropic:
180206
useranthropicModels := make([]dto.AnthropicModel, len(userOpenAiModels))

setting/ratio_setting/model_ratio.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,16 @@ func FormatMatchingModelName(name string) string {
823823
}
824824
return name
825825
}
826+
827+
// result: 倍率or价格, usePrice, exist
828+
func GetModelRatioOrPrice(model string) (float64, bool, bool) { // price or ratio
829+
price, usePrice := GetModelPrice(model, false)
830+
if usePrice {
831+
return price, true, true
832+
}
833+
modelRatio, success, _ := GetModelRatio(model)
834+
if success {
835+
return modelRatio, false, true
836+
}
837+
return 37.5, false, false
838+
}

0 commit comments

Comments
 (0)