__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """ZAI / GLM provider profile.
  2. GLM-4.5+ defaults to thinking ON, so ``reasoning_config`` is translated to
  3. ``extra_body.thinking``; GLM-5.2/5.3 also take a native ``reasoning_effort``.
  4. """
  5. import re
  6. from typing import Any
  7. from agent import reasoning_effort as re_
  8. from providers import register_provider
  9. from providers.base import ProviderProfile
  10. _GLM_VERSION_RE = re.compile(r"^glm-(\d+)(?:\.(\d+))?")
  11. # Alias spellings seen on relays (Fireworks ``glm-5p2``, ``zai-org-glm-5-2``…).
  12. _GLM_5_3_TOKENS = ("glm-5.3", "glm-5-3", "glm-5p3")
  13. _GLM_5_2_TOKENS = ("glm-5.2", "glm-5-2", "glm-5p2") + _GLM_5_3_TOKENS
  14. def _model_supports_thinking(model: str | None) -> bool:
  15. """GLM thinking-capable model families: glm-4.5 and later (4.5, 4.6, 5…)."""
  16. match = _GLM_VERSION_RE.match((model or "").strip().lower())
  17. return bool(match) and (int(match.group(1)), int(match.group(2) or 0)) >= (4, 5)
  18. def _has_token(model: str | None, tokens: tuple[str, ...]) -> bool:
  19. m = (model or "").strip().lower()
  20. return any(token in m for token in tokens)
  21. def _glm_5_2_reasoning_effort(reasoning_config: dict | None, *, model: str | None = None) -> str | None:
  22. """Hermes effort -> GLM vocabulary (5.2: high/max; 5.3: low..max). Below-floor
  23. efforts clamp to the floor; disabled/unset leaves the server default."""
  24. effort = re_.requested_effort(reasoning_config)
  25. if effort is None or effort == "none":
  26. return None
  27. if _has_token(model, _GLM_5_3_TOKENS):
  28. efforts, overrides, floor = re_.GLM53_EFFORTS, re_.GLM53_OVERRIDES, "low"
  29. else:
  30. efforts, overrides, floor = re_.GLM52_EFFORTS, re_.GLM52_OVERRIDES, "high"
  31. clamped = re_.clamp_effort(effort, efforts, overrides)
  32. return clamped if clamped in efforts else floor
  33. class ZaiProfile(ProviderProfile):
  34. """Z.AI / GLM — extra_body.thinking on/off + GLM-5.2 reasoning_effort."""
  35. def build_api_kwargs_extras(
  36. self, *, reasoning_config: dict | None = None, model: str | None = None, **context
  37. ) -> tuple[dict[str, Any], dict[str, Any]]:
  38. extra_body: dict[str, Any] = {}
  39. top_level: dict[str, Any] = {}
  40. is_5_2 = _has_token(model, _GLM_5_2_TOKENS)
  41. if not _model_supports_thinking(model) and not is_5_2:
  42. return extra_body, top_level
  43. # Only emit when the user expressed a preference (server default = enabled).
  44. if isinstance(reasoning_config, dict):
  45. enabled = reasoning_config.get("enabled") is not False
  46. if not enabled and _has_token(model, _GLM_5_3_TOKENS):
  47. # GLM-5.3 rejects disabled thinking; low is its lightest supported mode.
  48. enabled = True
  49. top_level["reasoning_effort"] = "low"
  50. # Preserved Thinking: with thinking on, the standard endpoint drops replayed prior-turn
  51. # reasoning_content unless clear_thinking is false (docs.z.ai/guides/capabilities/thinking-mode).
  52. extra_body["thinking"] = {"type": "enabled", "clear_thinking": False} if enabled else {"type": "disabled"}
  53. if is_5_2:
  54. effort = _glm_5_2_reasoning_effort(reasoning_config, model=model)
  55. if effort is not None:
  56. top_level["reasoning_effort"] = effort
  57. return extra_body, top_level
  58. zai = ZaiProfile(
  59. name="zai", aliases=("glm", "z-ai", "z.ai", "zhipu"),
  60. env_vars=("GLM_API_KEY", "ZAI_API_KEY", "Z_AI_API_KEY"), display_name="Z.AI (GLM)",
  61. description="Z.AI / GLM — Zhipu AI models", signup_url="https://z.ai/",
  62. fallback_models=("glm-5.2", "glm-5", "glm-4-9b"), base_url="https://api.z.ai/api/paas/v4",
  63. default_aux_model="glm-4.5-flash",
  64. )
  65. register_provider(zai)