__init__.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """DeepInfra provider profile (chat surface; image-gen/TTS/STT are wired via
  2. their own plugin subsystems)."""
  3. from typing import Any
  4. from agent.reasoning_effort import OPENAI_COMPAT_WIRE_EFFORTS, clamp_effort, requested_effort
  5. from providers import register_provider
  6. from providers.base import ProviderProfile
  7. class _DeepInfraProfile(ProviderProfile):
  8. """DeepInfra profile with live vision-default discovery, so shared vision
  9. resolution in ``agent/auxiliary_client.py`` stays provider-agnostic."""
  10. def build_api_kwargs_extras(
  11. self, *, reasoning_config: dict | None = None, **context: Any
  12. ) -> tuple[dict[str, Any], dict[str, Any]]:
  13. """Map Hermes reasoning controls to DeepInfra's top-level ``reasoning_effort``.
  14. DeepInfra applies a per-model default when the field is absent (DeepSeek-V4.x off,
  15. GLM/Qwen-Thinking on), so ``none`` is the only working off switch and an unset effort
  16. is omitted rather than guessed. The core ``_supports_reasoning_extra_body`` allowlist
  17. does not know this host, so the transport always passes ``supports_reasoning=False``
  18. here — gating on it would make the method a permanent no-op (#111872).
  19. """
  20. if isinstance(reasoning_config, dict) and reasoning_config.get("enabled") is False:
  21. return {}, {"reasoning_effort": "none"}
  22. effort = requested_effort(reasoning_config)
  23. clamped = clamp_effort(effort, OPENAI_COMPAT_WIRE_EFFORTS)
  24. return ({}, {"reasoning_effort": clamped}) if clamped in OPENAI_COMPAT_WIRE_EFFORTS else ({}, {})
  25. def default_vision_model(self): # type: ignore[override]
  26. """First vision-capable *chat* model from the live catalog, or None. Key-gated so a box
  27. without DEEPINFRA_API_KEY never pays the round-trip; requires the ``chat`` surface tag so
  28. an image-gen model carrying a ``vision`` tag can't be picked as a chat vision backend."""
  29. from agent.secret_scope import get_secret
  30. if not (get_secret("DEEPINFRA_API_KEY") or "").strip():
  31. return None
  32. try:
  33. from hermes_cli.models import _fetch_deepinfra_models_by_tag
  34. items = _fetch_deepinfra_models_by_tag("chat")
  35. except Exception:
  36. return None
  37. for item in items or []:
  38. metadata = item.get("metadata") or {}
  39. tags = metadata.get("tags") if isinstance(metadata, dict) else None
  40. if isinstance(tags, list) and "vision" in tags and item.get("id"):
  41. return item["id"]
  42. return None
  43. deepinfra = _DeepInfraProfile(
  44. name="deepinfra", aliases=("deep-infra", "deepinfra-ai"), display_name="DeepInfra",
  45. description="DeepInfra — 100+ open models, pay-per-use", signup_url="https://deepinfra.com/dash/api_keys",
  46. env_vars=("DEEPINFRA_API_KEY", "DEEPINFRA_BASE_URL"), base_url="https://api.deepinfra.com/v1/openai",
  47. auth_type="api_key",
  48. default_max_tokens=None, # DeepInfra applies its documented per-model limit
  49. # The only hardcoded DeepInfra model: aux resolution is synchronous, so it
  50. # can't wait on a catalog round-trip. Everything else is discovered live.
  51. default_aux_model="deepseek-ai/DeepSeek-V4-Flash",
  52. # Empty on purpose: the live catalog is the source of truth; an empty picker
  53. # beats silently routing to a retired model.
  54. fallback_models=(),
  55. )
  56. register_provider(deepinfra)