diff --git a/controller/option.go b/controller/option.go index 959f2f9b8..f827e0e4a 100644 --- a/controller/option.go +++ b/controller/option.go @@ -169,6 +169,15 @@ func UpdateOption(c *gin.Context) { }) return } + case "CreateCacheRatio": + err = ratio_setting.UpdateCreateCacheRatioByJSONString(option.Value.(string)) + if err != nil { + c.JSON(http.StatusOK, gin.H{ + "success": false, + "message": "缓存创建倍率设置失败: " + err.Error(), + }) + return + } case "ModelRequestRateLimitGroup": err = setting.CheckModelRequestRateLimitGroup(option.Value.(string)) if err != nil { diff --git a/model/option.go b/model/option.go index b738db4dc..697e77dfe 100644 --- a/model/option.go +++ b/model/option.go @@ -115,6 +115,7 @@ func InitOptionMap() { common.OptionMap["ModelRatio"] = ratio_setting.ModelRatio2JSONString() common.OptionMap["ModelPrice"] = ratio_setting.ModelPrice2JSONString() common.OptionMap["CacheRatio"] = ratio_setting.CacheRatio2JSONString() + common.OptionMap["CreateCacheRatio"] = ratio_setting.CreateCacheRatio2JSONString() common.OptionMap["GroupRatio"] = ratio_setting.GroupRatio2JSONString() common.OptionMap["GroupGroupRatio"] = ratio_setting.GroupGroupRatio2JSONString() common.OptionMap["UserUsableGroups"] = setting.UserUsableGroups2JSONString() @@ -427,6 +428,8 @@ func updateOptionMap(key string, value string) (err error) { err = ratio_setting.UpdateModelPriceByJSONString(value) case "CacheRatio": err = ratio_setting.UpdateCacheRatioByJSONString(value) + case "CreateCacheRatio": + err = ratio_setting.UpdateCreateCacheRatioByJSONString(value) case "ImageRatio": err = ratio_setting.UpdateImageRatioByJSONString(value) case "AudioRatio": diff --git a/setting/ratio_setting/cache_ratio.go b/setting/ratio_setting/cache_ratio.go index 626267537..42e6df8f3 100644 --- a/setting/ratio_setting/cache_ratio.go +++ b/setting/ratio_setting/cache_ratio.go @@ -101,6 +101,9 @@ var defaultCreateCacheRatio = map[string]float64{ var cacheRatioMap map[string]float64 var cacheRatioMapMutex sync.RWMutex +var createCacheRatioMap map[string]float64 +var createCacheRatioMapMutex sync.RWMutex + // GetCacheRatioMap returns the cache ratio map func GetCacheRatioMap() map[string]float64 { cacheRatioMapMutex.RLock() @@ -119,6 +122,17 @@ func CacheRatio2JSONString() string { return string(jsonBytes) } +// CreateCacheRatio2JSONString converts the create cache ratio map to a JSON string +func CreateCacheRatio2JSONString() string { + createCacheRatioMapMutex.RLock() + defer createCacheRatioMapMutex.RUnlock() + jsonBytes, err := json.Marshal(createCacheRatioMap) + if err != nil { + common.SysLog("error marshalling create cache ratio: " + err.Error()) + } + return string(jsonBytes) +} + // UpdateCacheRatioByJSONString updates the cache ratio map from a JSON string func UpdateCacheRatioByJSONString(jsonStr string) error { cacheRatioMapMutex.Lock() @@ -131,6 +145,18 @@ func UpdateCacheRatioByJSONString(jsonStr string) error { return err } +// UpdateCreateCacheRatioByJSONString updates the create cache ratio map from a JSON string +func UpdateCreateCacheRatioByJSONString(jsonStr string) error { + createCacheRatioMapMutex.Lock() + defer createCacheRatioMapMutex.Unlock() + createCacheRatioMap = make(map[string]float64) + err := json.Unmarshal([]byte(jsonStr), &createCacheRatioMap) + if err == nil { + InvalidateExposedDataCache() + } + return err +} + // GetCacheRatio returns the cache ratio for a model func GetCacheRatio(name string) (float64, bool) { cacheRatioMapMutex.RLock() @@ -143,7 +169,9 @@ func GetCacheRatio(name string) (float64, bool) { } func GetCreateCacheRatio(name string) (float64, bool) { - ratio, ok := defaultCreateCacheRatio[name] + createCacheRatioMapMutex.RLock() + defer createCacheRatioMapMutex.RUnlock() + ratio, ok := createCacheRatioMap[name] if !ok { return 1.25, false // Default to 1.25 if not found } @@ -159,3 +187,13 @@ func GetCacheRatioCopy() map[string]float64 { } return copyMap } + +func GetCreateCacheRatioCopy() map[string]float64 { + createCacheRatioMapMutex.RLock() + defer createCacheRatioMapMutex.RUnlock() + copyMap := make(map[string]float64, len(createCacheRatioMap)) + for k, v := range createCacheRatioMap { + copyMap[k] = v + } + return copyMap +} diff --git a/setting/ratio_setting/exposed_cache.go b/setting/ratio_setting/exposed_cache.go index 2fe2cd09b..c88216fcb 100644 --- a/setting/ratio_setting/exposed_cache.go +++ b/setting/ratio_setting/exposed_cache.go @@ -42,10 +42,11 @@ func GetExposedData() gin.H { return cloneGinH(c.data) } newData := gin.H{ - "model_ratio": GetModelRatioCopy(), - "completion_ratio": GetCompletionRatioCopy(), - "cache_ratio": GetCacheRatioCopy(), - "model_price": GetModelPriceCopy(), + "model_ratio": GetModelRatioCopy(), + "completion_ratio": GetCompletionRatioCopy(), + "cache_ratio": GetCacheRatioCopy(), + "create_cache_ratio": GetCreateCacheRatioCopy(), + "model_price": GetModelPriceCopy(), } exposedData.Store(&exposedCache{ data: newData, diff --git a/setting/ratio_setting/model_ratio.go b/setting/ratio_setting/model_ratio.go index 6b7d70e77..7e9162e4c 100644 --- a/setting/ratio_setting/model_ratio.go +++ b/setting/ratio_setting/model_ratio.go @@ -362,6 +362,11 @@ func InitRatioSettings() { cacheRatioMap = defaultCacheRatio cacheRatioMapMutex.Unlock() + // Initialize createCacheRatioMap (5m cache creation ratio) + createCacheRatioMapMutex.Lock() + createCacheRatioMap = defaultCreateCacheRatio + createCacheRatioMapMutex.Unlock() + // initialize imageRatioMap imageRatioMapMutex.Lock() imageRatioMap = defaultImageRatio diff --git a/web/src/components/settings/RatioSetting.jsx b/web/src/components/settings/RatioSetting.jsx index 21cccd26d..170413058 100644 --- a/web/src/components/settings/RatioSetting.jsx +++ b/web/src/components/settings/RatioSetting.jsx @@ -36,6 +36,7 @@ const RatioSetting = () => { ModelPrice: '', ModelRatio: '', CacheRatio: '', + CreateCacheRatio: '', CompletionRatio: '', GroupRatio: '', GroupGroupRatio: '', diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index 86e82f757..057b54218 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -1145,6 +1145,8 @@ "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址": "Tip: {key} in the link will be replaced with the API key, {address} will be replaced with the server address", "提示价格:{{symbol}}{{price}} / 1M tokens": "Prompt price: {{symbol}}{{price}} / 1M tokens", "提示缓存倍率": "Prompt cache ratio", + "缓存创建倍率": "Cache creation ratio", + "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)": "Defaults to the 5m cache creation ratio; the 1h cache creation ratio is computed by fixed multiplication (currently 1.6x)", "搜索供应商": "Search vendor", "搜索关键字": "Search keywords", "搜索失败": "Search failed", diff --git a/web/src/i18n/locales/fr.json b/web/src/i18n/locales/fr.json index 8fa6478cb..35590c3c4 100644 --- a/web/src/i18n/locales/fr.json +++ b/web/src/i18n/locales/fr.json @@ -1155,6 +1155,8 @@ "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址": "Astuce : {key} dans le lien sera remplacé par la clé API, {address} sera remplacé par l'adresse du serveur", "提示价格:{{symbol}}{{price}} / 1M tokens": "Prix d'invite : {{symbol}}{{price}} / 1M tokens", "提示缓存倍率": "Ratio de cache d'invite", + "缓存创建倍率": "Ratio de création du cache", + "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)": "Par défaut, le ratio de création de cache 5m est utilisé ; le ratio de création de cache 1h est calculé via une multiplication fixe (actuellement 1.6x)", "搜索供应商": "Rechercher un fournisseur", "搜索关键字": "Rechercher des mots-clés", "搜索失败": "Search failed", diff --git a/web/src/i18n/locales/ja.json b/web/src/i18n/locales/ja.json index 1aa66c93a..2de9d9c64 100644 --- a/web/src/i18n/locales/ja.json +++ b/web/src/i18n/locales/ja.json @@ -1140,6 +1140,8 @@ "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址": "ヒント:リンク内の{key}はAPIキーに、{address}はサーバーURLに置換されます", "提示价格:{{symbol}}{{price}} / 1M tokens": "プロンプト料金:{{symbol}}{{price}} / 1M tokens", "提示缓存倍率": "プロンプトキャッシュ倍率", + "缓存创建倍率": "キャッシュ作成倍率", + "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)": "デフォルトは5mのキャッシュ作成倍率です。1hのキャッシュ作成倍率は固定乗数で自動計算されます(現在は1.6倍)", "搜索供应商": "プロバイダーで検索", "搜索关键字": "検索キーワード", "搜索失败": "Search failed", diff --git a/web/src/i18n/locales/ru.json b/web/src/i18n/locales/ru.json index 2e9137946..329b28f36 100644 --- a/web/src/i18n/locales/ru.json +++ b/web/src/i18n/locales/ru.json @@ -1166,6 +1166,8 @@ "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址": "Промпт: {key} в ссылке будет заменен на API-ключ, {address} будет заменен на адрес сервера", "提示价格:{{symbol}}{{price}} / 1M tokens": "Цена промпта: {{symbol}}{{price}} / 1M токенов", "提示缓存倍率": "Коэффициент кэша промптов", + "缓存创建倍率": "Коэффициент создания кэша", + "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)": "По умолчанию используется коэффициент создания кэша 5m; коэффициент создания кэша 1h автоматически вычисляется фиксированным умножением (сейчас 1.6x)", "搜索供应商": "Поиск поставщиков", "搜索关键字": "Поиск по ключевым словам", "搜索失败": "Search failed", diff --git a/web/src/i18n/locales/vi.json b/web/src/i18n/locales/vi.json index f6162b8a9..2d6ff8602 100644 --- a/web/src/i18n/locales/vi.json +++ b/web/src/i18n/locales/vi.json @@ -1141,6 +1141,8 @@ "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址": "Mẹo: {key} trong liên kết sẽ được thay thế bằng khóa API, {address} sẽ được thay thế bằng địa chỉ máy chủ", "提示价格:{{symbol}}{{price}} / 1M tokens": "Giá gợi ý: {{symbol}}{{price}} / 1M tokens", "提示缓存倍率": "Tỷ lệ bộ nhớ đệm gợi ý", + "缓存创建倍率": "Tỷ lệ tạo bộ nhớ đệm", + "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)": "Mặc định dùng tỷ lệ tạo bộ nhớ đệm 5m; tỷ lệ tạo bộ nhớ đệm 1h được tự động tính bằng phép nhân cố định (hiện là 1.6x)", "搜索供应商": "Tìm kiếm nhà cung cấp", "搜索关键字": "Từ khóa tìm kiếm", "搜索失败": "Search failed", diff --git a/web/src/i18n/locales/zh.json b/web/src/i18n/locales/zh.json index 551915cd3..a766c2e14 100644 --- a/web/src/i18n/locales/zh.json +++ b/web/src/i18n/locales/zh.json @@ -1135,6 +1135,8 @@ "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址": "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址", "提示价格:{{symbol}}{{price}} / 1M tokens": "提示价格:{{symbol}}{{price}} / 1M tokens", "提示缓存倍率": "提示缓存倍率", + "缓存创建倍率": "缓存创建倍率", + "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)": "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)", "搜索供应商": "搜索供应商", "搜索关键字": "搜索关键字", "搜索失败": "搜索失败", diff --git a/web/src/pages/Setting/Ratio/ModelRatioSettings.jsx b/web/src/pages/Setting/Ratio/ModelRatioSettings.jsx index b298cc787..e9be19785 100644 --- a/web/src/pages/Setting/Ratio/ModelRatioSettings.jsx +++ b/web/src/pages/Setting/Ratio/ModelRatioSettings.jsx @@ -43,6 +43,7 @@ export default function ModelRatioSettings(props) { ModelPrice: '', ModelRatio: '', CacheRatio: '', + CreateCacheRatio: '', CompletionRatio: '', ImageRatio: '', AudioRatio: '', @@ -200,6 +201,30 @@ export default function ModelRatioSettings(props) { /> + + + verifyJSON(value), + message: '不是合法的 JSON 字符串', + }, + ]} + onChange={(value) => + setInputs({ ...inputs, CreateCacheRatio: value }) + } + /> + +