lmstudio_reasoning.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """LM Studio reasoning-effort resolution (used by the lmstudio provider profile). LM Studio
  2. publishes per-model
  3. ``capabilities.reasoning.allowed_options`` (``["off","on"]`` for toggle models,
  4. ``["off","minimal","low"]`` for graduated ones); the user's ``reasoning_config`` is
  5. mapped onto LM Studio's vocabulary, then clamped to the allowed set so the server
  6. doesn't 400."""
  7. from __future__ import annotations
  8. from typing import List, Optional
  9. _LM_VALID_EFFORTS = {"none", "minimal", "low", "medium", "high", "xhigh"}
  10. # Toggle vocabulary → request vocabulary; also applied to published allowed_options.
  11. _LM_EFFORT_ALIASES = {"off": "none", "on": "medium"}
  12. # Hermes' ladder grew past LM Studio's vocabulary ("max", "ultra"); without this
  13. # ceiling clamp they'd fall to the "medium" default (more yields less than "xhigh").
  14. # Separate from _LM_EFFORT_ALIASES, which must not rewrite allowed_options.
  15. _LM_EFFORT_CLAMP = {"max": "xhigh", "ultra": "xhigh"}
  16. def resolve_lmstudio_effort(reasoning_config: Optional[dict], allowed_options: Optional[list[str]]) -> Optional[str]:
  17. """Return the ``reasoning_effort`` to send to LM Studio, or ``None`` = omit the
  18. field (the user picked a level the model can't honor, so LM Studio falls back
  19. to the model's declared default rather than a silently substituted effort).
  20. Falsy ``allowed_options`` (probe failed) skips clamping."""
  21. effort = "medium"
  22. if reasoning_config and isinstance(reasoning_config, dict):
  23. if reasoning_config.get("enabled") is False:
  24. effort = "none"
  25. else:
  26. raw = (reasoning_config.get("effort") or "").strip().lower()
  27. raw = _LM_EFFORT_ALIASES.get(raw, raw)
  28. raw = _LM_EFFORT_CLAMP.get(raw, raw)
  29. if raw in _LM_VALID_EFFORTS:
  30. effort = raw
  31. if allowed_options and effort not in {_LM_EFFORT_ALIASES.get(opt, opt) for opt in allowed_options}:
  32. return None
  33. return effort