tts_tool_openai.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """OpenAI-compatible TTS backends for ``tools.tts_tool``: OpenAI and DeepInfra.
  2. Also owns the managed-gateway (Nous portal ``openai-audio`` proxy) route selection that
  3. decides where the OpenAI client points. Seams defined on the origin module (``_load_tts_config``,
  4. ``_import_openai_client``, ``_resolve_provider_key``, ``_generate_openai_tts``) are resolved
  5. through :func:`_origin` at call time.
  6. """
  7. from __future__ import annotations
  8. import logging
  9. import uuid
  10. from typing import Any, Dict, Optional
  11. from urllib.parse import urljoin
  12. from tools.managed_tool_gateway import resolve_managed_tool_gateway
  13. from tools.tool_backend_helpers import (
  14. NOUS_MANAGED_PROVIDER, managed_nous_tools_enabled, nous_tool_gateway_unavailable_message,
  15. read_selection, resolve_openai_audio_api_key, selection_error)
  16. from tools.tts_tool_delivery import _origin, _section
  17. from tools.tts_tool_providers import _tts_response_format_from_path
  18. logger = logging.getLogger("tools.tts_tool")
  19. DEFAULT_OPENAI_MODEL = "gpt-4o-mini-tts"
  20. # Speech models the managed Nous audio gateway serves (it bills a model only when NAS has a priced
  21. # rule for it, so anything else fails closed). Others are coerced to DEFAULT_OPENAI_MODEL.
  22. MANAGED_OPENAI_TTS_MODELS = frozenset({"gpt-4o-mini-tts", "tts-1", "tts-1-hd"})
  23. DEFAULT_OPENAI_VOICE = "alloy"
  24. DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1"
  25. # DeepInfra base URL is resolved via hermes_cli.models.deepinfra_base_url (shared).
  26. DEFAULT_DEEPINFRA_TTS_VOICE = "default"
  27. def _managed_openai_audio_route() -> Optional[tuple]:
  28. gateway = resolve_managed_tool_gateway("openai-audio")
  29. if gateway is None:
  30. return None
  31. return gateway.nous_user_token, urljoin(f"{gateway.gateway_origin.rstrip('/')}/", "v1"), True
  32. def _resolve_openai_audio_client_config() -> tuple[str, str, bool]:
  33. """``(api_key, base_url, is_managed)`` for the OpenAI audio client (``is_managed`` = the restricted
  34. Nous proxy, so callers coerce the request). Strict on the stored ``tts`` selection: ``"nous"``
  35. → managed ONLY (error if unavailable); any other → direct credentials ONLY (``tts.openai.api_key``
  36. then ``VOICE_TOOLS_OPENAI_KEY``/``OPENAI_API_KEY``); unset → config key → env key → managed."""
  37. origin = _origin()
  38. openai_cfg = _section(origin._load_tts_config(), "openai")
  39. selected = read_selection("tts")
  40. if selected == NOUS_MANAGED_PROVIDER:
  41. route = _managed_openai_audio_route()
  42. if route is None:
  43. raise ValueError(selection_error(
  44. "tts", NOUS_MANAGED_PROVIDER,
  45. "the Nous Tool Gateway is not available (not entitled or unreachable)"))
  46. return route
  47. direct_api_key = openai_cfg.get("api_key") or resolve_openai_audio_api_key()
  48. if direct_api_key:
  49. return direct_api_key, openai_cfg.get("base_url") or DEFAULT_OPENAI_BASE_URL, False
  50. if selected is not None:
  51. raise ValueError(selection_error(
  52. "tts", selected,
  53. "neither tts.openai.api_key in config nor VOICE_TOOLS_OPENAI_KEY/OPENAI_API_KEY is set",
  54. ))
  55. route = _managed_openai_audio_route()
  56. if route is None:
  57. message = "Neither tts.openai.api_key in config nor VOICE_TOOLS_OPENAI_KEY/OPENAI_API_KEY is set"
  58. if managed_nous_tools_enabled():
  59. message += ". " + nous_tool_gateway_unavailable_message("managed OpenAI audio for TTS")
  60. raise ValueError(message)
  61. return route
  62. def _has_openai_audio_backend() -> bool:
  63. """Return True when the selected OpenAI audio route is usable."""
  64. try:
  65. _resolve_openai_audio_client_config()
  66. return True
  67. except ValueError:
  68. return False
  69. def _openai_extra_body(oai_config: dict[str, Any]) -> dict[str, Any]:
  70. """Optional ``tts.openai`` fields OpenAI-compatible servers read from the JSON body: ``language``
  71. (sent as ``lang_code``) and ``consent_attestation`` (cloned voices). Unset keys are omitted so
  72. the official API and strict servers never see unknown fields."""
  73. extra_body: dict[str, Any] = {}
  74. if oai_config.get("language"):
  75. extra_body["lang_code"] = oai_config["language"]
  76. if oai_config.get("consent_attestation"):
  77. extra_body["consent_attestation"] = oai_config["consent_attestation"]
  78. return extra_body
  79. def _generate_openai_tts(
  80. text: str, output_path: str, tts_config: dict[str, Any], *, api_key: Optional[str] = None,
  81. base_url: Optional[str] = None, model: Optional[str] = None, voice: Optional[str] = None,
  82. speed: Optional[float] = None, instructions: Optional[str] = None) -> str:
  83. """Generate audio via the OpenAI ``audio.speech.create`` SDK shape.
  84. Explicit kwargs let OpenAI-compatible backends (DeepInfra) supply credentials/model/voice
  85. and skip the managed-gateway resolution; otherwise the OpenAI auth chain and ``tts.openai``
  86. (speed falling back to ``tts.speed``) apply. ``instructions`` is forwarded only when truthy
  87. so ``tts-1`` and strict OpenAI-compatible servers that reject unknown kwargs are unaffected."""
  88. fallback_base: Optional[str] = None
  89. is_managed = False
  90. explicit_base_url = base_url is not None
  91. if api_key is None:
  92. api_key, fallback_base, is_managed = _resolve_openai_audio_client_config()
  93. oai_config = _section(tts_config, "openai")
  94. if model is None:
  95. model = oai_config.get("model", DEFAULT_OPENAI_MODEL)
  96. if voice is None:
  97. voice = oai_config.get("voice", DEFAULT_OPENAI_VOICE)
  98. config_base_url = oai_config.get("base_url")
  99. if base_url is None: # config override beats the auth-chain fallback; explicit arg wins
  100. base_url = config_base_url or fallback_base or DEFAULT_OPENAI_BASE_URL
  101. if speed is None:
  102. speed_default = tts_config.get("speed", 1.0) if isinstance(tts_config, dict) else 1.0
  103. speed = float(oai_config.get("speed", speed_default))
  104. # The managed gateway only serves MANAGED_OPENAI_TTS_MODELS; coerce any other model
  105. # unless the user redirected base_url to their own endpoint.
  106. if is_managed and not explicit_base_url and not config_base_url and model not in MANAGED_OPENAI_TTS_MODELS:
  107. logger.warning(
  108. "TTS: managed OpenAI audio gateway does not support model %r; "
  109. "falling back to %s. Set VOICE_TOOLS_OPENAI_KEY or OPENAI_API_KEY "
  110. "to use %r directly.",
  111. model, DEFAULT_OPENAI_MODEL, model)
  112. model = DEFAULT_OPENAI_MODEL
  113. create_kwargs: dict[str, Any] = {
  114. "model": model, "voice": voice, "input": text,
  115. "response_format": _tts_response_format_from_path(output_path),
  116. "extra_headers": {"x-idempotency-key": str(uuid.uuid4())}}
  117. if speed != 1.0:
  118. create_kwargs["speed"] = max(0.25, min(4.0, speed))
  119. if instructions:
  120. create_kwargs["instructions"] = instructions
  121. if extra_body := _openai_extra_body(oai_config):
  122. create_kwargs["extra_body"] = extra_body
  123. client = _origin()._import_openai_client()(api_key=api_key, base_url=base_url)
  124. try:
  125. client.audio.speech.create(**create_kwargs).stream_to_file(output_path)
  126. return output_path
  127. finally:
  128. close = getattr(client, "close", None)
  129. if callable(close):
  130. close()
  131. def _generate_deepinfra_tts(text: str, output_path: str, tts_config: dict[str, Any]) -> str:
  132. """Resolve DeepInfra credentials/model (live ``hermes_cli.models`` catalog, no hardcoded ids), then
  133. delegate to the OpenAI-compatible handler."""
  134. api_key = _origin()._resolve_provider_key("DEEPINFRA_API_KEY", "deepinfra")
  135. if not api_key:
  136. raise ValueError("DEEPINFRA_API_KEY not set. Run `hermes setup` to configure, or set the env var directly.")
  137. di_config = _section(tts_config, "deepinfra")
  138. from hermes_cli.models import deepinfra_base_url, deepinfra_model_ids
  139. model = di_config.get("model")
  140. if not isinstance(model, str) or not model.strip():
  141. candidates = deepinfra_model_ids("tts")
  142. if not candidates:
  143. raise ValueError(
  144. "No DeepInfra TTS model available. Pin one in config.yaml "
  145. "under tts.deepinfra.model, or check connectivity to "
  146. "api.deepinfra.com so the live catalog can be fetched.")
  147. model = candidates[0]
  148. return _origin()._generate_openai_tts(
  149. text, output_path, tts_config, api_key=api_key, base_url=deepinfra_base_url(di_config),
  150. model=model, voice=di_config.get("voice", DEFAULT_DEEPINFRA_TTS_VOICE),
  151. speed=float(di_config.get("speed", tts_config.get("speed", 1.0))))