Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 73 additions & 10 deletions plugins/model-providers/custom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,70 @@
Ollama instances and OpenAI-compatible reasoning endpoints (GLM-5.2 on
Volcengine ARK, vLLM, llama.cpp). Key quirks:
- ollama_num_ctx → extra_body.options.num_ctx (local context window)
- reasoning_config disabled → top-level reasoning_effort="none"
- reasoning_config disabled + local endpoint → top-level reasoning_effort="none"
(Ollama /v1/chat/completions ignores think=False — ollama#14820)
+ extra_body.think = False for /api/chat and proxies
Remote endpoints get neither (they reject "none" as invalid).
- reasoning_config enabled + effort → top-level reasoning_effort
(the native OpenAI-compatible format GLM/ARK expect; unset omits it
so the endpoint's server default applies)
"""

import ipaddress
from typing import Any
from urllib.parse import urlparse

from providers import register_provider
from providers.base import ProviderProfile


def _is_local_endpoint(base_url: str) -> bool:
"""Return True if base_url points to a local/private/LAN endpoint.

Checks for loopback (v4 + v6), private LAN ranges, and .local domains.
Avoids false positives from remote URLs that happen to contain substrings
like 'localhost' (e.g. https://localhost.example.com).
"""
if not base_url:
return False
base_url = base_url.strip()
if not base_url:
return False

# Parse URL properly instead of substring matching
if not (base_url.startswith("http://") or base_url.startswith("https://")):
base_url = f"http://{base_url}"
try:
parsed = urlparse(base_url)
host = parsed.hostname or ""
except Exception:
return False

if not host:
return False

host_lower = host.lower()

# Exact hostname matches for loopback/loopback aliases
if host_lower in {"localhost", "ip6-localhost", "ip6-loopback", "0.0.0.0", "::", "::1"}:
return True

# .local mDNS domains (Bonjour/AVAHI — always local LAN)
if host_lower.endswith(".local") or host_lower.endswith(".localhost"):
return True

# IP address check: loopback or private/LAN range
try:
ip = ipaddress.ip_address(host)
if ip.is_loopback or ip.is_private or ip.is_link_local:
return True
except ValueError:
# Not an IP, hostname checked above
pass

return False


class CustomProfile(ProviderProfile):
"""Custom/Ollama local provider — think=false and num_ctx support."""

Expand All @@ -37,10 +87,25 @@ def build_api_kwargs_extras(
options["num_ctx"] = ollama_num_ctx
extra_body["options"] = options

# Resolve base URL for local detection
_base_url = str(
ctx.get("base_url")
or getattr(self, "base_url", "")
or ""
).strip()
# If no base_url is provided (only happens in legacy tests, never production),
# default to local behavior to preserve backward compatibility
_is_local = True if not _base_url else _is_local_endpoint(_base_url)

# Reasoning / thinking control for custom OpenAI-compatible endpoints
# (GLM-5.2 on Volcengine ARK, vLLM, Ollama, llama.cpp, …).
#
# - disabled → extra_body.think = False (Ollama's thinking-off flag)
# - disabled + LOCAL endpoint → extra_body.think = False + reasoning_effort="none"
# (Ollama's thinking-off flag + top-level field that Ollama /v1/chat/completions
# actually respects; ollama#14820, #25758)
# - disabled + REMOTE endpoint → emit NEITHER. Remote OpenAI-compatible APIs
# (ofox, Volcengine ARK, etc.) reject reasoning_effort="none" as invalid, so we
# omit the field entirely and let the server default apply.
# - enabled + effort set → TOP-LEVEL reasoning_effort string, the
# format GLM-5.2/ARK and other OpenAI-compatible reasoning APIs
# expect (GLM documents "high" and "max"; "max" is its default).
Expand All @@ -55,14 +120,12 @@ def build_api_kwargs_extras(
_effort = (reasoning_config.get("effort") or "").strip().lower()
_enabled = reasoning_config.get("enabled", True)
if _effort == "none" or _enabled is False:
# Ollama's /v1/chat/completions silently ignores
# extra_body.think (only /api/chat honours it — ollama#14820)
# but respects the top-level reasoning_effort field, so both
# are needed to actually stop a thinking-capable model from
# reasoning (#25758). Endpoints that recognize neither simply
# ignore them.
top_level["reasoning_effort"] = "none"
extra_body["think"] = False
# Only send reasoning_effort="none" + think=False to LOCAL
# endpoints (Ollama, local vLLM/llama.cpp). Remote endpoints
# reject "none" as invalid — omit entirely so server default applies.
if _is_local:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch infers API parameter support from network locality, but provider=custom is documented for arbitrary OpenAI-compatible endpoints (website/docs/integrations/providers.md:570). A local proxy can reject these fields and a remote endpoint can support them; please select this behavior through an explicit endpoint/provider capability instead, so enabled: false is not silently converted to an endpoint default based only on its hostname.

top_level["reasoning_effort"] = "none"
extra_body["think"] = False
elif _effort:
top_level["reasoning_effort"] = _effort

Expand Down
46 changes: 36 additions & 10 deletions tests/plugins/model_providers/test_custom_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
was silently dropped for every custom endpoint.

These tests pin the wire-shape contract:
- disabled → extra_body.think = False
- disabled + local endpoint → extra_body.think = False + reasoning_effort="none"
- disabled + remote endpoint → nothing emitted (avoids HTTP 400 from APIs
that reject reasoning_effort="none")
- enabled + effort → top-level reasoning_effort (native OpenAI-compat
format GLM/ARK expect), passed through verbatim
including ``max``/``xhigh``
Expand Down Expand Up @@ -48,28 +50,52 @@ def test_no_reasoning_config_emits_nothing(self, custom_profile):
assert eb == {}
assert tl == {}

def test_disabled_sends_think_false(self, custom_profile):
"""enabled=False → reasoning_effort='none' top-level + think=False.
def test_disabled_sends_think_false_for_local(self, custom_profile):
"""enabled=False + local base_url → reasoning_effort='none' + think=False.

Both fields are required: Ollama's /v1/chat/completions silently
Both fields are required for Ollama: /v1/chat/completions silently
ignores extra_body.think (only /api/chat honours it — ollama#14820)
but respects top-level reasoning_effort (#25758). think=False stays
for proxies and the native /api/chat path.
but respects top-level reasoning_effort (#25758).
"""
eb, tl = custom_profile.build_api_kwargs_extras(
reasoning_config={"enabled": False}, model="glm-5.2"
reasoning_config={"enabled": False}, model="glm-5.2",
base_url="http://localhost:11434/v1",
)
assert eb == {"think": False}
assert tl == {"reasoning_effort": "none"}

def test_effort_none_sends_think_false(self, custom_profile):
"""effort='none' is the disable alias → same dual emission."""
def test_disabled_omits_for_remote(self, custom_profile):
"""enabled=False + remote base_url → nothing emitted.

Remote OpenAI-compatible APIs (ofox, Volcengine ARK, etc.) reject
reasoning_effort="none" as invalid. Omit so the server default applies.
"""
eb, tl = custom_profile.build_api_kwargs_extras(
reasoning_config={"enabled": False}, model="doubao-seed-2.1-pro",
base_url="https://api.ofox.ai/v1",
)
assert eb == {}
assert tl == {}

def test_effort_none_sends_think_false_for_local(self, custom_profile):
"""effort='none' + local → same dual emission as enabled=False."""
eb, tl = custom_profile.build_api_kwargs_extras(
reasoning_config={"enabled": True, "effort": "none"}, model="glm-5.2"
reasoning_config={"enabled": True, "effort": "none"}, model="glm-5.2",
base_url="http://127.0.0.1:11434/v1",
)
assert eb == {"think": False}
assert tl == {"reasoning_effort": "none"}

def test_effort_none_omits_for_remote(self, custom_profile):
"""effort='none' + remote → nothing emitted."""
eb, tl = custom_profile.build_api_kwargs_extras(
reasoning_config={"enabled": True, "effort": "none"},
model="doubao-seed-2.1-pro",
base_url="https://api.ofox.ai/v1",
)
assert eb == {}
assert tl == {}

@pytest.mark.parametrize(
"effort", ["minimal", "low", "medium", "high", "xhigh", "max"]
)
Expand Down