__init__.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """DeepSeek provider profile.
  2. V4 defaults to thinking ON when ``extra_body.thinking`` is unset, and then
  3. requires ``reasoning_content`` to be echoed back on later turns (HTTP 400 after
  4. the first tool call otherwise). This profile sets ``thinking`` explicitly and
  5. maps effort onto DeepSeek's ``reasoning_effort``; V3 models are left untouched.
  6. Retired ``deepseek-chat``/``deepseek-reasoner`` IDs are remapped in
  7. ``hermes_cli.model_normalize`` before reaching here.
  8. """
  9. from typing import Any
  10. from agent.reasoning_effort import DEEPSEEK_V4_EFFORTS, DEEPSEEK_V4_OVERRIDES, thinking_toggle_extras
  11. from providers import register_provider
  12. from providers.base import ProviderProfile
  13. # Version-less canonical ids for thinking-capable DeepSeek models. The 2026-09 Flash
  14. # refresh dropped the ``v<N>`` marker from the public id: ``GET /v1/models`` reports
  15. # ``deepseek-flash`` and the API accepts it directly, so the generation check in
  16. # ``build_api_kwargs_extras`` cannot recognise it.
  17. _THINKING_CAPABLE_IDS: frozenset[str] = frozenset({"deepseek-flash"})
  18. class DeepSeekProfile(ProviderProfile):
  19. """DeepSeek — extra_body.thinking + top-level reasoning_effort."""
  20. def build_api_kwargs_extras(
  21. self, *, reasoning_config: dict | None = None, model: str | None = None, **context
  22. ) -> tuple[dict[str, Any], dict[str, Any]]:
  23. m = (model or "").strip().lower()
  24. # v4+ only; v3 excluded. Version-less canonicals (``deepseek-flash``) carry the
  25. # same thinking-mode contract but no ``v<N>`` prefix, so consult the id set too —
  26. # missing them makes Hermes omit ``thinking``, so the server defaults to on and
  27. # the user's thinking toggle / effort setting is silently ignored.
  28. versioned_v4_plus = m.startswith("deepseek-v") and not m.startswith("deepseek-v3")
  29. if not versioned_v4_plus and m not in _THINKING_CAPABLE_IDS:
  30. return {}, {}
  31. # Always set thinking explicitly (default enabled, matching the API default)
  32. # to avoid the reasoning_content echo trap on subsequent turns.
  33. extras, top_level = thinking_toggle_extras(
  34. reasoning_config, DEEPSEEK_V4_EFFORTS, DEEPSEEK_V4_OVERRIDES, always_emit_toggle=True
  35. )
  36. # ``{effort: "none"|"false"|"disabled"}`` is also an explicit disable:
  37. # UI/session surfaces report Off from the effort field even when
  38. # ``enabled`` is missing or still True (#107238).
  39. rc = reasoning_config
  40. effort = (rc.get("effort") or "").strip().lower() if isinstance(rc, dict) else ""
  41. if rc is not None and effort in {"none", "false", "disabled"}:
  42. return {"thinking": {"type": "disabled"}}, {}
  43. return extras, top_level
  44. deepseek = DeepSeekProfile(
  45. name="deepseek", aliases=("deepseek-chat", "deep-seek"), env_vars=("DEEPSEEK_API_KEY",), display_name="DeepSeek",
  46. description="DeepSeek — native DeepSeek API", signup_url="https://platform.deepseek.com/",
  47. fallback_models=("deepseek-v4-pro", "deepseek-flash"), base_url="https://api.deepseek.com/v1",
  48. default_aux_model="deepseek-flash",
  49. # Native API implements only ``json_object`` (https://api-docs.deepseek.com/guides/json_mode);
  50. # ``json_schema`` is a guaranteed HTTP 400 "This response_format type is unavailable now".
  51. unsupported_response_formats=("json_schema",),
  52. )
  53. register_provider(deepseek)