[DSV4] Support max reasoning effort (#40982)
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
This commit is contained in:
@@ -1002,6 +1002,31 @@ def test_chat_completion_request_n_parameter_default():
|
||||
assert sampling_params.n == 1, f"Expected n=1 (default), got n={sampling_params.n}"
|
||||
|
||||
|
||||
def test_chat_completion_request_accepts_model_specific_reasoning_effort():
|
||||
request = ChatCompletionRequest(
|
||||
model="test-model",
|
||||
messages=[{"role": "user", "content": "Hello"}],
|
||||
reasoning_effort="max",
|
||||
)
|
||||
|
||||
chat_params = request.build_chat_params(
|
||||
default_template=None,
|
||||
default_template_content_format="auto",
|
||||
)
|
||||
|
||||
assert request.reasoning_effort == "max"
|
||||
assert chat_params.chat_template_kwargs["reasoning_effort"] == "max"
|
||||
|
||||
|
||||
def test_chat_completion_request_rejects_unknown_reasoning_effort():
|
||||
with pytest.raises(ValueError, match="Input should be"):
|
||||
ChatCompletionRequest(
|
||||
model="test-model",
|
||||
messages=[{"role": "user", "content": "Hello"}],
|
||||
reasoning_effort="extra_high",
|
||||
)
|
||||
|
||||
|
||||
def test_chat_completion_request_n_parameter_various_values():
|
||||
"""Test n parameter with various values."""
|
||||
for n_value in [1, 2, 5, 10]:
|
||||
|
||||
@@ -843,6 +843,13 @@ class TestGetSystemMessage:
|
||||
f"{channel} missing when with_custom_tools={with_tools}"
|
||||
)
|
||||
|
||||
def test_unsupported_reasoning_effort_raises_clear_error(self) -> None:
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="reasoning_effort='max' is not supported by Harmony",
|
||||
):
|
||||
get_system_message(reasoning_effort="max")
|
||||
|
||||
|
||||
class TestResponseInputToHarmonyReasoningItem:
|
||||
"""Tests for response_input_to_harmony handling of reasoning input items.
|
||||
|
||||
@@ -182,7 +182,7 @@ def test_deepseek_v4_renders_parsed_history_tool_arguments():
|
||||
assert 'parameter name="arguments"' not in prompt
|
||||
|
||||
|
||||
@pytest.mark.parametrize("reasoning_effort", ["none", "low", "medium", "high"])
|
||||
@pytest.mark.parametrize("reasoning_effort", ["minimal", "low", "medium", "high"])
|
||||
def test_deepseek_v4_accepts_openai_reasoning_effort_values(reasoning_effort):
|
||||
prompt = _tokenizer().apply_chat_template(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
@@ -195,6 +195,58 @@ def test_deepseek_v4_accepts_openai_reasoning_effort_values(reasoning_effort):
|
||||
assert "Reasoning Effort: Absolute maximum" not in prompt
|
||||
|
||||
|
||||
def test_deepseek_v4_none_reasoning_effort_disables_thinking():
|
||||
prompt = _tokenizer().apply_chat_template(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
tokenize=False,
|
||||
enable_thinking=True,
|
||||
reasoning_effort="none",
|
||||
)
|
||||
|
||||
assert prompt == ("<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("reasoning_effort", "expected_mode", "expected_effort"),
|
||||
[
|
||||
("none", "chat", None),
|
||||
("minimal", "thinking", "high"),
|
||||
("low", "thinking", "high"),
|
||||
("medium", "thinking", "high"),
|
||||
("high", "thinking", "high"),
|
||||
("xhigh", "thinking", "max"),
|
||||
("max", "thinking", "max"),
|
||||
("unexpected", "thinking", "high"),
|
||||
],
|
||||
)
|
||||
def test_deepseek_v4_maps_compatible_thinking_reasoning_effort_values(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
reasoning_effort,
|
||||
expected_mode,
|
||||
expected_effort,
|
||||
):
|
||||
captured_kwargs = []
|
||||
|
||||
def fake_encode_messages(messages, **kwargs):
|
||||
captured_kwargs.append(kwargs)
|
||||
return "prompt"
|
||||
|
||||
monkeypatch.setattr(
|
||||
"vllm.tokenizers.deepseek_v4.encode_messages",
|
||||
fake_encode_messages,
|
||||
)
|
||||
|
||||
_tokenizer().apply_chat_template(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
tokenize=False,
|
||||
enable_thinking=True,
|
||||
reasoning_effort=reasoning_effort,
|
||||
)
|
||||
|
||||
assert captured_kwargs[-1]["thinking_mode"] == expected_mode
|
||||
assert captured_kwargs[-1]["reasoning_effort"] == expected_effort
|
||||
|
||||
|
||||
def test_deepseek_v4_preserves_reference_max_reasoning_effort():
|
||||
prompt = _tokenizer().apply_chat_template(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
@@ -208,6 +260,19 @@ def test_deepseek_v4_preserves_reference_max_reasoning_effort():
|
||||
)
|
||||
|
||||
|
||||
def test_deepseek_v4_maps_xhigh_to_reference_max_reasoning_effort():
|
||||
prompt = _tokenizer().apply_chat_template(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
tokenize=False,
|
||||
enable_thinking=True,
|
||||
reasoning_effort="xhigh",
|
||||
)
|
||||
|
||||
assert prompt.startswith(
|
||||
"<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("case_id", "kwargs"),
|
||||
[
|
||||
|
||||
@@ -182,7 +182,19 @@ class ChatCompletionRequest(OpenAIBaseModel):
|
||||
| ChatCompletionNamedToolChoiceParam
|
||||
| None
|
||||
) = "none"
|
||||
reasoning_effort: Literal["none", "low", "medium", "high"] | None = None
|
||||
reasoning_effort: (
|
||||
Literal["none", "minimal", "low", "medium", "high", "xhigh", "max"] | None
|
||||
) = Field(
|
||||
default=None,
|
||||
description=(
|
||||
"Constrains effort on reasoning for reasoning models. "
|
||||
"Currently supported values are none, minimal, low, medium, "
|
||||
"high, xhigh, and max. Reducing reasoning effort can result in "
|
||||
"faster responses and fewer tokens used on reasoning in a response. "
|
||||
"Note that 'max' is specific to the DeepSeek V4 series and is not "
|
||||
"part of the standard OpenAI API specification."
|
||||
),
|
||||
)
|
||||
thinking_token_budget: int | None = None
|
||||
include_reasoning: bool = True
|
||||
parallel_tool_calls: bool | None = True
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
import datetime
|
||||
from collections.abc import Iterable, Sequence
|
||||
from typing import Literal
|
||||
|
||||
from openai.types.responses.tool import Tool
|
||||
from openai_harmony import (
|
||||
@@ -66,7 +65,7 @@ def get_encoding():
|
||||
|
||||
def get_system_message(
|
||||
model_identity: str | None = None,
|
||||
reasoning_effort: Literal["high", "medium", "low"] | None = None,
|
||||
reasoning_effort: str | None = None,
|
||||
start_date: str | None = None,
|
||||
browser_description: str | None = None,
|
||||
python_description: str | None = None,
|
||||
@@ -84,6 +83,12 @@ def get_system_message(
|
||||
)
|
||||
sys_msg_content = sys_msg_content.with_model_identity(new_identity)
|
||||
if reasoning_effort is not None:
|
||||
if reasoning_effort not in REASONING_EFFORT:
|
||||
supported_values = ", ".join(REASONING_EFFORT)
|
||||
raise ValueError(
|
||||
f"reasoning_effort={reasoning_effort!r} is not supported by "
|
||||
f"Harmony. Supported values are: {supported_values}."
|
||||
)
|
||||
sys_msg_content = sys_msg_content.with_reasoning_effort(
|
||||
REASONING_EFFORT[reasoning_effort]
|
||||
)
|
||||
|
||||
@@ -40,10 +40,16 @@ def get_deepseek_v4_tokenizer(tokenizer: HfTokenizer) -> HfTokenizer:
|
||||
messages.insert(0, {"role": "system"})
|
||||
messages[0]["tools"] = tools # type: ignore[typeddict-unknown-key]
|
||||
|
||||
# The V4 reference currently accepts only "max", "high", or None.
|
||||
reasoning_effort = kwargs.get("reasoning_effort")
|
||||
if reasoning_effort not in ("max", "high"):
|
||||
if not isinstance(reasoning_effort, str):
|
||||
reasoning_effort = None
|
||||
elif reasoning_effort == "none":
|
||||
thinking_mode = "chat"
|
||||
reasoning_effort = None
|
||||
elif reasoning_effort in ("max", "xhigh"):
|
||||
reasoning_effort = "max"
|
||||
else:
|
||||
reasoning_effort = "high"
|
||||
|
||||
encode_config = dict(
|
||||
thinking_mode=thinking_mode,
|
||||
|
||||
Reference in New Issue
Block a user