mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 09:09:40 +08:00
fix(llm): resolve model-intrinsic overrides from the model, not the route
Kimi models lock temperature server-side and reject any explicit value with HTTP 400 "invalid temperature: only 1 is allowed for this model". The moonshot spec has dropped the parameter for them since before v1.5.5, but the lookup ran over the *configured binding's* spec, so it only fired for someone who had picked binding="moonshot". #938 picked binding="openai" and pointed it at Moonshot — what most people do with an OpenAI-compatible endpoint — and kept getting the 400 two months after the fix landed. The route is not what enforces the limit, so the route should not be what decides. `model_overrides_for` states that once: the configured spec wins when it says something about this model, otherwise the model's own vendor spec answers. `find_by_model` already skipped gateway and local entries, so it lands on the vendor whose API enforces the limit; it was simply never consulted on this path. This generalises past kimi — every model-intrinsic override was being lost behind a generic gateway binding, and a spec-less config got none at all. The override had no test, which is how it stayed half-wired. The added ones are written as the reporter's scenario, and pin the two things that must not break: an explicit binding still wins, and the tunable moonshot-v1-* series (no "kimi" in the name) keeps the caller's temperature.
This commit is contained in:
@@ -33,6 +33,7 @@ from deeptutor.services.llm.reasoning_params import (
|
||||
build_openai_compatible_reasoning_kwargs,
|
||||
)
|
||||
from deeptutor.services.llm.usage_frame import token_counts
|
||||
from deeptutor.services.provider_registry import model_overrides_for
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from deeptutor.services.provider_registry import ProviderSpec
|
||||
@@ -299,18 +300,13 @@ class OpenAICompatProvider(LLMProvider):
|
||||
else:
|
||||
kwargs["max_tokens"] = max(1, max_tokens)
|
||||
|
||||
if spec:
|
||||
model_lower = model_name.lower()
|
||||
for pattern, overrides in spec.model_overrides:
|
||||
if pattern in model_lower:
|
||||
for key, value in overrides.items():
|
||||
# None means "drop this parameter" — e.g. Kimi models
|
||||
# reject any explicit temperature and must be sent none.
|
||||
if value is None:
|
||||
kwargs.pop(key, None)
|
||||
else:
|
||||
kwargs[key] = value
|
||||
break
|
||||
for key, value in model_overrides_for(model_name, spec).items():
|
||||
# None means "drop this parameter" — e.g. Kimi models reject any
|
||||
# explicit temperature and must be sent none.
|
||||
if value is None:
|
||||
kwargs.pop(key, None)
|
||||
else:
|
||||
kwargs[key] = value
|
||||
|
||||
kwargs.update(
|
||||
build_openai_compatible_reasoning_kwargs(
|
||||
|
||||
@@ -538,6 +538,48 @@ def find_by_model(model: str | None) -> ProviderSpec | None:
|
||||
return None
|
||||
|
||||
|
||||
def _matching_overrides(spec: ProviderSpec, model_lower: str) -> dict[str, Any]:
|
||||
for pattern, overrides in spec.model_overrides:
|
||||
if pattern in model_lower:
|
||||
return dict(overrides)
|
||||
return {}
|
||||
|
||||
|
||||
def model_overrides_for(model: str | None, spec: ProviderSpec | None) -> dict[str, Any]:
|
||||
"""Request-parameter overrides *model* needs, whatever route serves it.
|
||||
|
||||
``model_overrides`` are intrinsic to the model rather than to the route: a
|
||||
Kimi model rejects an explicit ``temperature`` whether the caller reached
|
||||
it through the ``moonshot`` binding, through an OpenAI-compatible router,
|
||||
or through a gateway. Resolving them from the configured binding alone
|
||||
loses them for everyone who picked a generic binding — which is the common
|
||||
case — so #938 still got HTTP 400 ``invalid temperature: only 1 is allowed
|
||||
for this model`` from ``kimi-k3`` under ``binding="openai"``, two months
|
||||
after the override itself landed.
|
||||
|
||||
The configured spec is consulted first so an explicit binding always wins.
|
||||
When it says nothing about this model, the model's own vendor spec does;
|
||||
``find_by_model`` skips gateway and local entries, so that lookup lands on
|
||||
the vendor whose API is the thing actually enforcing the limit.
|
||||
|
||||
A value of ``None`` means "send this parameter as absent" — see the
|
||||
``moonshot`` spec for why that differs from sending a fixed value.
|
||||
"""
|
||||
model_lower = (model or "").strip().lower()
|
||||
if not model_lower:
|
||||
return {}
|
||||
|
||||
if spec is not None:
|
||||
configured = _matching_overrides(spec, model_lower)
|
||||
if configured:
|
||||
return configured
|
||||
|
||||
vendor = find_by_model(model_lower)
|
||||
if vendor is None or vendor is spec:
|
||||
return {}
|
||||
return _matching_overrides(vendor, model_lower)
|
||||
|
||||
|
||||
def find_gateway(
|
||||
provider_name: str | None = None,
|
||||
api_key: str | None = None,
|
||||
|
||||
85
tests/services/llm/test_model_overrides.py
Normal file
85
tests/services/llm/test_model_overrides.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""Model-intrinsic request overrides survive a generic binding (issue #938).
|
||||
|
||||
Kimi-branded models lock ``temperature`` server-side and answer HTTP 400
|
||||
``invalid temperature: only 1 is allowed for this model`` to any explicit
|
||||
value. The ``moonshot`` spec has dropped the parameter for them since before
|
||||
v1.5.5 — but the override was resolved from the *configured binding*, so it
|
||||
only fired for someone who had picked ``binding="moonshot"``. #938 picked
|
||||
``binding="openai"`` and pointed it at Moonshot, which is what most users do
|
||||
with an OpenAI-compatible endpoint, and kept getting the 400.
|
||||
|
||||
The route is not what enforces the limit, so the route is not what should
|
||||
decide. These tests pin that, and pin the two things that must keep working:
|
||||
an explicit binding still wins, and the tunable ``moonshot-v1-*`` series
|
||||
(which carries no "kimi" in its name) keeps the caller's temperature.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from deeptutor.services.llm.provider_core.openai_compat_provider import OpenAICompatProvider
|
||||
from deeptutor.services.provider_registry import find_by_name, model_overrides_for
|
||||
|
||||
_MOONSHOT_BASE = "https://api.moonshot.cn/v1"
|
||||
|
||||
|
||||
def _payload(binding: str | None, model: str) -> dict:
|
||||
"""Build the request exactly as a chat round would."""
|
||||
spec = find_by_name(binding)
|
||||
provider = OpenAICompatProvider(
|
||||
api_key="test-key",
|
||||
api_base=_MOONSHOT_BASE,
|
||||
default_model=model,
|
||||
spec=spec,
|
||||
provider_name=binding,
|
||||
)
|
||||
return provider._build_kwargs(
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
tools=None,
|
||||
model=model,
|
||||
max_tokens=256,
|
||||
temperature=0.7,
|
||||
reasoning_effort=None,
|
||||
tool_choice=None,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("binding", ["openai", "moonshot", "azure", "no-such-provider"])
|
||||
def test_kimi_never_carries_an_explicit_temperature(binding: str) -> None:
|
||||
"""The reporter's scenario, plus every other route to the same model."""
|
||||
assert "temperature" not in _payload(binding, "kimi-k3")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("model", ["moonshot-v1-32k", "moonshot-v1-8k-vision"])
|
||||
def test_tunable_moonshot_series_keeps_the_callers_temperature(model: str) -> None:
|
||||
"""These accept temperature; dropping it would silently change sampling."""
|
||||
assert _payload("moonshot", model)["temperature"] == pytest.approx(0.7)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("model", ["gpt-4o", "claude-sonnet-5", "deepseek-chat"])
|
||||
def test_unrelated_models_are_untouched(model: str) -> None:
|
||||
assert _payload("openai", model)["temperature"] == pytest.approx(0.7)
|
||||
|
||||
|
||||
def test_configured_binding_wins_over_the_vendor_fallback() -> None:
|
||||
"""An explicit binding is the user's statement about the route; the
|
||||
vendor lookup is only consulted when it says nothing about this model."""
|
||||
moonshot = find_by_name("moonshot")
|
||||
assert model_overrides_for("kimi-k3", moonshot) == {"temperature": None}
|
||||
# Same answer with no binding at all — resolved from the model itself.
|
||||
assert model_overrides_for("kimi-k3", None) == {"temperature": None}
|
||||
|
||||
|
||||
def test_a_model_with_no_intrinsic_override_resolves_to_nothing() -> None:
|
||||
assert model_overrides_for("gpt-4o", find_by_name("openai")) == {}
|
||||
assert model_overrides_for("", find_by_name("moonshot")) == {}
|
||||
assert model_overrides_for(None, None) == {}
|
||||
|
||||
|
||||
def test_vendor_prefixed_routing_still_finds_the_model() -> None:
|
||||
"""Routers advertise Moonshot models as ``moonshotai/kimi-...``; the
|
||||
upstream API enforces the same limit whichever name reaches it."""
|
||||
assert model_overrides_for("moonshotai/kimi-k2", find_by_name("openai")) == {
|
||||
"temperature": None
|
||||
}
|
||||
Reference in New Issue
Block a user