auxiliary_reasoning_floor.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """Reasoning floor for auxiliary requests: when a route refuses to switch reasoning OFF, step it UP.
  2. Aux lanes that want speed over thought (title generation: ``max_tokens=64``, JSON body) send the
  3. provider's thinking-off encoding — top-level ``reasoning_effort: "none"`` on the custom profile,
  4. ``extra_body.reasoning: {"enabled": false}`` on OpenRouter-shaped relays, ``_reasoning_config`` on the
  5. Anthropic Messages adapters. Some endpoints understand the field but refuse the disable ("Reasoning is
  6. mandatory for this endpoint and cannot be disabled" — the Nous Portal on gpt-6-astra); before this
  7. module every title call there 400'd and the session stayed untitled.
  8. The recovery is a *step up*, not a strip: the same request goes out again at the lowest effort every
  9. reasoning wire accepts (``low``; ``minimal`` is rejected by o-series and Claude), and the (route, model)
  10. pair is memoised so the next disabled-reasoning aux call on it starts at the floor instead of burning
  11. the guaranteed 400 first. Dropping the field would also succeed once, but it says nothing about the
  12. next call and hands the effort choice back to the provider default (often ``medium`` or higher, the
  13. opposite of what a thinking-off caller asked for).
  14. """
  15. from __future__ import annotations
  16. import logging
  17. from typing import Any, Dict, Optional
  18. from urllib.parse import urlparse
  19. logger = logging.getLogger(__name__)
  20. REASONING_FLOOR_EFFORT = "low"
  21. # (route key, model) pairs that refused a reasoning disable in this process.
  22. _FLOORED_ROUTES: set[tuple[str, str]] = set()
  23. _DISABLED_EFFORTS = {"none", "off", "disabled", "false", "0"}
  24. def _route_key(provider: Optional[str], base_url: Optional[str]) -> str:
  25. """Endpoint host:port when known (a base_url override turns a named provider into ``custom``), else
  26. the provider name — the same key shape ``auxiliary_structured_output`` uses."""
  27. return (urlparse(base_url or "").netloc or "").lower() or str(provider or "").strip().lower()
  28. def _is_disabled(reasoning_config: Any) -> bool:
  29. return isinstance(reasoning_config, dict) and reasoning_config.get("enabled") is False
  30. def floor_reasoning_config(reasoning_config: Any) -> dict[str, Any]:
  31. """The caller's disabled ``reasoning_config`` lifted to the floor; anything else returned as-is."""
  32. if _is_disabled(reasoning_config):
  33. return {"enabled": True, "effort": REASONING_FLOOR_EFFORT}
  34. return reasoning_config
  35. def with_reasoning_floor(kwargs: dict[str, Any]) -> Optional[dict[str, Any]]:
  36. """Copy of *kwargs* with every thinking-OFF encoding lifted to ``REASONING_FLOOR_EFFORT``:
  37. top-level ``reasoning_effort``, ``extra_body.reasoning`` (OpenRouter shape) and the adapter's private
  38. ``_reasoning_config``. ``None`` when nothing was disabled, so the ladder never re-sends an unchanged
  39. request."""
  40. changed = False
  41. retry = dict(kwargs)
  42. if str(retry.get("reasoning_effort", "")).strip().lower() in _DISABLED_EFFORTS:
  43. retry["reasoning_effort"] = REASONING_FLOOR_EFFORT
  44. changed = True
  45. if _is_disabled(retry.get("_reasoning_config")):
  46. retry["_reasoning_config"] = floor_reasoning_config(retry["_reasoning_config"])
  47. changed = True
  48. extra_body = retry.get("extra_body")
  49. if isinstance(extra_body, dict):
  50. reasoning = extra_body.get("reasoning")
  51. if _is_disabled(reasoning) or (
  52. isinstance(reasoning, dict) and str(reasoning.get("effort", "")).strip().lower() in _DISABLED_EFFORTS
  53. ):
  54. retry["extra_body"] = {**extra_body, "reasoning": {"enabled": True, "effort": REASONING_FLOOR_EFFORT}}
  55. changed = True
  56. return retry if changed else None
  57. def remember_reasoning_floor(
  58. provider: Optional[str], base_url: Optional[str], rejected_kwargs: dict[str, Any], error: BaseException,
  59. ) -> None:
  60. """Record that this route's ``rejected_kwargs["model"]`` refuses to disable reasoning (the ladder
  61. calls this after the stepped-up retry succeeded)."""
  62. _FLOORED_ROUTES.add((_route_key(provider, base_url), str(rejected_kwargs.get("model") or "")))
  63. _NOUS_PROVIDERS = {"nous", "nous-portal", "nousresearch"}
  64. def _catalog_marks_mandatory(provider: Optional[str], base_url: Optional[str], model: Optional[str]) -> bool:
  65. """True when the route's ``/v1/models`` catalog (OpenRouter, Nous Portal) flags *model*
  66. ``reasoning.mandatory``. Cache-only — memory, then the disk mirror — so it never blocks; a cold
  67. catalog is warmed in the background, and the mirror it writes answers every later call and process.
  68. Without this an aux-only OpenRouter route (nothing else warms that catalog) paid the 400 in every
  69. process."""
  70. provider_norm = str(provider or "").strip().lower()
  71. host = (urlparse(base_url or "").hostname or "").lower()
  72. from hermes_cli import models_reasoning_caps as caps_mod
  73. if provider_norm == "openrouter" or host == "openrouter.ai" or host.endswith(".openrouter.ai"):
  74. lookup, warm = caps_mod.openrouter_model_reasoning_capabilities, caps_mod.warm_openrouter_reasoning_caps_async
  75. elif provider_norm in _NOUS_PROVIDERS:
  76. lookup, warm = caps_mod.nous_model_reasoning_capabilities, caps_mod.warm_nous_reasoning_caps_async
  77. else:
  78. return False
  79. try:
  80. caps = lookup(model)
  81. if caps is None:
  82. warm()
  83. except Exception:
  84. return False
  85. return bool(caps and caps.get("mandatory"))
  86. def known_reasoning_floor(
  87. reasoning_config: Any, provider: Optional[str], base_url: Optional[str], model: Optional[str],
  88. task: Optional[str] = None,
  89. ) -> Any:
  90. """*reasoning_config* lifted to the floor when this route+model is known to refuse a disable — learned
  91. from an earlier 400 in this process, or flagged mandatory by the route's model catalog; unchanged
  92. otherwise. Runs before the profile projection so every wire shape starts at the floor."""
  93. if not _is_disabled(reasoning_config):
  94. return reasoning_config
  95. if (_route_key(provider, base_url), str(model or "")) not in _FLOORED_ROUTES and not _catalog_marks_mandatory(
  96. provider, base_url, model,
  97. ):
  98. return reasoning_config
  99. logger.info(
  100. "Auxiliary %s: %s (%s) cannot disable reasoning; sending effort=%s up front",
  101. task or "call", _route_key(provider, base_url) or "provider", model or "model", REASONING_FLOOR_EFFORT,
  102. )
  103. return floor_reasoning_config(reasoning_config)