mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
refactor: 废弃 rate_multiplier 字段,改进成本倍率回退逻辑
- 在前后端模型中为 rate_multiplier 添加 @deprecated 标记 - 修复 rate_multipliers 回退逻辑:当 rate_multipliers 存在但未配置当前格式时使用 1.0 - ModelMappingsTab: 删除映射后自动保存,简化空状态提示 - ModelManagement: 添加 refreshModel 事件在映射更新后刷新模型数据 - CacheAwareScheduler: 添加映射匹配调试日志,移除 candidate_models 限制
This commit is contained in:
@@ -1004,12 +1004,14 @@ class ProviderAPIKey(Base):
|
||||
note = Column(String(500), nullable=True) # 备注说明(可选)
|
||||
|
||||
# 成本计算
|
||||
# [DEPRECATED] rate_multiplier 已废弃,请使用 rate_multipliers
|
||||
# 将在未来版本中移除,目前仅作为 rate_multipliers 未配置时的回退值
|
||||
rate_multiplier = Column(
|
||||
Float, default=1.0, nullable=False
|
||||
) # 默认成本倍率(真实成本 = 表面成本 × 倍率)
|
||||
) # [DEPRECATED] 默认成本倍率,请使用 rate_multipliers
|
||||
rate_multipliers = Column(
|
||||
JSON, nullable=True
|
||||
) # 按 API 格式的成本倍率 {"CLAUDE": 1.0, "OPENAI": 0.8}
|
||||
) # 按 API 格式的成本倍率 {"CLAUDE_CLI": 1.0, "OPENAI_CLI": 0.8}
|
||||
|
||||
# 优先级配置 (数字越小越优先)
|
||||
internal_priority = Column(
|
||||
|
||||
@@ -132,11 +132,12 @@ class EndpointAPIKeyCreate(BaseModel):
|
||||
name: str = Field(..., min_length=1, max_length=100, description="密钥名称(必填,用于识别)")
|
||||
|
||||
# 成本计算
|
||||
# [DEPRECATED] rate_multiplier 已废弃,请使用 rate_multipliers
|
||||
rate_multiplier: float = Field(
|
||||
default=1.0, ge=0.01, description="默认成本倍率(真实成本 = 表面成本 × 倍率)"
|
||||
default=1.0, ge=0.01, description="[DEPRECATED] 默认成本倍率,已废弃,请使用 rate_multipliers"
|
||||
)
|
||||
rate_multipliers: Optional[Dict[str, float]] = Field(
|
||||
default=None, description="按 API 格式的成本倍率,如 {'CLAUDE': 1.0, 'OPENAI': 0.8}"
|
||||
default=None, description="按 API 格式的成本倍率,如 {'CLAUDE_CLI': 1.0, 'OPENAI_CLI': 0.8}"
|
||||
)
|
||||
|
||||
# 优先级和限制(数字越小越优先)
|
||||
@@ -273,9 +274,10 @@ class EndpointAPIKeyUpdate(BaseModel):
|
||||
default=None, min_length=3, max_length=500, description="API Key(将自动加密)"
|
||||
)
|
||||
name: Optional[str] = Field(default=None, min_length=1, max_length=100, description="密钥名称")
|
||||
rate_multiplier: Optional[float] = Field(default=None, ge=0.01, description="默认成本倍率")
|
||||
# [DEPRECATED] rate_multiplier 已废弃,请使用 rate_multipliers
|
||||
rate_multiplier: Optional[float] = Field(default=None, ge=0.01, description="[DEPRECATED] 默认成本倍率,已废弃")
|
||||
rate_multipliers: Optional[Dict[str, float]] = Field(
|
||||
default=None, description="按 API 格式的成本倍率,如 {'CLAUDE': 1.0, 'OPENAI': 0.8}"
|
||||
default=None, description="按 API 格式的成本倍率,如 {'CLAUDE_CLI': 1.0, 'OPENAI_CLI': 0.8}"
|
||||
)
|
||||
internal_priority: Optional[int] = Field(
|
||||
default=None, description="Key 内部优先级(提供商优先模式,数字越小越优先)"
|
||||
@@ -399,9 +401,10 @@ class EndpointAPIKeyResponse(BaseModel):
|
||||
name: str = Field(..., description="密钥名称")
|
||||
|
||||
# 成本计算
|
||||
rate_multiplier: float = Field(default=1.0, description="默认成本倍率")
|
||||
# [DEPRECATED] rate_multiplier 已废弃,请使用 rate_multipliers
|
||||
rate_multiplier: float = Field(default=1.0, description="[DEPRECATED] 默认成本倍率,已废弃")
|
||||
rate_multipliers: Optional[Dict[str, float]] = Field(
|
||||
default=None, description="按 API 格式的成本倍率,如 {'CLAUDE': 1.0, 'OPENAI': 0.8}"
|
||||
default=None, description="按 API 格式的成本倍率,如 {'CLAUDE_CLI': 1.0, 'OPENAI_CLI': 0.8}"
|
||||
)
|
||||
|
||||
# 优先级和限制
|
||||
|
||||
12
src/services/cache/aware_scheduler.py
vendored
12
src/services/cache/aware_scheduler.py
vendored
@@ -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(
|
||||
|
||||
11
src/services/cache/provider_cache.py
vendored
11
src/services/cache/provider_cache.py
vendored
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user