fix: 修正 Hermite 同步逻辑与模型优先级配置
This commit is contained in:
@@ -60,12 +60,15 @@ def sync_hermes_model_settings(
|
||||
target_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
config = _load_existing_config(target_path)
|
||||
config["model"] = _build_primary_model_config(primary_route)
|
||||
config["model"] = _build_primary_model_config(primary_route, existing_model_config=config.get("model"))
|
||||
|
||||
if fallback_route is None:
|
||||
config.pop("fallback_model", None)
|
||||
else:
|
||||
config["fallback_model"] = _build_fallback_model_config(fallback_route)
|
||||
config["fallback_model"] = _build_fallback_model_config(
|
||||
fallback_route,
|
||||
existing_fallback_config=config.get("fallback_model"),
|
||||
)
|
||||
|
||||
_atomic_yaml_write(target_path, config)
|
||||
return target_path
|
||||
@@ -87,7 +90,11 @@ def _load_existing_config(config_path: Path) -> dict[str, Any]:
|
||||
return dict(loaded)
|
||||
|
||||
|
||||
def _build_primary_model_config(route: HermesModelRoute) -> dict[str, Any]:
|
||||
def _build_primary_model_config(
|
||||
route: HermesModelRoute,
|
||||
*,
|
||||
existing_model_config: Any = None,
|
||||
) -> dict[str, Any]:
|
||||
normalized_model = route.model.strip()
|
||||
normalized_endpoint = route.endpoint.strip().rstrip("/")
|
||||
if not normalized_model or not normalized_endpoint:
|
||||
@@ -99,10 +106,11 @@ def _build_primary_model_config(route: HermesModelRoute) -> dict[str, Any]:
|
||||
"default": normalized_model,
|
||||
"base_url": normalized_endpoint,
|
||||
}
|
||||
existing_api_key = _extract_existing_api_key(existing_model_config)
|
||||
if route.api_key.strip():
|
||||
payload["api_key"] = route.api_key.strip()
|
||||
else:
|
||||
payload.pop("api_key", None)
|
||||
elif existing_api_key:
|
||||
payload["api_key"] = existing_api_key
|
||||
if api_mode != "chat_completions":
|
||||
payload["api_mode"] = api_mode
|
||||
else:
|
||||
@@ -110,7 +118,11 @@ def _build_primary_model_config(route: HermesModelRoute) -> dict[str, Any]:
|
||||
return payload
|
||||
|
||||
|
||||
def _build_fallback_model_config(route: HermesModelRoute) -> dict[str, Any]:
|
||||
def _build_fallback_model_config(
|
||||
route: HermesModelRoute,
|
||||
*,
|
||||
existing_fallback_config: Any = None,
|
||||
) -> dict[str, Any]:
|
||||
normalized_model = route.model.strip()
|
||||
normalized_endpoint = route.endpoint.strip().rstrip("/")
|
||||
if not normalized_model or not normalized_endpoint:
|
||||
@@ -122,10 +134,11 @@ def _build_fallback_model_config(route: HermesModelRoute) -> dict[str, Any]:
|
||||
"model": normalized_model,
|
||||
"base_url": normalized_endpoint,
|
||||
}
|
||||
existing_api_key = _extract_existing_api_key(existing_fallback_config)
|
||||
if route.api_key.strip():
|
||||
payload["api_key"] = route.api_key.strip()
|
||||
else:
|
||||
payload.pop("api_key", None)
|
||||
elif existing_api_key:
|
||||
payload["api_key"] = existing_api_key
|
||||
if api_mode != "chat_completions":
|
||||
payload["api_mode"] = api_mode
|
||||
else:
|
||||
@@ -133,6 +146,15 @@ def _build_fallback_model_config(route: HermesModelRoute) -> dict[str, Any]:
|
||||
return payload
|
||||
|
||||
|
||||
def _extract_existing_api_key(config_section: Any) -> str:
|
||||
if not isinstance(config_section, dict):
|
||||
return ""
|
||||
api_key = config_section.get("api_key")
|
||||
if not isinstance(api_key, str):
|
||||
return ""
|
||||
return api_key.strip()
|
||||
|
||||
|
||||
def _infer_api_mode(route: HermesModelRoute) -> str:
|
||||
provider_label = route.provider_label.strip().casefold()
|
||||
endpoint = route.endpoint.strip().lower().rstrip("/")
|
||||
|
||||
Reference in New Issue
Block a user