__init__.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Nous Portal provider profile."""
  2. from typing import Any
  3. from agent.portal_tags import get_affinity_scope, get_conversation_context, nous_portal_tags
  4. from agent.transports.codex import _cache_scope_from_session_id
  5. from providers import register_provider
  6. from providers.base import ProviderProfile
  7. class NousProfile(ProviderProfile):
  8. """Nous Portal — product tags, reasoning with Nous-specific omission."""
  9. def resolve_aux_model(self, *, vision: bool = False) -> str:
  10. """Portal's tier-aware ``/api/nous/recommended-models`` pick (cached, offline-safe)."""
  11. try:
  12. from hermes_cli.models import get_nous_recommended_aux_model
  13. return get_nous_recommended_aux_model(vision=vision) or ""
  14. except Exception:
  15. return ""
  16. def build_extra_body(
  17. self, *, session_id: str | None = None, data_use: frozenset[str] = frozenset(), **context,
  18. ) -> dict[str, Any]:
  19. body: dict[str, Any] = {"tags": nous_portal_tags(session_id=session_id)}
  20. # Top-level session_id = sticky routing key, so Anthropic-style cache
  21. # breakpoints stay warm on one upstream instance. Resolved like the
  22. # ``conversation=`` tag: declared scope, then the ambient lineage ROOT
  23. # (covers aux call sites that pass no session_id), then the explicit argument.
  24. sticky_key = _cache_scope_from_session_id(get_affinity_scope() or get_conversation_context() or session_id)
  25. if sticky_key:
  26. body["session_id"] = sticky_key
  27. # The Portal routes centrally and 400s on caller routing fields, except data_collection and
  28. # zdr: sent only when an outgoing connector result requires them (``data_use``), never from config.
  29. if data_use:
  30. body["provider"] = {"data_collection": "deny", **({"zdr": True} if "zero_retention" in data_use else {})}
  31. return body
  32. @staticmethod
  33. def _cannot_disable_reasoning(model: str | None) -> bool:
  34. """True when ``reasoning: {enabled: false}`` would 400 on *model*. Cache-only catalog
  35. lookup; unknown/cold (warmer kicked) and no-reasoning routes both answer True (omit > 400)."""
  36. try:
  37. from hermes_cli.models_reasoning_caps import nous_model_reasoning_capabilities, warm_nous_reasoning_caps_async
  38. caps = nous_model_reasoning_capabilities(model)
  39. if caps is None:
  40. warm_nous_reasoning_caps_async()
  41. return True
  42. except Exception:
  43. return True
  44. return not caps.get("supports_reasoning") or bool(caps.get("mandatory"))
  45. def build_api_kwargs_extras(
  46. self, *, reasoning_config: dict | None = None, supports_reasoning: bool = False,
  47. model: str | None = None, **context,
  48. ) -> tuple[dict[str, Any], dict[str, Any]]:
  49. """Pass the full reasoning_config, disable included (the Portal honors it;
  50. omitting it means the upstream default, thinking ON for V4-class models)."""
  51. if not supports_reasoning:
  52. return {}, {}
  53. if reasoning_config is None:
  54. return {"reasoning": {"enabled": True, "effort": "medium"}}, {}
  55. rc = dict(reasoning_config)
  56. if rc.get("enabled") is False and self._cannot_disable_reasoning(model):
  57. return {}, {}
  58. return {"reasoning": rc}, {}
  59. nous = NousProfile(
  60. name="nous", aliases=("nous-portal", "nousresearch"), env_vars=("NOUS_API_KEY",),
  61. display_name="Nous Research", description="Nous Research — Hermes model family",
  62. signup_url="https://nousresearch.com/", fallback_models=("hermes-3-405b", "hermes-3-70b"),
  63. base_url="https://inference-api.nousresearch.com/v1", auth_type="oauth_device_code",
  64. )
  65. register_provider(nous)