__init__.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Custom / Ollama (local) provider profile: any endpoint registered as
  2. provider="custom" (Ollama, vLLM, llama.cpp, GLM-5.2 on ARK, …)."""
  3. from typing import Any
  4. from urllib.parse import urlparse
  5. from agent.reasoning_effort import OPENAI_COMPAT_WIRE_EFFORTS, clamp_effort
  6. from providers import register_provider
  7. from providers.base import ProviderProfile
  8. from utils import base_url_host_matches
  9. def _looks_like_ollama_endpoint(base_url: str | None) -> bool:
  10. """True only for explicit Ollama signatures (port 11434 or an ``ollama`` host label).
  11. ``think`` is Ollama-native; strict hosts (Mistral, Groq) 422 on it, and
  12. arbitrary localhost may be llama.cpp / vLLM / LM Studio."""
  13. raw = (base_url or "").strip()
  14. if not raw:
  15. return False
  16. parsed = urlparse(raw if "://" in raw else f"//{raw}")
  17. try: # urlparse raises ValueError on malformed ports ("host:99999"); treat as not-Ollama.
  18. if parsed.port == 11434:
  19. return True
  20. except ValueError:
  21. return False
  22. host = (parsed.hostname or "").lower().rstrip(".")
  23. return bool(host) and (host == "ollama.com" or host.endswith(".ollama.com") or "ollama" in host.split("."))
  24. class CustomProfile(ProviderProfile):
  25. """Custom/Ollama local provider — think=false and num_ctx support."""
  26. def supported_reasoning_efforts(self, model: str | None) -> tuple[str, ...]:
  27. """The OpenAI-compat wire set, mirroring this profile's own chat-completions clamp.
  28. Without this declaration the Responses transport clamps onto the OpenAI
  29. per-model ladder (``codex_supported_efforts``), where ``max`` is gpt-5.6-only —
  30. so a custom relay's model had a configured ``max`` silently demoted to
  31. ``xhigh`` while the same provider over chat-completions forwarded ``max``
  32. unchanged (#114249). A custom endpoint's vocabulary is undiscoverable, so
  33. the widest OpenAI-compat set is the honest ceiling; ``ultra`` still clamps
  34. to ``max`` via the shared ``clamp_effort`` policy.
  35. """
  36. return OPENAI_COMPAT_WIRE_EFFORTS
  37. def default_reasoning_config(self, model: str | None = None) -> dict | None:
  38. """Unset ``agent.reasoning_effort`` → ``medium``, as on the Nous / OpenRouter profiles.
  39. Leaving the field off lets the endpoint's own default apply, and for a hosted reasoning
  40. model that default can be its ceiling: kimi-k3 behind an OpenAI-compatible relay defaults
  41. to ``max`` — 3x the reasoning tokens and ~3x the latency of medium. The agent skips this
  42. default for models the catalog marks non-reasoning (``agent.reasoning_params``).
  43. """
  44. return {"enabled": True, "effort": "medium"}
  45. def build_api_kwargs_extras(
  46. self, *, reasoning_config: dict | None = None, ollama_num_ctx: int | None = None, **ctx: Any
  47. ) -> tuple[dict[str, Any], dict[str, Any]]:
  48. extra_body: dict[str, Any] = {}
  49. top_level: dict[str, Any] = {}
  50. if ollama_num_ctx:
  51. extra_body["options"] = {"num_ctx": ollama_num_ctx}
  52. # disabled -> top-level reasoning_effort="none" (Ollama's /v1 ignores
  53. # extra_body.think) plus think=False only on Ollama URLs; enabled+effort ->
  54. # top-level reasoning_effort clamped to the OpenAI-compat wire (GLM/ARK,
  55. # vLLM and SGLang all top out at "max"; "ultra" verbatim 400s); None ->
  56. # omit so the server default applies (auxiliary calls without an effort, and
  57. # the main loop after the route rejected the reasoning field — an unset main
  58. # effort arrives here already filled by default_reasoning_config). Never emit
  59. # think=True (Ollama-only flag).
  60. if reasoning_config and isinstance(reasoning_config, dict):
  61. effort = (reasoning_config.get("effort") or "").strip().lower()
  62. if effort == "none" or reasoning_config.get("enabled", True) is False:
  63. # See #14820.
  64. top_level["reasoning_effort"] = "none"
  65. if _looks_like_ollama_endpoint(ctx.get("base_url")):
  66. extra_body["think"] = False
  67. elif effort and base_url_host_matches(str(ctx.get("base_url") or ""), "api.groq.com"):
  68. # Groq's OpenAI-compatible wire accepts top-level reasoning_effort only as
  69. # "none" / "default"; any graded level ("medium", "high") 400s (#75089).
  70. top_level["reasoning_effort"] = "default"
  71. elif effort:
  72. top_level["reasoning_effort"] = clamp_effort(effort, OPENAI_COMPAT_WIRE_EFFORTS)
  73. return extra_body, top_level
  74. def fetch_models(
  75. self, *, api_key: str | None = None, base_url: str | None = None, timeout: float = 8.0
  76. ) -> list[str] | None:
  77. """base_url is user-configured; fetch only if set."""
  78. if not (base_url or self.base_url):
  79. return None
  80. return super().fetch_models(api_key=api_key, base_url=base_url, timeout=timeout)
  81. custom = CustomProfile(
  82. name="custom", aliases=("ollama", "local", "vllm", "llamacpp", "llama.cpp", "llama-cpp"),
  83. env_vars=(), # No fixed key — custom endpoint
  84. base_url="", # User-configured
  85. # An arbitrary client ceiling can exceed a local server's actual output limit.
  86. # The endpoint owns its generation default.
  87. )
  88. register_provider(custom)