Prechádzať zdrojové kódy

Merge pull request #2870 from seefs001/feature/cache-creation-configurable

feat: make 5m cache-creation ratio configurable
Calcium-Ion 3 týždňov pred
rodič
commit
7a146a11f5

+ 9 - 0
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 {

+ 3 - 0
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":

+ 39 - 1
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
+}

+ 5 - 4
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,

+ 5 - 0
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

+ 1 - 0
web/src/components/settings/RatioSetting.jsx

@@ -36,6 +36,7 @@ const RatioSetting = () => {
     ModelPrice: '',
     ModelRatio: '',
     CacheRatio: '',
+    CreateCacheRatio: '',
     CompletionRatio: '',
     GroupRatio: '',
     GroupGroupRatio: '',

+ 2 - 0
web/src/i18n/locales/en.json

@@ -1146,6 +1146,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",

+ 2 - 0
web/src/i18n/locales/fr.json

@@ -1156,6 +1156,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",

+ 2 - 0
web/src/i18n/locales/ja.json

@@ -1141,6 +1141,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",

+ 2 - 0
web/src/i18n/locales/ru.json

@@ -1167,6 +1167,8 @@
     "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址": "Промпт: {key} в ссылке будет заменен на API-ключ, {address} будет заменен на адрес сервера",
     "提示价格:{{symbol}}{{price}} / 1M tokens": "Цена промпта: {{symbol}}{{price}} / 1M токенов",
     "提示缓存倍率": "Коэффициент кэша промптов",
+    "缓存创建倍率": "Коэффициент создания кэша",
+    "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)": "По умолчанию используется коэффициент создания кэша 5m; коэффициент создания кэша 1h автоматически вычисляется фиксированным умножением (сейчас 1.6x)",
     "搜索供应商": "Поиск поставщиков",
     "搜索关键字": "Поиск по ключевым словам",
     "搜索失败": "Search failed",

+ 2 - 0
web/src/i18n/locales/vi.json

@@ -1142,6 +1142,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",

+ 2 - 0
web/src/i18n/locales/zh.json

@@ -1136,6 +1136,8 @@
     "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址": "提示:链接中的{key}将被替换为API密钥,{address}将被替换为服务器地址",
     "提示价格:{{symbol}}{{price}} / 1M tokens": "提示价格:{{symbol}}{{price}} / 1M tokens",
     "提示缓存倍率": "提示缓存倍率",
+    "缓存创建倍率": "缓存创建倍率",
+    "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)": "默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)",
     "搜索供应商": "搜索供应商",
     "搜索关键字": "搜索关键字",
     "搜索失败": "搜索失败",

+ 25 - 0
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) {
             />
           </Col>
         </Row>
+        <Row gutter={16}>
+          <Col xs={24} sm={16}>
+            <Form.TextArea
+              label={t('缓存创建倍率')}
+              extraText={t(
+                '默认为 5m 缓存创建倍率;1h 缓存创建倍率按固定乘法自动计算(当前为 1.6x)',
+              )}
+              placeholder={t('为一个 JSON 文本,键为模型名称,值为倍率')}
+              field={'CreateCacheRatio'}
+              autosize={{ minRows: 6, maxRows: 12 }}
+              trigger='blur'
+              stopValidateWithError
+              rules={[
+                {
+                  validator: (rule, value) => verifyJSON(value),
+                  message: '不是合法的 JSON 字符串',
+                },
+              ]}
+              onChange={(value) =>
+                setInputs({ ...inputs, CreateCacheRatio: value })
+              }
+            />
+          </Col>
+        </Row>
         <Row gutter={16}>
           <Col xs={24} sm={16}>
             <Form.TextArea