__init__.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """Kimi / Moonshot provider profiles (chat_completions path; sk-kimi-* keys are
  2. redirected to api.kimi.com/coding by core)."""
  3. from typing import Any
  4. from urllib.parse import urlparse
  5. from agent.reasoning_effort import KIMI_K3_EFFORTS, KIMI_K3_OVERRIDES, thinking_toggle_extras
  6. from hermes_cli.version_info import hermes_user_agent
  7. from providers import register_provider
  8. from providers.base import OMIT_TEMPERATURE, LazyHeaders, ProviderProfile
  9. _HEADERS = {
  10. "HTTP-Referer": "https://hermes-agent.nousresearch.com",
  11. "X-Title": "Hermes Agent",
  12. "User-Agent": lambda: hermes_user_agent("HermesAgent"),
  13. # Exclude brotli: httpx's brotlicffi backend has a streaming decode bug on
  14. # Moonshot's content-encoding: br SSE responses (#28043, #48428, #59556).
  15. # gzip sidesteps it while still compressing the transfer.
  16. "Accept-Encoding": "gzip",
  17. }
  18. def _is_confirmed_kimi_coding_url(base_url: str) -> bool:
  19. """True only for Kimi Code's canonical HTTPS API surfaces."""
  20. try:
  21. p = urlparse(base_url)
  22. port = p.port
  23. except ValueError:
  24. return False
  25. return (
  26. p.scheme.lower() == "https" and (p.hostname or "").lower() == "api.kimi.com" and port in (None, 443)
  27. and p.username is None and p.password is None
  28. and p.path.rstrip("/") in {"/coding", "/coding/v1"} and not p.query and not p.fragment
  29. )
  30. class KimiProfile(ProviderProfile):
  31. """Kimi/Moonshot — temperature omitted, thinking xor reasoning_effort."""
  32. def fetch_models(
  33. self, *, api_key: str | None = None, base_url: str | None = None, timeout: float = 8.0
  34. ) -> list[str] | None:
  35. """Use Kimi Code's OpenAI-compatible surface for model discovery; the bare
  36. ``k3`` slug is only served there, so it is filtered off other endpoints."""
  37. effective_base = (base_url or self.base_url or "").rstrip("/")
  38. confirmed_coding_endpoint = _is_confirmed_kimi_coding_url(effective_base)
  39. if confirmed_coding_endpoint and urlparse(effective_base).path.rstrip("/") == "/coding":
  40. effective_base += "/v1"
  41. models = super().fetch_models(api_key=api_key, base_url=effective_base or None, timeout=timeout)
  42. if models is None or confirmed_coding_endpoint:
  43. return models
  44. return [model for model in models if model.strip().lower() != "k3"]
  45. def build_api_kwargs_extras(
  46. self, *, reasoning_config: dict | None = None, model: str | None = None, **context
  47. ) -> tuple[dict[str, Any], dict[str, Any]]:
  48. """Moonshot treats extra_body.thinking and reasoning_effort as mutually
  49. exclusive (400 on both): send effort when requested, else the toggle. K2.x keeps
  50. replayed prior-turn reasoning_content only with ``thinking.keep: "all"`` (default null
  51. ignores it; platform.kimi.ai/docs/guide/use-thinking-models); K3 always keeps it."""
  52. extra_body, top_level = thinking_toggle_extras(reasoning_config, KIMI_K3_EFFORTS, KIMI_K3_OVERRIDES)
  53. thinking = extra_body.get("thinking")
  54. if isinstance(thinking, dict) and thinking.get("type") == "enabled" and "k2" in (model or "").lower():
  55. extra_body["thinking"] = {**thinking, "keep": "all"}
  56. return extra_body, top_level
  57. def _kimi(name: str, aliases: tuple, env_vars: tuple, base_url: str) -> KimiProfile:
  58. return KimiProfile(
  59. name=name, aliases=aliases, env_vars=env_vars, base_url=base_url,
  60. fixed_temperature=OMIT_TEMPERATURE, default_max_tokens=32000,
  61. default_headers=LazyHeaders(_HEADERS), default_aux_model="kimi-k2-turbo-preview",
  62. )
  63. kimi = _kimi("kimi-coding", ("kimi", "moonshot", "kimi-for-coding"), ("KIMI_API_KEY", "KIMI_CODING_API_KEY"),
  64. "https://api.moonshot.ai/v1")
  65. kimi_cn = _kimi("kimi-coding-cn", ("kimi-cn", "moonshot-cn"), ("KIMI_CN_API_KEY",), "https://api.moonshot.cn/v1")
  66. register_provider(kimi)
  67. register_provider(kimi_cn)