model_normalize.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. """Per-provider model name normalization."""
  2. from __future__ import annotations
  3. import logging
  4. import re
  5. from typing import Optional
  6. # First hyphen-delimited token of a bare model name → vendor slug used by aggregator APIs
  7. # ("claude-sonnet-4.6" → "anthropic/claude-sonnet-4.6").
  8. _VENDOR_PREFIXES: dict[str, str] = {
  9. "claude": "anthropic",
  10. "gpt": "openai",
  11. "o1": "openai",
  12. "o3": "openai",
  13. "o4": "openai",
  14. "gemini": "google",
  15. "gemma": "google",
  16. "deepseek": "deepseek",
  17. "glm": "z-ai",
  18. "kimi": "moonshotai",
  19. "minimax": "minimax",
  20. "grok": "x-ai",
  21. "qwen": "qwen",
  22. "mimo": "xiaomi",
  23. "trinity": "arcee-ai",
  24. "nemotron": "nvidia",
  25. "llama": "meta-llama",
  26. "step": "stepfun"}
  27. # Providers whose APIs consume vendor/model slugs.
  28. _AGGREGATOR_PROVIDERS: frozenset[str] = frozenset({
  29. "openrouter", "nous", "ai-gateway", "kilocode"})
  30. # Providers that want bare names with dots replaced by hyphens.
  31. _DOT_TO_HYPHEN_PROVIDERS: frozenset[str] = frozenset({
  32. "anthropic"})
  33. # Providers that want bare names with dots preserved.
  34. _STRIP_VENDOR_ONLY_PROVIDERS: frozenset[str] = frozenset({
  35. "openai-codex"})
  36. # Providers whose native naming is authoritative -- pass through unchanged.
  37. _AUTHORITATIVE_NATIVE_PROVIDERS: frozenset[str] = frozenset({
  38. "huggingface"})
  39. # Direct providers that accept bare native names but should repair a matching
  40. # provider/ prefix when users copy the aggregator form into config.yaml.
  41. _MATCHING_PREFIX_STRIP_PROVIDERS: frozenset[str] = frozenset({
  42. "zai",
  43. "kimi-coding",
  44. # Providers whose endpoint does not accept image input, even though the provider's broader ecosystem has
  45. # vision models available elsewhere. When `auxiliary.vision.provider: auto` sees one of these as the
  46. # main provider, it must skip straight to the aggregator chain instead of returning a client that will
  47. # 404 on every vision request. kimi-coding / kimi-coding-cn: the Kimi Coding Plan routes through
  48. # api.kimi.com/coding (Anthropic Messages wire) which Kimi's own docs describe as having no image_in
  49. # capability. Vision lives on the separate Kimi Platform (api.moonshot.ai, OpenAI-wire, pay-as-you-go).
  50. # See #17076.
  51. "kimi-coding-cn",
  52. "minimax",
  53. "minimax-oauth",
  54. "minimax-cn",
  55. "alibaba",
  56. "qwen-oauth",
  57. "xiaomi",
  58. "arcee",
  59. "ollama-cloud",
  60. "nebius-token-factory",
  61. "custom",
  62. "gemini",
  63. "xai"})
  64. # Providers whose API serves ``vendor/model`` ids but whose endpoint can also
  65. # front arbitrary self-hosted models, so a bare name cannot be prefixed
  66. # blindly. A bare id is repaired only when the curated catalogue for that
  67. # provider holds exactly one entry ending in ``/<name>`` — a lookup, not a
  68. # guess. NVIDIA NIM is the case in hand: build.nvidia.com serves
  69. # ``nvidia/nemotron-…`` (and third-party ``z-ai/glm-…``), while the same
  70. # provider id also points at local NIM containers with their own naming.
  71. # Without this repair a bare ``nemotron-3-ultra-550b-a55b`` reaches the API
  72. # and returns a bare ``404 page not found`` that never names the model (#78796).
  73. _CATALOGUE_PREFIX_REPAIR_PROVIDERS: frozenset[str] = frozenset({
  74. "nvidia"})
  75. # Providers whose APIs require lowercase model IDs (Xiaomi rejects ``MiMo-V2.5-Pro`` copied from
  76. # marketing docs; only ``mimo-v2.5-pro`` works). Applied after matching-prefix stripping.
  77. _LOWERCASE_MODEL_PROVIDERS: frozenset[str] = frozenset({
  78. "xiaomi"})
  79. # DeepSeek retired ``deepseek-chat`` / ``deepseek-reasoner`` on 2026-07-24 (HTTP 400 since); saved
  80. # configs still carry them, so they fold onto the current Flash id (thinking mode is controlled by
  81. # extra_body.thinking on the profile). Every other id is the user's call and goes to the wire as typed:
  82. # a shape-based allow-list swallowed the vendor's own ``deepseek-flash`` the day it shipped (#107206),
  83. # and any new id without a ``v<N>`` marker would have met the same fate.
  84. _DEEPSEEK_RETIRED_ALIASES: dict[str, str] = {
  85. "deepseek-chat": "deepseek-flash", "deepseek-reasoner": "deepseek-flash"}
  86. def _normalize_for_deepseek(model_name: str) -> str:
  87. """Fold retired DeepSeek aliases onto their replacement; pass everything else through."""
  88. bare = _strip_vendor_prefix(model_name).lower()
  89. return _DEEPSEEK_RETIRED_ALIASES.get(bare, bare)
  90. def _strip_vendor_prefix(model_name: str) -> str:
  91. """Remove a ``vendor/`` prefix if present."""
  92. return model_name.split("/", 1)[1] if "/" in model_name else model_name
  93. def _dots_to_hyphens(model_name: str) -> str:
  94. return model_name.replace(".", "-")
  95. def _normalize_provider_alias(provider_name: str) -> str:
  96. """Resolve provider aliases to Hermes' canonical ids."""
  97. raw = (provider_name or "").strip().lower()
  98. if not raw:
  99. return raw
  100. try:
  101. from hermes_cli.models import normalize_provider
  102. return normalize_provider(raw)
  103. except Exception:
  104. return raw
  105. def _strip_matching_provider_prefix(model_name: str, target_provider: str) -> str:
  106. """Strip ``provider/`` or ``provider:`` only when the prefix matches the target provider, so
  107. arbitrary slash-bearing ids aren't mangled while ``zai/glm-5.1`` is repaired for ``zai``. The colon
  108. form is Hermes's own ``provider:model`` switch syntax (``-m openai-codex:gpt-5.6-sol``); left intact
  109. it reaches the wire and the Codex backend rejects it with HTTP 400 (#64787). Only the FIRST separator
  110. counts, so an Ollama-style ``qwen3:8b`` tag is never split on a later colon. ``custom`` is a bucket,
  111. not a vendor: an alias resolving to it (``ollama``) may be a real LiteLLM-style routing prefix, so
  112. only a literal ``custom/`` / ``custom:`` prefix is redundant there."""
  113. cut = min((i for i in (model_name.find("/"), model_name.find(":")) if i >= 0), default=-1)
  114. if cut < 0:
  115. return model_name
  116. prefix, remainder = model_name[:cut], model_name[cut + 1:]
  117. if not prefix.strip() or not remainder.strip():
  118. return model_name
  119. normalized_target = _normalize_provider_alias(target_provider)
  120. if normalized_target == "custom":
  121. return remainder.strip() if prefix.strip().lower() == "custom" else model_name
  122. normalized_prefix = _normalize_provider_alias(prefix)
  123. return remainder.strip() if normalized_prefix and normalized_prefix == normalized_target else model_name
  124. def detect_vendor(model_name: str) -> Optional[str]:
  125. """Vendor slug from a bare model name: an existing ``vendor/`` prefix, the first hyphen token,
  126. or a ``_VENDOR_PREFIXES`` key the name starts with (``qwen3.5-plus`` → ``qwen``)."""
  127. name = model_name.strip()
  128. if not name:
  129. return None
  130. if "/" in name:
  131. return name.split("/", 1)[0].lower() or None
  132. name_lower = name.lower()
  133. first_token = name_lower.split("-")[0]
  134. if first_token in _VENDOR_PREFIXES:
  135. return _VENDOR_PREFIXES[first_token]
  136. return next((vendor for prefix, vendor in _VENDOR_PREFIXES.items() if name_lower.startswith(prefix)), None)
  137. def _prepend_vendor(model_name: str) -> str:
  138. """Prepend the detected ``vendor/`` for aggregators; names with ``/`` or no detectable vendor
  139. pass through (the aggregator may still accept them)."""
  140. if "/" in model_name:
  141. return model_name
  142. vendor = detect_vendor(model_name)
  143. return f"{vendor}/{model_name}" if vendor else model_name
  144. def _repair_prefix_from_catalogue(model_name: str, provider: str) -> str:
  145. """Restore a dropped ``vendor/`` prefix only when the bare id matches **exactly one** curated
  146. entry for this provider modulo the prefix — a lookup, never a guess from name shape."""
  147. if "/" in model_name:
  148. return model_name
  149. try:
  150. from hermes_cli.models import _PROVIDER_MODELS
  151. except Exception:
  152. return model_name
  153. # Compare against the catalogue's own suffix, tag included: a bare ``…:free`` id must resolve to
  154. # the ``:free`` entry, not its paid sibling.
  155. needle = model_name.strip().lower()
  156. catalogue = _PROVIDER_MODELS.get(provider) or []
  157. matches = {e for e in catalogue if "/" in e and e.split("/", 1)[1].strip().lower() == needle}
  158. return matches.pop() if len(matches) == 1 else model_name
  159. def suggest_prefixed_model_id(provider: str, model_name: str) -> Optional[str]:
  160. """Prefixed catalogue id for a bare *model_name* if unambiguous, else ``None`` — the diagnostic
  161. counterpart to :func:`_repair_prefix_from_catalogue` for explaining a content-free 404."""
  162. name = (model_name or "").strip()
  163. if not name or "/" in name:
  164. return None
  165. try:
  166. canonical = _normalize_provider_alias(provider)
  167. except Exception:
  168. return None
  169. repaired = _repair_prefix_from_catalogue(name, canonical)
  170. return repaired if repaired != name else None
  171. def normalize_model_for_provider(model_input: str, target_provider: str) -> str:
  172. """Translate a model name (bare, vendor-prefixed or native) into what the target provider's API
  173. expects. ``target_provider`` should already be canonical. Never raises."""
  174. name = (model_input or "").strip()
  175. if not name:
  176. return name
  177. provider = _normalize_provider_alias(target_provider)
  178. if provider in _AGGREGATOR_PROVIDERS:
  179. return _prepend_vendor(name)
  180. # OpenCode Zen / Go are flat-namespace resellers: /v1/models returns bare IDs and inference 401s
  181. # vendor-prefixed names, so strip ANY leading ``vendor/`` (commonly copied from aggregator slugs).
  182. from hermes_cli.models import opencode_provider_family
  183. _oc_family = opencode_provider_family(provider)
  184. if _oc_family is not None:
  185. if "/" in name:
  186. name = name.split("/", 1)[1].strip() or name
  187. if _oc_family == "opencode-zen" and name.lower().startswith("claude-"):
  188. return _dots_to_hyphens(name)
  189. return name
  190. if provider in _DOT_TO_HYPHEN_PROVIDERS:
  191. bare = _strip_matching_provider_prefix(name, provider)
  192. return bare if "/" in bare else _dots_to_hyphens(bare)
  193. # A profile that owns its id space (alias table, dash-to-dot repair) answers first; a wrong
  194. # notation otherwise hits HTTP 400 model_not_supported. See issue #6879.
  195. from providers import get_provider_profile
  196. profile = get_provider_profile(provider)
  197. try:
  198. normalized = profile.normalize_model_id(name) if profile is not None else None
  199. except Exception: # a plugin hook failing must not break normalization: generic rules take over
  200. logging.getLogger(__name__).debug("normalize_model_id(%s) failed", provider, exc_info=True)
  201. normalized = None
  202. if normalized:
  203. return normalized
  204. if provider in _STRIP_VENDOR_ONLY_PROVIDERS:
  205. stripped = _strip_matching_provider_prefix(name, provider)
  206. if stripped == name and name.startswith(("openai/", "openai:")):
  207. return name[len("openai/"):] # openai-codex maps openai/gpt-5.4 and openai:gpt-5.4 -> gpt-5.4
  208. return stripped
  209. if provider == "deepseek":
  210. bare = _strip_matching_provider_prefix(name, provider)
  211. return bare if "/" in bare else _normalize_for_deepseek(bare)
  212. if provider in _MATCHING_PREFIX_STRIP_PROVIDERS:
  213. result = _strip_matching_provider_prefix(name, provider)
  214. return result.lower() if provider in _LOWERCASE_MODEL_PROVIDERS else result
  215. # Unknown names (a local NIM container, a proxied model) pass through untouched.
  216. if provider in _CATALOGUE_PREFIX_REPAIR_PROVIDERS:
  217. return _repair_prefix_from_catalogue(name, provider)
  218. # Authoritative native providers, custom and all others: pass through as-is.
  219. return name