From 2f9faba40df759175420d40820d5cd9902c32b95 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sat, 1 Mar 2025 15:27:32 +0800 Subject: [PATCH 01/15] fix: Improve error handling for model ratio and price validation #800 --- controller/channel-test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controller/channel-test.go b/controller/channel-test.go index f7b73f6a..23922073 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -147,8 +147,8 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr } modelPrice, usePrice := common.GetModelPrice(testModel, false) modelRatio, success := common.GetModelRatio(testModel) - if !success { - return fmt.Errorf("模型 %s 倍率未设置", testModel), nil + if !usePrice && !success { + return fmt.Errorf("模型 %s 倍率和价格均未设置", testModel), nil } completionRatio := common.GetCompletionRatio(testModel) ratio := modelRatio From ce1854847b15f810ccf77a67be1e54114696c05f Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sat, 1 Mar 2025 17:02:31 +0800 Subject: [PATCH 02/15] fix: Enhance error message for missing model ratio configuration --- relay/helper/price.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/relay/helper/price.go b/relay/helper/price.go index 31d4e9cf..1f4a5b3c 100644 --- a/relay/helper/price.go +++ b/relay/helper/price.go @@ -29,7 +29,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens var success bool modelRatio, success = common.GetModelRatio(info.OriginModelName) if !success { - return PriceData{}, fmt.Errorf("model %s ratio or price not found, please contact admin", info.OriginModelName) + return PriceData{}, fmt.Errorf("模型 %s 倍率或价格未配置, 请联系管理员设置;Model %s ratio or price not set, please contact administrator to set", info.OriginModelName, info.OriginModelName) } ratio := modelRatio * groupRatio preConsumedQuota = int(float64(preConsumedTokens) * ratio) From 7dbb6b017c4efe9457683bcba56fe9d24b22e5d9 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sat, 1 Mar 2025 21:13:48 +0800 Subject: [PATCH 03/15] feat: Add self-use mode for model ratio and price configuration - Introduce `SelfUseModeEnabled` setting to allow flexible model ratio configuration - Update error handling to provide more informative messages when model ratios are not set - Modify pricing and relay logic to support self-use mode - Add UI toggle for enabling self-use mode in operation settings - Implement fallback mechanism for model ratios when self-use mode is enabled --- controller/channel-test.go | 9 +++++---- controller/pricing.go | 5 ++--- model/option.go | 15 ++++++++------ model/pricing.go | 7 ++++--- relay/helper/price.go | 10 +++++++--- relay/relay-mj.go | 8 ++++---- relay/relay-text.go | 2 +- relay/relay_task.go | 4 ++-- relay/websocket.go | 4 ++-- service/quota.go | 20 +++++++++---------- service/token_counter.go | 3 ++- {common => setting}/model-ratio.go | 17 ++++++++-------- setting/operation_setting.go | 1 + web/src/components/OperationSetting.js | 1 + .../Setting/Operation/SettingsGeneral.js | 17 ++++++++++++++++ 15 files changed, 76 insertions(+), 47 deletions(-) rename {common => setting}/model-ratio.go (97%) diff --git a/controller/channel-test.go b/controller/channel-test.go index 23922073..68e4d939 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -18,6 +18,7 @@ import ( relaycommon "one-api/relay/common" "one-api/relay/constant" "one-api/service" + "one-api/setting" "strconv" "strings" "sync" @@ -145,12 +146,12 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr if err != nil { return err, nil } - modelPrice, usePrice := common.GetModelPrice(testModel, false) - modelRatio, success := common.GetModelRatio(testModel) + modelPrice, usePrice := setting.GetModelPrice(testModel, false) + modelRatio, success := setting.GetModelRatio(testModel) if !usePrice && !success { - return fmt.Errorf("模型 %s 倍率和价格均未设置", testModel), nil + return fmt.Errorf("模型 %s 倍率和价格均未设置,请设置或者开启自用模式", testModel), nil } - completionRatio := common.GetCompletionRatio(testModel) + completionRatio := setting.GetCompletionRatio(testModel) ratio := modelRatio quota := 0 if !usePrice { diff --git a/controller/pricing.go b/controller/pricing.go index d7af5a4c..97f27490 100644 --- a/controller/pricing.go +++ b/controller/pricing.go @@ -2,7 +2,6 @@ package controller import ( "github.com/gin-gonic/gin" - "one-api/common" "one-api/model" "one-api/setting" ) @@ -40,7 +39,7 @@ func GetPricing(c *gin.Context) { } func ResetModelRatio(c *gin.Context) { - defaultStr := common.DefaultModelRatio2JSONString() + defaultStr := setting.DefaultModelRatio2JSONString() err := model.UpdateOption("ModelRatio", defaultStr) if err != nil { c.JSON(200, gin.H{ @@ -49,7 +48,7 @@ func ResetModelRatio(c *gin.Context) { }) return } - err = common.UpdateModelRatioByJSONString(defaultStr) + err = setting.UpdateModelRatioByJSONString(defaultStr) if err != nil { c.JSON(200, gin.H{ "success": false, diff --git a/model/option.go b/model/option.go index 64d15ca8..5426c28d 100644 --- a/model/option.go +++ b/model/option.go @@ -91,11 +91,11 @@ func InitOptionMap() { common.OptionMap["ModelRequestRateLimitCount"] = strconv.Itoa(setting.ModelRequestRateLimitCount) common.OptionMap["ModelRequestRateLimitDurationMinutes"] = strconv.Itoa(setting.ModelRequestRateLimitDurationMinutes) common.OptionMap["ModelRequestRateLimitSuccessCount"] = strconv.Itoa(setting.ModelRequestRateLimitSuccessCount) - common.OptionMap["ModelRatio"] = common.ModelRatio2JSONString() - common.OptionMap["ModelPrice"] = common.ModelPrice2JSONString() + common.OptionMap["ModelRatio"] = setting.ModelRatio2JSONString() + common.OptionMap["ModelPrice"] = setting.ModelPrice2JSONString() common.OptionMap["GroupRatio"] = setting.GroupRatio2JSONString() common.OptionMap["UserUsableGroups"] = setting.UserUsableGroups2JSONString() - common.OptionMap["CompletionRatio"] = common.CompletionRatio2JSONString() + common.OptionMap["CompletionRatio"] = setting.CompletionRatio2JSONString() common.OptionMap["TopUpLink"] = common.TopUpLink common.OptionMap["ChatLink"] = common.ChatLink common.OptionMap["ChatLink2"] = common.ChatLink2 @@ -111,6 +111,7 @@ func InitOptionMap() { common.OptionMap["MjActionCheckSuccessEnabled"] = strconv.FormatBool(setting.MjActionCheckSuccessEnabled) common.OptionMap["CheckSensitiveEnabled"] = strconv.FormatBool(setting.CheckSensitiveEnabled) common.OptionMap["DemoSiteEnabled"] = strconv.FormatBool(setting.DemoSiteEnabled) + common.OptionMap["SelfUseModeEnabled"] = strconv.FormatBool(setting.SelfUseModeEnabled) common.OptionMap["ModelRequestRateLimitEnabled"] = strconv.FormatBool(setting.ModelRequestRateLimitEnabled) common.OptionMap["CheckSensitiveOnPromptEnabled"] = strconv.FormatBool(setting.CheckSensitiveOnPromptEnabled) common.OptionMap["StopOnSensitiveEnabled"] = strconv.FormatBool(setting.StopOnSensitiveEnabled) @@ -243,6 +244,8 @@ func updateOptionMap(key string, value string) (err error) { setting.CheckSensitiveEnabled = boolValue case "DemoSiteEnabled": setting.DemoSiteEnabled = boolValue + case "SelfUseModeEnabled": + setting.SelfUseModeEnabled = boolValue case "CheckSensitiveOnPromptEnabled": setting.CheckSensitiveOnPromptEnabled = boolValue case "ModelRequestRateLimitEnabled": @@ -340,15 +343,15 @@ func updateOptionMap(key string, value string) (err error) { case "DataExportDefaultTime": common.DataExportDefaultTime = value case "ModelRatio": - err = common.UpdateModelRatioByJSONString(value) + err = setting.UpdateModelRatioByJSONString(value) case "GroupRatio": err = setting.UpdateGroupRatioByJSONString(value) case "UserUsableGroups": err = setting.UpdateUserUsableGroupsByJSONString(value) case "CompletionRatio": - err = common.UpdateCompletionRatioByJSONString(value) + err = setting.UpdateCompletionRatioByJSONString(value) case "ModelPrice": - err = common.UpdateModelPriceByJSONString(value) + err = setting.UpdateModelPriceByJSONString(value) case "TopUpLink": common.TopUpLink = value case "ChatLink": diff --git a/model/pricing.go b/model/pricing.go index fc709ce4..2d0aa1b7 100644 --- a/model/pricing.go +++ b/model/pricing.go @@ -2,6 +2,7 @@ package model import ( "one-api/common" + "one-api/setting" "sync" "time" ) @@ -64,14 +65,14 @@ func updatePricing() { ModelName: model, EnableGroup: groups, } - modelPrice, findPrice := common.GetModelPrice(model, false) + modelPrice, findPrice := setting.GetModelPrice(model, false) if findPrice { pricing.ModelPrice = modelPrice pricing.QuotaType = 1 } else { - modelRatio, _ := common.GetModelRatio(model) + modelRatio, _ := setting.GetModelRatio(model) pricing.ModelRatio = modelRatio - pricing.CompletionRatio = common.GetCompletionRatio(model) + pricing.CompletionRatio = setting.GetCompletionRatio(model) pricing.QuotaType = 0 } pricingMap = append(pricingMap, pricing) diff --git a/relay/helper/price.go b/relay/helper/price.go index 1f4a5b3c..97cbf162 100644 --- a/relay/helper/price.go +++ b/relay/helper/price.go @@ -17,7 +17,7 @@ type PriceData struct { } func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens int, maxTokens int) (PriceData, error) { - modelPrice, usePrice := common.GetModelPrice(info.OriginModelName, false) + modelPrice, usePrice := setting.GetModelPrice(info.OriginModelName, false) groupRatio := setting.GetGroupRatio(info.Group) var preConsumedQuota int var modelRatio float64 @@ -27,9 +27,13 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens preConsumedTokens = promptTokens + maxTokens } var success bool - modelRatio, success = common.GetModelRatio(info.OriginModelName) + modelRatio, success = setting.GetModelRatio(info.OriginModelName) if !success { - return PriceData{}, fmt.Errorf("模型 %s 倍率或价格未配置, 请联系管理员设置;Model %s ratio or price not set, please contact administrator to set", info.OriginModelName, info.OriginModelName) + if info.UserId == 1 { + return PriceData{}, fmt.Errorf("模型 %s 倍率或价格未配置,请设置或开始自用模式;Model %s ratio or price not set, please set or start self-use mode", info.OriginModelName, info.OriginModelName) + } else { + return PriceData{}, fmt.Errorf("模型 %s 倍率或价格未配置, 请联系管理员设置;Model %s ratio or price not set, please contact administrator to set", info.OriginModelName, info.OriginModelName) + } } ratio := modelRatio * groupRatio preConsumedQuota = int(float64(preConsumedTokens) * ratio) diff --git a/relay/relay-mj.go b/relay/relay-mj.go index 57de8d10..8baf033a 100644 --- a/relay/relay-mj.go +++ b/relay/relay-mj.go @@ -157,10 +157,10 @@ func RelaySwapFace(c *gin.Context) *dto.MidjourneyResponse { return service.MidjourneyErrorWrapper(constant.MjRequestError, "sour_base64_and_target_base64_is_required") } modelName := service.CoverActionToModelName(constant.MjActionSwapFace) - modelPrice, success := common.GetModelPrice(modelName, true) + modelPrice, success := setting.GetModelPrice(modelName, true) // 如果没有配置价格,则使用默认价格 if !success { - defaultPrice, ok := common.GetDefaultModelRatioMap()[modelName] + defaultPrice, ok := setting.GetDefaultModelRatioMap()[modelName] if !ok { modelPrice = 0.1 } else { @@ -463,10 +463,10 @@ func RelayMidjourneySubmit(c *gin.Context, relayMode int) *dto.MidjourneyRespons fullRequestURL := fmt.Sprintf("%s%s", baseURL, requestURL) modelName := service.CoverActionToModelName(midjRequest.Action) - modelPrice, success := common.GetModelPrice(modelName, true) + modelPrice, success := setting.GetModelPrice(modelName, true) // 如果没有配置价格,则使用默认价格 if !success { - defaultPrice, ok := common.GetDefaultModelRatioMap()[modelName] + defaultPrice, ok := setting.GetDefaultModelRatioMap()[modelName] if !ok { modelPrice = 0.1 } else { diff --git a/relay/relay-text.go b/relay/relay-text.go index eb331e25..bf6c5fd3 100644 --- a/relay/relay-text.go +++ b/relay/relay-text.go @@ -311,7 +311,7 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, modelName := relayInfo.OriginModelName tokenName := ctx.GetString("token_name") - completionRatio := common.GetCompletionRatio(modelName) + completionRatio := setting.GetCompletionRatio(modelName) ratio := priceData.ModelRatio * priceData.GroupRatio modelRatio := priceData.ModelRatio groupRatio := priceData.GroupRatio diff --git a/relay/relay_task.go b/relay/relay_task.go index 591ad3bb..ab35d3e8 100644 --- a/relay/relay_task.go +++ b/relay/relay_task.go @@ -37,9 +37,9 @@ func RelayTaskSubmit(c *gin.Context, relayMode int) (taskErr *dto.TaskError) { } modelName := service.CoverTaskActionToModelName(platform, relayInfo.Action) - modelPrice, success := common.GetModelPrice(modelName, true) + modelPrice, success := setting.GetModelPrice(modelName, true) if !success { - defaultPrice, ok := common.GetDefaultModelRatioMap()[modelName] + defaultPrice, ok := setting.GetDefaultModelRatioMap()[modelName] if !ok { modelPrice = 0.1 } else { diff --git a/relay/websocket.go b/relay/websocket.go index 2dac60af..b0636057 100644 --- a/relay/websocket.go +++ b/relay/websocket.go @@ -39,7 +39,7 @@ func WssHelper(c *gin.Context, ws *websocket.Conn) (openaiErr *dto.OpenAIErrorWi } } //relayInfo.UpstreamModelName = textRequest.Model - modelPrice, getModelPriceSuccess := common.GetModelPrice(relayInfo.UpstreamModelName, false) + modelPrice, getModelPriceSuccess := setting.GetModelPrice(relayInfo.UpstreamModelName, false) groupRatio := setting.GetGroupRatio(relayInfo.Group) var preConsumedQuota int @@ -65,7 +65,7 @@ func WssHelper(c *gin.Context, ws *websocket.Conn) (openaiErr *dto.OpenAIErrorWi //if realtimeEvent.Session.MaxResponseOutputTokens != 0 { // preConsumedTokens = promptTokens + int(realtimeEvent.Session.MaxResponseOutputTokens) //} - modelRatio, _ = common.GetModelRatio(relayInfo.UpstreamModelName) + modelRatio, _ = setting.GetModelRatio(relayInfo.UpstreamModelName) ratio = modelRatio * groupRatio preConsumedQuota = int(float64(preConsumedTokens) * ratio) } else { diff --git a/service/quota.go b/service/quota.go index 9ce2858d..b3412c1e 100644 --- a/service/quota.go +++ b/service/quota.go @@ -38,9 +38,9 @@ func calculateAudioQuota(info QuotaInfo) int { return int(info.ModelPrice * common.QuotaPerUnit * info.GroupRatio) } - completionRatio := common.GetCompletionRatio(info.ModelName) - audioRatio := common.GetAudioRatio(info.ModelName) - audioCompletionRatio := common.GetAudioCompletionRatio(info.ModelName) + completionRatio := setting.GetCompletionRatio(info.ModelName) + audioRatio := setting.GetAudioRatio(info.ModelName) + audioCompletionRatio := setting.GetAudioCompletionRatio(info.ModelName) ratio := info.GroupRatio * info.ModelRatio quota := info.InputDetails.TextTokens + int(math.Round(float64(info.OutputDetails.TextTokens)*completionRatio)) @@ -75,7 +75,7 @@ func PreWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, usag audioInputTokens := usage.InputTokenDetails.AudioTokens audioOutTokens := usage.OutputTokenDetails.AudioTokens groupRatio := setting.GetGroupRatio(relayInfo.Group) - modelRatio, _ := common.GetModelRatio(modelName) + modelRatio, _ := setting.GetModelRatio(modelName) quotaInfo := QuotaInfo{ InputDetails: TokenDetails{ @@ -122,9 +122,9 @@ func PostWssConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, mod audioOutTokens := usage.OutputTokenDetails.AudioTokens tokenName := ctx.GetString("token_name") - completionRatio := common.GetCompletionRatio(modelName) - audioRatio := common.GetAudioRatio(relayInfo.OriginModelName) - audioCompletionRatio := common.GetAudioCompletionRatio(modelName) + completionRatio := setting.GetCompletionRatio(modelName) + audioRatio := setting.GetAudioRatio(relayInfo.OriginModelName) + audioCompletionRatio := setting.GetAudioCompletionRatio(modelName) quotaInfo := QuotaInfo{ InputDetails: TokenDetails{ @@ -184,9 +184,9 @@ func PostAudioConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo, audioOutTokens := usage.CompletionTokenDetails.AudioTokens tokenName := ctx.GetString("token_name") - completionRatio := common.GetCompletionRatio(relayInfo.OriginModelName) - audioRatio := common.GetAudioRatio(relayInfo.OriginModelName) - audioCompletionRatio := common.GetAudioCompletionRatio(relayInfo.OriginModelName) + completionRatio := setting.GetCompletionRatio(relayInfo.OriginModelName) + audioRatio := setting.GetAudioRatio(relayInfo.OriginModelName) + audioCompletionRatio := setting.GetAudioCompletionRatio(relayInfo.OriginModelName) modelRatio := priceData.ModelRatio groupRatio := priceData.GroupRatio diff --git a/service/token_counter.go b/service/token_counter.go index aa62bc6e..e868beb4 100644 --- a/service/token_counter.go +++ b/service/token_counter.go @@ -10,6 +10,7 @@ import ( "one-api/constant" "one-api/dto" relaycommon "one-api/relay/common" + "one-api/setting" "strings" "unicode/utf8" @@ -32,7 +33,7 @@ func InitTokenEncoders() { if err != nil { common.FatalLog(fmt.Sprintf("failed to get gpt-4o token encoder: %s", err.Error())) } - for model, _ := range common.GetDefaultModelRatioMap() { + for model, _ := range setting.GetDefaultModelRatioMap() { if strings.HasPrefix(model, "gpt-3.5") { tokenEncoderMap[model] = cl100TokenEncoder } else if strings.HasPrefix(model, "gpt-4") { diff --git a/common/model-ratio.go b/setting/model-ratio.go similarity index 97% rename from common/model-ratio.go rename to setting/model-ratio.go index 03681172..0606f107 100644 --- a/common/model-ratio.go +++ b/setting/model-ratio.go @@ -1,7 +1,8 @@ -package common +package setting import ( "encoding/json" + "one-api/common" "strings" "sync" ) @@ -261,7 +262,7 @@ func ModelPrice2JSONString() string { GetModelPriceMap() jsonBytes, err := json.Marshal(modelPriceMap) if err != nil { - SysError("error marshalling model price: " + err.Error()) + common.SysError("error marshalling model price: " + err.Error()) } return string(jsonBytes) } @@ -285,7 +286,7 @@ func GetModelPrice(name string, printErr bool) (float64, bool) { price, ok := modelPriceMap[name] if !ok { if printErr { - SysError("model price not found: " + name) + common.SysError("model price not found: " + name) } return -1, false } @@ -305,7 +306,7 @@ func ModelRatio2JSONString() string { GetModelRatioMap() jsonBytes, err := json.Marshal(modelRatioMap) if err != nil { - SysError("error marshalling model ratio: " + err.Error()) + common.SysError("error marshalling model ratio: " + err.Error()) } return string(jsonBytes) } @@ -324,8 +325,8 @@ func GetModelRatio(name string) (float64, bool) { } ratio, ok := modelRatioMap[name] if !ok { - SysError("model ratio not found: " + name) - return 37.5, false + common.SysError("model ratio not found: " + name) + return 37.5, SelfUseModeEnabled } return ratio, true } @@ -333,7 +334,7 @@ func GetModelRatio(name string) (float64, bool) { func DefaultModelRatio2JSONString() string { jsonBytes, err := json.Marshal(defaultModelRatio) if err != nil { - SysError("error marshalling model ratio: " + err.Error()) + common.SysError("error marshalling model ratio: " + err.Error()) } return string(jsonBytes) } @@ -355,7 +356,7 @@ func CompletionRatio2JSONString() string { GetCompletionRatioMap() jsonBytes, err := json.Marshal(CompletionRatio) if err != nil { - SysError("error marshalling completion ratio: " + err.Error()) + common.SysError("error marshalling completion ratio: " + err.Error()) } return string(jsonBytes) } diff --git a/setting/operation_setting.go b/setting/operation_setting.go index 4940d0fc..d4275168 100644 --- a/setting/operation_setting.go +++ b/setting/operation_setting.go @@ -3,6 +3,7 @@ package setting import "strings" var DemoSiteEnabled = false +var SelfUseModeEnabled = false var AutomaticDisableKeywords = []string{ "Your credit balance is too low", diff --git a/web/src/components/OperationSetting.js b/web/src/components/OperationSetting.js index 19f2dbe6..5c51c751 100644 --- a/web/src/components/OperationSetting.js +++ b/web/src/components/OperationSetting.js @@ -60,6 +60,7 @@ const OperationSetting = () => { RetryTimes: 0, Chats: "[]", DemoSiteEnabled: false, + SelfUseModeEnabled: false, AutomaticDisableKeywords: '', }); diff --git a/web/src/pages/Setting/Operation/SettingsGeneral.js b/web/src/pages/Setting/Operation/SettingsGeneral.js index 1c98d33e..e46e7db2 100644 --- a/web/src/pages/Setting/Operation/SettingsGeneral.js +++ b/web/src/pages/Setting/Operation/SettingsGeneral.js @@ -22,6 +22,7 @@ export default function GeneralSettings(props) { DisplayTokenStatEnabled: false, DefaultCollapseSidebar: false, DemoSiteEnabled: false, + SelfUseModeEnabled: false, }); const refForm = useRef(); const [inputsRow, setInputsRow] = useState(inputs); @@ -205,6 +206,22 @@ export default function GeneralSettings(props) { } /> + + + setInputs({ + ...inputs, + SelfUseModeEnabled: value + }) + } + /> + + + + + + + {t('启动时间')}:{getStartTimeString()} + + + + {/* 通用设置 */}
{
- {/* setShowUpdateModal(false)}*/} - {/* onOpen={() => setShowUpdateModal(true)}*/} - {/* open={showUpdateModal}*/} - {/*>*/} - {/* 新版本:{updateData.tag_name}*/} - {/* */} - {/* */} - {/*
*/} - {/*
*/} - {/*
*/} - {/* */} - {/* */} - {/* {*/} - {/* setShowUpdateModal(false);*/} - {/* openGitHubRelease();*/} - {/* }}*/} - {/* />*/} - {/* */} - {/**/} + setShowUpdateModal(false)} + footer={[ + + ]} + > +
+
); }; diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 750f762f..3c7d368c 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -1320,5 +1320,20 @@ "模型倍率和补全倍率同时设置": "Both model ratio and completion ratio are set", "自用模式": "Self-use mode", "开启后不限制:必须设置模型倍率": "After enabling, no limit: must set model ratio", - "演示站点模式": "Demo site mode" + "演示站点模式": "Demo site mode", + "当前版本": "Current version", + "Gemini设置": "Gemini settings", + "Gemini安全设置": "Gemini safety settings", + "default为默认设置,可单独设置每个分类的安全等级": "\"default\" is the default setting, and each category can be set separately", + "Gemini版本设置": "Gemini version settings", + "default为默认设置,可单独设置每个模型的版本": "\"default\" is the default setting, and each model can be set separately", + "Claude设置": "Claude settings", + "Claude请求头覆盖": "Claude request header override", + "示例": "Example", + "缺省 MaxTokens": "Default MaxTokens", + "启用Claude思考适配(-thinking后缀)": "Enable Claude thinking adaptation (-thinking suffix)", + "Claude思考适配 BudgetTokens = MaxTokens * BudgetTokens 百分比": "Claude thinking adaptation BudgetTokens = MaxTokens * BudgetTokens percentage", + "思考适配 BudgetTokens 百分比": "Thinking adaptation BudgetTokens percentage", + "0.1-1之间的小数": "Decimal between 0.1 and 1", + "模型相关设置": "Model related settings" } From 816e831a2e1c04e3f8a67bc23493efb699172c2f Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 2 Mar 2025 01:35:50 +0800 Subject: [PATCH 08/15] feat: Persist models expanded state in PersonalSetting component --- web/src/components/PersonalSetting.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/web/src/components/PersonalSetting.js b/web/src/components/PersonalSetting.js index 49a0784c..7e802914 100644 --- a/web/src/components/PersonalSetting.js +++ b/web/src/components/PersonalSetting.js @@ -69,7 +69,11 @@ const PersonalSetting = () => { const [models, setModels] = useState([]); const [openTransfer, setOpenTransfer] = useState(false); const [transferAmount, setTransferAmount] = useState(0); - const [isModelsExpanded, setIsModelsExpanded] = useState(false); + const [isModelsExpanded, setIsModelsExpanded] = useState(() => { + // Initialize from localStorage if available + const savedState = localStorage.getItem('modelsExpanded'); + return savedState ? JSON.parse(savedState) : false; + }); const MODELS_DISPLAY_COUNT = 10; // 默认显示的模型数量 const [notificationSettings, setNotificationSettings] = useState({ warningType: 'email', @@ -124,6 +128,11 @@ const PersonalSetting = () => { } }, [userState?.user?.setting]); + // Save models expanded state to localStorage whenever it changes + useEffect(() => { + localStorage.setItem('modelsExpanded', JSON.stringify(isModelsExpanded)); + }, [isModelsExpanded]); + const handleInputChange = (name, value) => { setInputs((inputs) => ({...inputs, [name]: value})); }; From d042a1bd55b67f225a1bc3562efc74fd073c1dea Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 2 Mar 2025 15:47:12 +0800 Subject: [PATCH 09/15] refactor: Improve channel testing and model price handling --- controller/channel-test.go | 55 ++++++++++++++++---------------------- relay/helper/price.go | 4 +++ 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/controller/channel-test.go b/controller/channel-test.go index 68e4d939..cf253900 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -17,8 +17,8 @@ import ( "one-api/relay" relaycommon "one-api/relay/common" "one-api/relay/constant" + "one-api/relay/helper" "one-api/service" - "one-api/setting" "strconv" "strings" "sync" @@ -73,18 +73,6 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr } } - modelMapping := *channel.ModelMapping - if modelMapping != "" && modelMapping != "{}" { - modelMap := make(map[string]string) - err := json.Unmarshal([]byte(modelMapping), &modelMap) - if err != nil { - return err, service.OpenAIErrorWrapperLocal(err, "unmarshal_model_mapping_failed", http.StatusInternalServerError) - } - if modelMap[testModel] != "" { - testModel = modelMap[testModel] - } - } - cache, err := model.GetUserCache(1) if err != nil { return err, nil @@ -98,7 +86,13 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr middleware.SetupContextForSelectedChannel(c, channel, testModel) - meta := relaycommon.GenRelayInfo(c) + info := relaycommon.GenRelayInfo(c) + + err = helper.ModelMappedHelper(c, info) + if err != nil { + return err, nil + } + apiType, _ := constant.ChannelType2APIType(channel.Type) adaptor := relay.GetAdaptor(apiType) if adaptor == nil { @@ -106,12 +100,12 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr } request := buildTestRequest(testModel) - meta.UpstreamModelName = testModel - common.SysLog(fmt.Sprintf("testing channel %d with model %s , meta %v ", channel.Id, testModel, meta)) + info.OriginModelName = testModel + common.SysLog(fmt.Sprintf("testing channel %d with model %s , info %v ", channel.Id, testModel, info)) - adaptor.Init(meta) + adaptor.Init(info) - convertedRequest, err := adaptor.ConvertRequest(c, meta, request) + convertedRequest, err := adaptor.ConvertRequest(c, info, request) if err != nil { return err, nil } @@ -121,7 +115,7 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr } requestBody := bytes.NewBuffer(jsonData) c.Request.Body = io.NopCloser(requestBody) - resp, err := adaptor.DoRequest(c, meta, requestBody) + resp, err := adaptor.DoRequest(c, info, requestBody) if err != nil { return err, nil } @@ -133,7 +127,7 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr return fmt.Errorf("status code %d: %s", httpResp.StatusCode, err.Error.Message), err } } - usageA, respErr := adaptor.DoResponse(c, httpResp, meta) + usageA, respErr := adaptor.DoResponse(c, httpResp, info) if respErr != nil { return fmt.Errorf("%s", respErr.Error.Message), respErr } @@ -146,27 +140,24 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr if err != nil { return err, nil } - modelPrice, usePrice := setting.GetModelPrice(testModel, false) - modelRatio, success := setting.GetModelRatio(testModel) - if !usePrice && !success { - return fmt.Errorf("模型 %s 倍率和价格均未设置,请设置或者开启自用模式", testModel), nil + priceData, err := helper.ModelPriceHelper(c, info, usage.PromptTokens, int(request.MaxTokens)) + if err != nil { + return err, nil } - completionRatio := setting.GetCompletionRatio(testModel) - ratio := modelRatio quota := 0 - if !usePrice { - quota = usage.PromptTokens + int(math.Round(float64(usage.CompletionTokens)*completionRatio)) - quota = int(math.Round(float64(quota) * ratio)) - if ratio != 0 && quota <= 0 { + if !priceData.UsePrice { + quota = usage.PromptTokens + int(math.Round(float64(usage.CompletionTokens)*priceData.CompletionRatio)) + quota = int(math.Round(float64(quota) * priceData.ModelRatio)) + if priceData.ModelRatio != 0 && quota <= 0 { quota = 1 } } else { - quota = int(modelPrice * common.QuotaPerUnit) + quota = int(priceData.ModelPrice * common.QuotaPerUnit) } tok := time.Now() milliseconds := tok.Sub(tik).Milliseconds() consumedTime := float64(milliseconds) / 1000.0 - other := service.GenerateTextOtherInfo(c, meta, modelRatio, 1, completionRatio, modelPrice) + other := service.GenerateTextOtherInfo(c, info, priceData.ModelRatio, priceData.GroupRatio, priceData.CompletionRatio, priceData.ModelPrice) model.RecordConsumeLog(c, 1, channel.Id, usage.PromptTokens, usage.CompletionTokens, testModel, "模型测试", quota, "模型测试", 0, quota, int(consumedTime), false, "default", other) common.SysLog(fmt.Sprintf("testing channel #%d, response: \n%s", channel.Id, string(respBody))) diff --git a/relay/helper/price.go b/relay/helper/price.go index 97cbf162..51f64082 100644 --- a/relay/helper/price.go +++ b/relay/helper/price.go @@ -11,6 +11,7 @@ import ( type PriceData struct { ModelPrice float64 ModelRatio float64 + CompletionRatio float64 GroupRatio float64 UsePrice bool ShouldPreConsumedQuota int @@ -21,6 +22,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens groupRatio := setting.GetGroupRatio(info.Group) var preConsumedQuota int var modelRatio float64 + var completionRatio float64 if !usePrice { preConsumedTokens := common.PreConsumedQuota if maxTokens != 0 { @@ -35,6 +37,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens return PriceData{}, fmt.Errorf("模型 %s 倍率或价格未配置, 请联系管理员设置;Model %s ratio or price not set, please contact administrator to set", info.OriginModelName, info.OriginModelName) } } + completionRatio = setting.GetCompletionRatio(info.OriginModelName) ratio := modelRatio * groupRatio preConsumedQuota = int(float64(preConsumedTokens) * ratio) } else { @@ -43,6 +46,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens return PriceData{ ModelPrice: modelPrice, ModelRatio: modelRatio, + CompletionRatio: completionRatio, GroupRatio: groupRatio, UsePrice: usePrice, ShouldPreConsumedQuota: preConsumedQuota, From 2ca0d7246d935201cab49a475e9fedf43234c491 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 2 Mar 2025 15:59:39 +0800 Subject: [PATCH 10/15] fix: Use channel group in model testing log record --- controller/channel-test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/controller/channel-test.go b/controller/channel-test.go index cf253900..75400c93 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -140,6 +140,7 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr if err != nil { return err, nil } + info.PromptTokens = usage.PromptTokens priceData, err := helper.ModelPriceHelper(c, info, usage.PromptTokens, int(request.MaxTokens)) if err != nil { return err, nil @@ -159,7 +160,7 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr consumedTime := float64(milliseconds) / 1000.0 other := service.GenerateTextOtherInfo(c, info, priceData.ModelRatio, priceData.GroupRatio, priceData.CompletionRatio, priceData.ModelPrice) model.RecordConsumeLog(c, 1, channel.Id, usage.PromptTokens, usage.CompletionTokens, testModel, "模型测试", - quota, "模型测试", 0, quota, int(consumedTime), false, "default", other) + quota, "模型测试", 0, quota, int(consumedTime), false, info.Group, other) common.SysLog(fmt.Sprintf("testing channel #%d, response: \n%s", channel.Id, string(respBody))) return nil, nil } From 4084b1807160c269589596e8489598d601014417 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 2 Mar 2025 17:34:39 +0800 Subject: [PATCH 11/15] refactor: Update rate limit configuration to use dynamic expiration duration --- middleware/model-rate-limit.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/middleware/model-rate-limit.go b/middleware/model-rate-limit.go index 135e0005..7986bd49 100644 --- a/middleware/model-rate-limit.go +++ b/middleware/model-rate-limit.go @@ -51,7 +51,7 @@ func checkRedisRateLimit(ctx context.Context, rdb *redis.Client, key string, max // 如果在时间窗口内已达到限制,拒绝请求 subTime := nowTime.Sub(oldTime).Seconds() if int64(subTime) < duration { - rdb.Expire(ctx, key, common.RateLimitKeyExpirationDuration) + rdb.Expire(ctx, key, time.Duration(setting.ModelRequestRateLimitDurationMinutes)*time.Minute) return false, nil } @@ -68,7 +68,7 @@ func recordRedisRequest(ctx context.Context, rdb *redis.Client, key string, maxC now := time.Now().Format(timeFormat) rdb.LPush(ctx, key, now) rdb.LTrim(ctx, key, 0, int64(maxCount-1)) - rdb.Expire(ctx, key, common.RateLimitKeyExpirationDuration) + rdb.Expire(ctx, key, time.Duration(setting.ModelRequestRateLimitDurationMinutes)*time.Minute) } // Redis限流处理器 @@ -118,7 +118,7 @@ func redisRateLimitHandler(duration int64, totalMaxCount, successMaxCount int) g // 内存限流处理器 func memoryRateLimitHandler(duration int64, totalMaxCount, successMaxCount int) gin.HandlerFunc { - inMemoryRateLimiter.Init(common.RateLimitKeyExpirationDuration) + inMemoryRateLimiter.Init(time.Duration(setting.ModelRequestRateLimitDurationMinutes) * time.Minute) return func(c *gin.Context) { userId := strconv.Itoa(c.GetInt("id")) @@ -161,6 +161,7 @@ func ModelRequestRateLimit() func(c *gin.Context) { // 计算限流参数 duration := int64(setting.ModelRequestRateLimitDurationMinutes * 60) totalMaxCount := setting.ModelRequestRateLimitCount + successMaxCount := setting.ModelRequestRateLimitSuccessCount // 根据存储类型选择限流处理器 From 7208a65e5d8a051d697ad2f0d2505a498fc83d18 Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 2 Mar 2025 17:57:52 +0800 Subject: [PATCH 12/15] refactor: Add index to Username column in Log model --- model/log.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/model/log.go b/model/log.go index ed7ec2c7..86850a55 100644 --- a/model/log.go +++ b/model/log.go @@ -2,12 +2,13 @@ package model import ( "fmt" - "github.com/gin-gonic/gin" "one-api/common" "os" "strings" "time" + "github.com/gin-gonic/gin" + "github.com/bytedance/gopkg/util/gopool" "gorm.io/gorm" ) @@ -18,7 +19,7 @@ type Log struct { CreatedAt int64 `json:"created_at" gorm:"bigint;index:idx_created_at_id,priority:2;index:idx_created_at_type"` Type int `json:"type" gorm:"index:idx_created_at_type"` Content string `json:"content"` - Username string `json:"username" gorm:"index:index_username_model_name,priority:2;default:''"` + Username string `json:"username" gorm:"index;index:index_username_model_name,priority:2;default:''"` TokenName string `json:"token_name" gorm:"index;default:''"` ModelName string `json:"model_name" gorm:"index;index:index_username_model_name,priority:1;default:''"` Quota int `json:"quota" gorm:"default:0"` From 8731a32e5621723ed6b25d47696a5e83f185725a Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 2 Mar 2025 19:53:35 +0800 Subject: [PATCH 13/15] feat: Add model testing modal with search functionality in ChannelsTable - Implement a new modal for selecting and testing models per channel - Add search functionality to filter models by keyword - Replace dropdown with direct button for model testing - Introduce new state variables for managing model test modal --- web/src/components/ChannelsTable.js | 96 +++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 12 deletions(-) diff --git a/web/src/components/ChannelsTable.js b/web/src/components/ChannelsTable.js index 71914c4e..94cd6ba8 100644 --- a/web/src/components/ChannelsTable.js +++ b/web/src/components/ChannelsTable.js @@ -15,7 +15,7 @@ import { getQuotaPerUnit, renderGroup, renderNumberWithPoint, - renderQuota, renderQuotaWithPrompt + renderQuota, renderQuotaWithPrompt, stringToColor } from '../helpers/render'; import { Button, Divider, @@ -378,17 +378,15 @@ const ChannelsTable = () => { > {t('测试')} - - - + { const [enableTagMode, setEnableTagMode] = useState(false); const [showBatchSetTag, setShowBatchSetTag] = useState(false); const [batchSetTagValue, setBatchSetTagValue] = useState(''); + const [showModelTestModal, setShowModelTestModal] = useState(false); + const [currentTestChannel, setCurrentTestChannel] = useState(null); + const [modelSearchKeyword, setModelSearchKeyword] = useState(''); const removeRecord = (record) => { @@ -1289,6 +1290,77 @@ const ChannelsTable = () => { onChange={(v) => setBatchSetTagValue(v)} /> + + {/* 模型测试弹窗 */} + { + setShowModelTestModal(false); + setModelSearchKeyword(''); + }} + footer={null} + maskClosable={true} + centered={true} + width={600} + > +
+ {currentTestChannel && ( +
+ + {t('渠道')}: {currentTestChannel.name} + + + {/* 搜索框 */} + setModelSearchKeyword(value)} + style={{ marginBottom: '16px' }} + showClear + /> + +
+ {currentTestChannel.models.split(',') + .filter(model => model.toLowerCase().includes(modelSearchKeyword.toLowerCase())) + .map((model, index) => { + + return ( + + ); + })} +
+ + {/* 显示搜索结果数量 */} + {modelSearchKeyword && ( + + {t('找到')} {currentTestChannel.models.split(',').filter(model => + model.toLowerCase().includes(modelSearchKeyword.toLowerCase()) + ).length} {t('个模型')} + + )} +
+ )} +
+
); }; From 254c25c27a0f86445499c9d6f2ed490b41a8faec Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 2 Mar 2025 23:17:37 +0800 Subject: [PATCH 14/15] feat: yanjingxia --- web/src/constants/channel.constants.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/src/constants/channel.constants.js b/web/src/constants/channel.constants.js index dec74b06..5738d656 100644 --- a/web/src/constants/channel.constants.js +++ b/web/src/constants/channel.constants.js @@ -82,7 +82,7 @@ export const CHANNEL_OPTIONS = [ { value: 45, color: 'blue', - label: '火山方舟(豆包)' + label: '字节火山方舟、豆包、DeepSeek通用' }, { value: 25, color: 'green', label: 'Moonshot' }, { value: 19, color: 'blue', label: '360 智脑' }, From 760514c3e1ebd72250d3b00ee2cac4efcac7976a Mon Sep 17 00:00:00 2001 From: "1808837298@qq.com" <1808837298@qq.com> Date: Sun, 2 Mar 2025 23:53:10 +0800 Subject: [PATCH 15/15] fix: channel test model mapped --- controller/channel-test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/controller/channel-test.go b/controller/channel-test.go index 75400c93..4ac618d6 100644 --- a/controller/channel-test.go +++ b/controller/channel-test.go @@ -92,6 +92,7 @@ func testChannel(channel *model.Channel, testModel string) (err error, openAIErr if err != nil { return err, nil } + testModel = info.UpstreamModelName apiType, _ := constant.ChannelType2APIType(channel.Type) adaptor := relay.GetAdaptor(apiType)