refactor: 废弃 rate_multiplier 字段,改进成本倍率回退逻辑

- 在前后端模型中为 rate_multiplier 添加 @deprecated 标记
- 修复 rate_multipliers 回退逻辑:当 rate_multipliers 存在但未配置当前格式时使用 1.0
- ModelMappingsTab: 删除映射后自动保存,简化空状态提示
- ModelManagement: 添加 refreshModel 事件在映射更新后刷新模型数据
- CacheAwareScheduler: 添加映射匹配调试日志,移除 candidate_models 限制
This commit is contained in:
fawney19
2026-01-14 21:07:28 +08:00
parent bec9c3a989
commit 0348b7d915
10 changed files with 67 additions and 22 deletions

View File

@@ -593,6 +593,8 @@ class CacheAwareScheduler:
# 提取模型映射(用于 Provider Key 的 allowed_models 匹配)
model_mappings: List[str] = (global_model.config or {}).get("model_mappings", [])
if model_mappings:
logger.debug(f"[Scheduler] GlobalModel={global_model.name} 配置了映射规则: {model_mappings}")
# 获取合并后的访问限制ApiKey + User
restrictions = self._get_effective_restrictions(user_api_key)
@@ -929,6 +931,12 @@ class CacheAwareScheduler:
model_mappings=model_mappings,
candidate_models=candidate_models,
)
if mapping_matched_model:
logger.debug(
f"[Scheduler] Key {key.id[:8]}... 映射匹配成功: "
f"model={model_name} -> {mapping_matched_model}, "
f"allowed_models={key.allowed_models}, model_mappings={model_mappings}"
)
except TimeoutError:
# 正则匹配超时(可能是 ReDoS 攻击或复杂模式)
logger.warning(f"映射匹配超时: key_id={key.id}, model={model_name}")
@@ -1047,6 +1055,9 @@ class CacheAwareScheduler:
for key in keys:
# Key 级别的能力检查
# 注意:不传入 candidate_models 限制,允许映射匹配到 Key 的 allowed_models 中的任意模型名
# 这支持以下场景Key 只允许使用 gpt-5.2,而 GlobalModel 配置了映射 gpt-5.*2
# 映射匹配后,实际请求会使用 gpt-5.2 作为模型名发送给 Provider
is_available, skip_reason, mapping_matched_model = self._check_key_availability(
key,
target_format_str,
@@ -1054,7 +1065,6 @@ class CacheAwareScheduler:
capability_requirements,
resolved_model_name=resolved_model_name,
model_mappings=model_mappings,
candidate_models=provider_model_names,
)
candidate = ProviderCandidate(

View File

@@ -64,12 +64,19 @@ class ProviderCacheService:
# 3. 计算倍率并写入缓存
if provider_key:
# 优先使用 rate_multipliers[api_format],回退到 rate_multiplier
rate_multiplier = provider_key.rate_multiplier or 1.0
# 优先使用 rate_multipliers[api_format]
# 如果 rate_multipliers 存在但未配置该格式,默认为 1.0
# 只有当 rate_multipliers 完全不存在时,才回退到 rate_multiplier
if api_format and provider_key.rate_multipliers:
format_upper = api_format.upper()
if format_upper in provider_key.rate_multipliers:
rate_multiplier = provider_key.rate_multipliers[format_upper]
else:
# rate_multipliers 存在但该格式未配置,使用默认值 1.0
rate_multiplier = 1.0
else:
# rate_multipliers 不存在或未指定 api_format回退到默认倍率
rate_multiplier = provider_key.rate_multiplier or 1.0
await CacheService.set(
cache_key, rate_multiplier, ttl_seconds=ProviderCacheService.CACHE_TTL