Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

18.4 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Adaptive effort nests under output_config, since a top-level effort field 400s upstream."""

import asyncio
import itertools
import json
import re
from pathlib import Path

import httpx
import pytest

from core.inference import external_provider as ep_mod
from core.inference.external_provider import ExternalProviderClient


def _drive(coro):
    return asyncio.new_event_loop().run_until_complete(coro)


async def _collect(agen):
    out = []
    async for line in agen:
        out.append(line)
    return out


def _mock_http_client(monkeypatch, handler):
    transport = httpx.MockTransport(handler)
    monkeypatch.setattr(ep_mod, "_http_client", httpx.AsyncClient(transport = transport))


def _make_client() -> ExternalProviderClient:
    return ExternalProviderClient(
        provider_type = "anthropic",
        base_url = "https://api.anthropic.com/v1",
        api_key = "sk-ant-test",
    )


def _anthropic_sse(events: list[dict]) -> bytes:
    """Serialize a list of Messages-API event dicts as an SSE byte stream."""
    chunks: list[str] = []
    for event in events:
        chunks.append(f"event: {event['type']}")
        chunks.append(f"data: {json.dumps(event)}")
        chunks.append("")
    return ("\n".join(chunks) + "\n").encode("utf-8")


def _payloads_from_lines(lines: list[str]) -> list:
    out = []
    for line in lines:
        if not line.startswith("data:"):
            continue
        raw = line[len("data:") :].strip()
        if not raw:
            continue
        if raw == "[DONE]":
            out.append("[DONE]")
        else:
            out.append(json.loads(raw))
    return out


def test_adaptive_thinking_body_uses_output_config_effort_shape(monkeypatch):
    captured: dict = {}

    def handler(request: httpx.Request) -> httpx.Response:
        captured["body"] = json.loads(request.content.decode("utf-8"))
        return httpx.Response(
            200,
            content = _anthropic_sse([{"type": "message_stop"}]),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        async for _ in client._stream_anthropic(
            messages = [{"role": "user", "content": "hi"}],
            model = "claude-opus-4-6",
            temperature = 0.7,
            top_p = 0.95,
            max_tokens = 4096,
            top_k = None,
            enable_thinking = None,
            reasoning_effort = "medium",
        ):
            pass
        await client.close()

    _drive(run())

    body = captured["body"]
    # Opus 4.7 defaults display to omitted; summarized keeps thinking_delta.
    assert body["thinking"] == {"type": "adaptive", "display": "summarized"}
    # Effort must nest under output_config; top-level 400s.
    assert body["output_config"] == {"effort": "medium"}
    assert "effort" not in body
    assert body["temperature"] == 1
    assert "top_p" not in body
    assert "top_k" not in body


@pytest.mark.parametrize(
    ("model", "sampling_removed"),
    (
        ("claude-opus-4-7", True),
        ("claude-opus-4-8", True),
        ("claude-opus-5", True),
        ("claude-sonnet-5", True),
        ("claude-fable-5", True),
        ("claude-mythos-5", True),
        ("claude-mythos-preview", True),
        ("claude-opus-4-6", False),
        ("claude-sonnet-4-6", False),
        ("claude-haiku-4-5-20251001", False),
    ),
)
def test_sampling_parameter_capability_detection(model, sampling_removed):
    assert ep_mod._anthropic_sampling_params_removed(model) is sampling_removed


def test_claude_opus_5_request_omits_removed_sampling_parameters(monkeypatch):
    captured: dict = {}

    def handler(request: httpx.Request) -> httpx.Response:
        captured["body"] = json.loads(request.content.decode("utf-8"))
        return httpx.Response(
            200,
            content = _anthropic_sse([{"type": "message_stop"}]),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        async for _ in client._stream_anthropic(
            messages = [{"role": "user", "content": "hi"}],
            model = "claude-opus-5",
            temperature = 0.7,
            top_p = 0.95,
            max_tokens = 1024,
            top_k = 40,
            enable_thinking = False,
        ):
            pass
        await client.close()

    _drive(run())

    body = captured["body"]
    assert "temperature" not in body
    assert "top_k" not in body


def test_adaptive_thinking_maps_xhigh_to_max_on_claude_4_6(monkeypatch):
    captured: dict = {}

    def handler(request: httpx.Request) -> httpx.Response:
        captured["body"] = json.loads(request.content.decode("utf-8"))
        return httpx.Response(
            200,
            content = _anthropic_sse([{"type": "message_stop"}]),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        async for _ in client._stream_anthropic(
            messages = [{"role": "user", "content": "hi"}],
            model = "claude-sonnet-4-6",
            temperature = 0.7,
            top_p = 0.95,
            max_tokens = 4096,
            top_k = None,
            enable_thinking = None,
            reasoning_effort = "xhigh",
        ):
            pass
        await client.close()

    _drive(run())

    assert captured["body"]["output_config"] == {"effort": "max"}


def test_adaptive_thinking_keeps_max_on_claude_4_6(monkeypatch):
    captured: dict = {}

    def handler(request: httpx.Request) -> httpx.Response:
        captured["body"] = json.loads(request.content.decode("utf-8"))
        return httpx.Response(
            200,
            content = _anthropic_sse([{"type": "message_stop"}]),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        async for _ in client._stream_anthropic(
            messages = [{"role": "user", "content": "hi"}],
            model = "claude-opus-4-6",
            temperature = 0.7,
            top_p = 0.95,
            max_tokens = 4096,
            top_k = None,
            enable_thinking = None,
            reasoning_effort = "max",
        ):
            pass
        await client.close()

    _drive(run())

    assert captured["body"]["output_config"] == {"effort": "max"}


def test_adaptive_thinking_keeps_xhigh_on_claude_4_7(monkeypatch):
    captured: dict = {}

    def handler(request: httpx.Request) -> httpx.Response:
        captured["body"] = json.loads(request.content.decode("utf-8"))
        return httpx.Response(
            200,
            content = _anthropic_sse([{"type": "message_stop"}]),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        async for _ in client._stream_anthropic(
            messages = [{"role": "user", "content": "hi"}],
            model = "claude-opus-4-7",
            temperature = 0.7,
            top_p = 0.95,
            max_tokens = 4096,
            top_k = None,
            enable_thinking = None,
            reasoning_effort = "xhigh",
        ):
            pass
        await client.close()

    _drive(run())

    body = captured["body"]
    assert body["output_config"] == {"effort": "xhigh"}
    assert "effort" not in body


def test_manual_thinking_body_uses_budget_tokens_on_4_5(monkeypatch):
    captured: dict = {}

    def handler(request: httpx.Request) -> httpx.Response:
        captured["body"] = json.loads(request.content.decode("utf-8"))
        return httpx.Response(
            200,
            content = _anthropic_sse([{"type": "message_stop"}]),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        async for _ in client._stream_anthropic(
            messages = [{"role": "user", "content": "hi"}],
            model = "claude-opus-4-5",
            temperature = 0.7,
            top_p = 0.95,
            max_tokens = 1024,
            top_k = None,
            enable_thinking = None,
            reasoning_effort = "high",
        ):
            pass
        await client.close()

    _drive(run())

    body = captured["body"]
    assert body["thinking"] == {"type": "enabled", "budget_tokens": 4096}
    assert body["max_tokens"] > body["thinking"]["budget_tokens"]
    assert "effort" not in body
    assert "output_config" not in body


def test_thinking_delta_wrapped_in_think_tags(monkeypatch):
    def handler(request: httpx.Request) -> httpx.Response:
        events = [
            {
                "type": "content_block_start",
                "index": 0,
                "content_block": {"type": "thinking", "thinking": "", "signature": ""},
            },
            {
                "type": "content_block_delta",
                "index": 0,
                "delta": {"type": "thinking_delta", "thinking": "First "},
            },
            {
                "type": "content_block_delta",
                "index": 0,
                "delta": {"type": "thinking_delta", "thinking": "I plan."},
            },
            {
                "type": "content_block_delta",
                "index": 0,
                "delta": {"type": "signature_delta", "signature": "abc123"},
            },
            {"type": "content_block_stop", "index": 0},
            {
                "type": "content_block_start",
                "index": 1,
                "content_block": {"type": "text", "text": ""},
            },
            {
                "type": "content_block_delta",
                "index": 1,
                "delta": {"type": "text_delta", "text": "Answer."},
            },
            {"type": "content_block_stop", "index": 1},
            {"type": "message_delta", "delta": {"stop_reason": "end_turn"}},
            {"type": "message_stop"},
        ]
        return httpx.Response(
            200,
            content = _anthropic_sse(events),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        lines = await _collect(
            client._stream_anthropic(
                messages = [{"role": "user", "content": "hi"}],
                model = "claude-opus-4-6",
                temperature = 0.7,
                top_p = 0.95,
                max_tokens = 4096,
                top_k = None,
                enable_thinking = True,
                reasoning_effort = None,
            )
        )
        await client.close()
        return lines

    lines = _drive(run())
    payloads = _payloads_from_lines(lines)

    combined = "".join(
        p["choices"][0]["delta"].get("content", "")
        for p in payloads
        if isinstance(p, dict) and p["choices"][0]["delta"]
    )

    assert "<think>First I plan.</think>" in combined
    assert combined.endswith("Answer.")
    assert "abc123" not in combined
    assert "[DONE]" in payloads


def test_thinking_only_turn_closes_tag_without_text_delta(monkeypatch):
    """A thinking-only turn with no text_delta must still close the think tag at content_block_stop."""

    def handler(request: httpx.Request) -> httpx.Response:
        events = [
            {
                "type": "content_block_start",
                "index": 0,
                "content_block": {"type": "thinking", "thinking": "", "signature": ""},
            },
            {
                "type": "content_block_delta",
                "index": 0,
                "delta": {"type": "thinking_delta", "thinking": "internal"},
            },
            {"type": "content_block_stop", "index": 0},
            {"type": "message_delta", "delta": {"stop_reason": "end_turn"}},
            {"type": "message_stop"},
        ]
        return httpx.Response(
            200,
            content = _anthropic_sse(events),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        lines = await _collect(
            client._stream_anthropic(
                messages = [{"role": "user", "content": "hi"}],
                model = "claude-opus-4-7",
                temperature = 0.7,
                top_p = 0.95,
                max_tokens = 4096,
                top_k = None,
                enable_thinking = True,
                reasoning_effort = None,
            )
        )
        await client.close()
        return lines

    payloads = _payloads_from_lines(_drive(run()))
    combined = "".join(
        p["choices"][0]["delta"].get("content", "")
        for p in payloads
        if isinstance(p, dict) and p["choices"][0]["delta"]
    )
    assert combined == "<think>internal</think>"


def _capture_body(monkeypatch, **kwargs) -> dict:
    """Drive one streamed call and return the outbound Anthropic body."""
    captured: dict = {}

    def handler(request: httpx.Request) -> httpx.Response:
        captured["body"] = json.loads(request.content.decode("utf-8"))
        return httpx.Response(
            200,
            content = _anthropic_sse([{"type": "message_stop"}]),
            headers = {"content-type": "text/event-stream"},
        )

    _mock_http_client(monkeypatch, handler)

    async def run():
        client = _make_client()
        async for _ in client.stream_chat_completion(
            messages = [{"role": "user", "content": "hi"}],
            temperature = 0.7,
            top_p = 0.95,
            max_tokens = 64,
            **kwargs,
        ):
            pass
        await client.close()

    _drive(run())
    return captured["body"]


@pytest.mark.parametrize(
    "model",
    ("claude-opus-5", "claude-sonnet-5", "claude-fable-5", "claude-opus-4-8"),
)
def test_claude_5_uses_adaptive_thinking_and_effort(monkeypatch, model):
    """Claude 5 / Opus 4.8 must get adaptive thinking, not a bare request."""
    body = _capture_body(
        monkeypatch,
        model = model,
        enable_thinking = True,
        reasoning_effort = "xhigh",
    )
    assert body["thinking"] == {"type": "adaptive", "display": "summarized"}
    assert body["output_config"] == {"effort": "xhigh"}


def test_thinking_off_disables_explicitly_on_opus_5(monkeypatch):
    """Opus 5 defaults to thinking, so off requires an explicit disable."""
    body = _capture_body(monkeypatch, model = "claude-opus-5", enable_thinking = False)
    assert body["thinking"] == {"type": "disabled"}
    assert "output_config" not in body


def test_thinking_off_omits_disable_on_fable_5(monkeypatch):
    """Fable 5 always thinks and rejects explicit disable requests with HTTP 400."""
    body = _capture_body(monkeypatch, model = "claude-fable-5", enable_thinking = False)
    assert "thinking" not in body


@pytest.mark.parametrize("off", ({"enable_thinking": False}, {"reasoning_effort": "none"}))
def test_thinking_off_on_sonnet_5_5_sends_between_tools(monkeypatch, off):
    body = _capture_body(monkeypatch, model = "claude-sonnet-5-5", **off)
    assert body["thinking"] == {"type": "between_tools"}
    assert "output_config" not in body


@pytest.mark.parametrize("off", ({"enable_thinking": False}, {"reasoning_effort": "none"}))
def test_thinking_off_omits_disable_on_opus_5_5(monkeypatch, off):
    body = _capture_body(monkeypatch, model = "claude-opus-5-5", **off)
    assert "thinking" not in body


@pytest.mark.parametrize(
    ("model", "web", "code", "compaction", "fast"),
    (
        ("claude-opus-5", "web_search_20260209", "code_execution_20260120", True, True),
        ("claude-sonnet-5", "web_search_20260209", "code_execution_20260120", True, False),
        ("claude-opus-4-8", "web_search_20260209", "code_execution_20260120", True, True),
        ("claude-opus-4-7", "web_search_20260209", "code_execution_20260120", True, False),
        ("claude-opus-4-5", "web_search_20250305", "code_execution_20260120", False, False),
    ),
)
def test_model_capability_tables_cover_claude_5(model, web, code, compaction, fast):
    assert ep_mod._anthropic_web_search_version(model) == web
    assert ep_mod._anthropic_code_execution_version(model) == code
    assert ep_mod._anthropic_supports_compaction(model) is compaction
    assert ep_mod._anthropic_supports_fast_mode(model) is fast


@pytest.mark.parametrize(
    ("model", "sampling_removed"),
    (
        ("claude-opus-4.8", True),
        ("claude-opus-4.7", True),
        ("claude-sonnet-4.6", False),
        ("claude-3-5-sonnet-20241022", False),
        ("claude-3-7-sonnet-20250219", False),
        # A snapshot date is not a minor version (Opus 4.0).
        ("claude-opus-4-20250514", False),
        ("claude-opus-4", False),
        ("claude-opus-4-1-20250805", False),
        ("claude-opus-5-20260724", True),
        ("claude-opus-4-7-20260414", True),
        ("claude-opus-4-10", True),
    ),
)
def test_sampling_capability_handles_alternate_id_spellings(model, sampling_removed):
    assert ep_mod._anthropic_sampling_params_removed(model) is sampling_removed


def test_frontend_hides_claude_sampling_exactly_where_the_request_strips_it():
    """A shown slider the request strips does nothing; a hidden one drops a value it would send."""
    source = (
        Path(__file__).resolve().parents[2] / "frontend/src/features/chat/provider-capabilities.ts"
    ).read_text(encoding = "utf-8")
    literal = re.search(r"const ANTHROPIC_SAMPLING_REMOVED_MODEL =\s*/(.+)/;", source)
    assert literal, "ANTHROPIC_SAMPLING_REMOVED_MODEL moved"
    frontend = re.compile(literal.group(1))
    models = [
        f"claude-{family}-{major}{minor}{suffix}"
        for family, major, minor, suffix in itertools.product(
            ("opus", "sonnet", "haiku", "fable", "mythos", "nova"),
            ("1", "3", "4", "5", "6", "7", "9", "10"),
            ("", "-0", "-00", "-1", "-6", "-06", "-7", "-07", "-8", "-10", ".6", ".7"),
            ("", "-20250514", "-latest"),
        )
    ] + [
        "claude-mythos-preview",
        "claude-mythos-preview-1",
        "claude-mythos-previewer",
        "claude-3-5-sonnet-20241022",
        "claude-3-7-sonnet-20250219",
        "claude-opus-latest",
    ]
    for model in models:
        assert bool(frontend.match(model)) is ep_mod._anthropic_sampling_params_removed(
            model
        ), model