[Frontend] DeepSeek V4 0731 reasoning effort prompts & mappings (#50580)
This commit is contained in:
@@ -24,11 +24,16 @@ const THINKING_END_TOKEN: &str = "</think>";
|
|||||||
const DSML_TOKEN: &str = "|DSML|";
|
const DSML_TOKEN: &str = "|DSML|";
|
||||||
const USER_SP_TOKEN: &str = "<|User|>";
|
const USER_SP_TOKEN: &str = "<|User|>";
|
||||||
const ASSISTANT_SP_TOKEN: &str = "<|Assistant|>";
|
const ASSISTANT_SP_TOKEN: &str = "<|Assistant|>";
|
||||||
const REASONING_EFFORT_MAX: &str = concat!(
|
const REASONING_EFFORT_HIGH: &str = concat!(
|
||||||
"Reasoning Effort: Absolute maximum with no shortcuts permitted.\n",
|
"Reasoning Effort: Absolute maximum with no shortcuts permitted.\n",
|
||||||
"You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\n",
|
"You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\n",
|
||||||
"Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n",
|
"Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n",
|
||||||
);
|
);
|
||||||
|
const REASONING_EFFORT_MAX: &str = concat!(
|
||||||
|
"Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.\n",
|
||||||
|
"You MUST reason with the utmost depth and rigor, leaving absolutely nothing to chance: exhaustively decompose the problem into its most fundamental components, trace every causal chain to its root, and resolve the underlying cause rather than any surface symptom.\n",
|
||||||
|
"Do not stop reasoning until you have independently verified the solution from multiple angles and are certain that no assumption remains unchecked and no error remains undiscovered.\n\n",
|
||||||
|
);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
enum ThinkingMode {
|
enum ThinkingMode {
|
||||||
@@ -47,7 +52,7 @@ struct RenderedToolSchema<'a> {
|
|||||||
|
|
||||||
/// Render one chat request into the final prompt string.
|
/// Render one chat request into the final prompt string.
|
||||||
pub(super) fn render_request(request: &ChatRequest) -> Result<String> {
|
pub(super) fn render_request(request: &ChatRequest) -> Result<String> {
|
||||||
let (thinking_mode, max_reasoning_effort) = resolve_thinking_options(request)?;
|
let (thinking_mode, reasoning_effort_prompt) = resolve_thinking_options(request)?;
|
||||||
let request_tools = request_tools(request);
|
let request_tools = request_tools(request);
|
||||||
let synthetic_tool_system = needs_synthetic_tool_system(request, request_tools);
|
let synthetic_tool_system = needs_synthetic_tool_system(request, request_tools);
|
||||||
let drop_thinking = request.parse_template_bool("drop_thinking")?.unwrap_or(true)
|
let drop_thinking = request.parse_template_bool("drop_thinking")?.unwrap_or(true)
|
||||||
@@ -55,8 +60,8 @@ pub(super) fn render_request(request: &ChatRequest) -> Result<String> {
|
|||||||
let last_user_render_index =
|
let last_user_render_index =
|
||||||
find_last_user_render_index(request.messages.as_slice(), synthetic_tool_system);
|
find_last_user_render_index(request.messages.as_slice(), synthetic_tool_system);
|
||||||
let mut out = String::from(BOS_TOKEN);
|
let mut out = String::from(BOS_TOKEN);
|
||||||
if thinking_mode == ThinkingMode::Thinking && max_reasoning_effort {
|
if thinking_mode == ThinkingMode::Thinking {
|
||||||
out.push_str(REASONING_EFFORT_MAX);
|
out.push_str(reasoning_effort_prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut request_tools_attached = false;
|
let mut request_tools_attached = false;
|
||||||
@@ -124,20 +129,28 @@ pub(super) fn render_request(request: &ChatRequest) -> Result<String> {
|
|||||||
/// wrapper, the Rust renderer only consumes the typed top-level
|
/// wrapper, the Rust renderer only consumes the typed top-level
|
||||||
/// `reasoning_effort`; the generic template-kwargs map is left for HF
|
/// `reasoning_effort`; the generic template-kwargs map is left for HF
|
||||||
/// templates.
|
/// templates.
|
||||||
fn resolve_thinking_options(request: &ChatRequest) -> Result<(ThinkingMode, bool)> {
|
fn resolve_thinking_options(request: &ChatRequest) -> Result<(ThinkingMode, &'static str)> {
|
||||||
let mut thinking_mode = match request.enable_thinking()?.unwrap_or(false) {
|
let mut thinking_mode = match request.enable_thinking()?.unwrap_or(true) {
|
||||||
true => ThinkingMode::Thinking,
|
true => ThinkingMode::Thinking,
|
||||||
false => ThinkingMode::Chat,
|
false => ThinkingMode::Chat,
|
||||||
};
|
};
|
||||||
let mut max_reasoning_effort = false;
|
let mut reasoning_effort_prompt = REASONING_EFFORT_HIGH;
|
||||||
|
|
||||||
match request.chat_options.reasoning_effort {
|
match request.chat_options.reasoning_effort {
|
||||||
Some(ReasoningEffort::None) => thinking_mode = ThinkingMode::Chat,
|
Some(ReasoningEffort::None) => thinking_mode = ThinkingMode::Chat,
|
||||||
Some(ReasoningEffort::Max | ReasoningEffort::XHigh) => max_reasoning_effort = true,
|
Some(ReasoningEffort::Max) => {
|
||||||
Some(_) | None => {}
|
reasoning_effort_prompt = REASONING_EFFORT_MAX;
|
||||||
|
}
|
||||||
|
Some(ReasoningEffort::XHigh | ReasoningEffort::High) => {
|
||||||
|
reasoning_effort_prompt = REASONING_EFFORT_HIGH;
|
||||||
|
}
|
||||||
|
Some(ReasoningEffort::Minimal | ReasoningEffort::Medium | ReasoningEffort::Low) => {
|
||||||
|
reasoning_effort_prompt = "";
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok((thinking_mode, max_reasoning_effort))
|
Ok((thinking_mode, reasoning_effort_prompt))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return request-level tools only when native tool parsing is enabled.
|
/// Return request-level tools only when native tool parsing is enabled.
|
||||||
|
|||||||
@@ -22,7 +22,9 @@ fn render_request(request: &ChatRequest) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn fixture_request(input_name: &str) -> ChatRequest {
|
fn fixture_request(input_name: &str) -> ChatRequest {
|
||||||
fixture_chat_request(&fixture_path(input_name), deepseek_fixture_options())
|
let mut request = fixture_chat_request(&fixture_path(input_name), deepseek_fixture_options());
|
||||||
|
request.chat_options.reasoning_effort = Some(ReasoningEffort::Low);
|
||||||
|
request
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deepseek_fixture_options() -> FixtureRequestOptions {
|
fn deepseek_fixture_options() -> FixtureRequestOptions {
|
||||||
@@ -75,6 +77,29 @@ fn reasoning_effort_max_adds_prefix_when_thinking_is_enabled() {
|
|||||||
|
|
||||||
let rendered = render_request(&request);
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
expect![[r#"
|
||||||
|
<|begin▁of▁sentence|>Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.
|
||||||
|
You MUST reason with the utmost depth and rigor, leaving absolutely nothing to chance: exhaustively decompose the problem into its most fundamental components, trace every causal chain to its root, and resolve the underlying cause rather than any surface symptom.
|
||||||
|
Do not stop reasoning until you have independently verified the solution from multiple angles and are certain that no assumption remains unchecked and no error remains undiscovered.
|
||||||
|
|
||||||
|
<|User|>solve it<|Assistant|><think>"#]]
|
||||||
|
.assert_eq(&rendered);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reasoning_effort_high_adds_0731_high_prefix() {
|
||||||
|
let mut request = ChatRequest {
|
||||||
|
messages: vec![ChatMessage::user("solve it")],
|
||||||
|
..ChatRequest::for_test()
|
||||||
|
};
|
||||||
|
request
|
||||||
|
.chat_options
|
||||||
|
.template_kwargs
|
||||||
|
.insert("thinking".to_string(), Value::Bool(true));
|
||||||
|
request.chat_options.reasoning_effort = Some(ReasoningEffort::High);
|
||||||
|
|
||||||
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum with no shortcuts permitted.
|
<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum with no shortcuts permitted.
|
||||||
You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.
|
You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.
|
||||||
@@ -84,6 +109,66 @@ fn reasoning_effort_max_adds_prefix_when_thinking_is_enabled() {
|
|||||||
.assert_eq(&rendered);
|
.assert_eq(&rendered);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn omitted_thinking_and_effort_default_to_high() {
|
||||||
|
let request = ChatRequest {
|
||||||
|
messages: vec![ChatMessage::user("solve it")],
|
||||||
|
..ChatRequest::for_test()
|
||||||
|
};
|
||||||
|
|
||||||
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
assert!(rendered.starts_with("<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"));
|
||||||
|
assert!(rendered.ends_with("<|Assistant|><think>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reasoning_effort_xhigh_maps_to_high() {
|
||||||
|
let mut request = ChatRequest {
|
||||||
|
messages: vec![ChatMessage::user("solve it")],
|
||||||
|
..ChatRequest::for_test()
|
||||||
|
};
|
||||||
|
request.chat_options.reasoning_effort = Some(ReasoningEffort::XHigh);
|
||||||
|
|
||||||
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
assert!(rendered.starts_with("<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"));
|
||||||
|
assert!(rendered.ends_with("<|Assistant|><think>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn minimal_and_medium_reasoning_effort_map_to_low() {
|
||||||
|
for effort in [ReasoningEffort::Minimal, ReasoningEffort::Medium] {
|
||||||
|
let mut request = ChatRequest {
|
||||||
|
messages: vec![ChatMessage::user("solve it")],
|
||||||
|
..ChatRequest::for_test()
|
||||||
|
};
|
||||||
|
request.chat_options.reasoning_effort = Some(effort);
|
||||||
|
|
||||||
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
expect!["<|begin▁of▁sentence|><|User|>solve it<|Assistant|><think>"]
|
||||||
|
.assert_eq(&rendered);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn reasoning_effort_low_keeps_the_default_prompt() {
|
||||||
|
let mut request = ChatRequest {
|
||||||
|
messages: vec![ChatMessage::user("solve it")],
|
||||||
|
..ChatRequest::for_test()
|
||||||
|
};
|
||||||
|
request
|
||||||
|
.chat_options
|
||||||
|
.template_kwargs
|
||||||
|
.insert("thinking".to_string(), Value::Bool(true));
|
||||||
|
request.chat_options.reasoning_effort = Some(ReasoningEffort::Low);
|
||||||
|
|
||||||
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
expect!["<|begin▁of▁sentence|><|User|>solve it<|Assistant|><think>"].assert_eq(&rendered);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn reasoning_effort_none_disables_thinking() {
|
fn reasoning_effort_none_disables_thinking() {
|
||||||
let mut request = ChatRequest {
|
let mut request = ChatRequest {
|
||||||
@@ -119,12 +204,13 @@ fn reasoning_effort_template_kwarg_is_ignored() {
|
|||||||
|
|
||||||
let rendered = render_request(&request);
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
expect!["<|begin▁of▁sentence|><|User|>solve it<|Assistant|><think>"].assert_eq(&rendered);
|
assert!(rendered.starts_with("<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"));
|
||||||
|
assert!(rendered.ends_with("<|Assistant|><think>"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tool_results_are_sorted_by_previous_assistant_tool_call_order() {
|
fn tool_results_are_sorted_by_previous_assistant_tool_call_order() {
|
||||||
let request = ChatRequest {
|
let mut request = ChatRequest {
|
||||||
messages: vec![
|
messages: vec![
|
||||||
ChatMessage::assistant_blocks(vec![
|
ChatMessage::assistant_blocks(vec![
|
||||||
AssistantContentBlock::ToolCall(AssistantToolCall {
|
AssistantContentBlock::ToolCall(AssistantToolCall {
|
||||||
@@ -144,6 +230,11 @@ fn tool_results_are_sorted_by_previous_assistant_tool_call_order() {
|
|||||||
..ChatRequest::for_test()
|
..ChatRequest::for_test()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
request
|
||||||
|
.chat_options
|
||||||
|
.template_kwargs
|
||||||
|
.insert("thinking".to_string(), Value::Bool(false));
|
||||||
|
|
||||||
let rendered = render_request(&request);
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
@@ -186,6 +277,7 @@ fn drop_thinking_false_keeps_prior_assistant_reasoning() {
|
|||||||
.chat_options
|
.chat_options
|
||||||
.template_kwargs
|
.template_kwargs
|
||||||
.insert("drop_thinking".to_string(), Value::Bool(false));
|
.insert("drop_thinking".to_string(), Value::Bool(false));
|
||||||
|
request.chat_options.reasoning_effort = Some(ReasoningEffort::Low);
|
||||||
|
|
||||||
let rendered = render_request(&request);
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
@@ -197,7 +289,7 @@ fn drop_thinking_false_keeps_prior_assistant_reasoning() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn continue_final_assistant_omits_final_eos() {
|
fn continue_final_assistant_omits_final_eos() {
|
||||||
let request = ChatRequest {
|
let mut request = ChatRequest {
|
||||||
messages: vec![
|
messages: vec![
|
||||||
ChatMessage::user("write"),
|
ChatMessage::user("write"),
|
||||||
ChatMessage::assistant_text("partial answer"),
|
ChatMessage::assistant_text("partial answer"),
|
||||||
@@ -208,6 +300,10 @@ fn continue_final_assistant_omits_final_eos() {
|
|||||||
},
|
},
|
||||||
..ChatRequest::for_test()
|
..ChatRequest::for_test()
|
||||||
};
|
};
|
||||||
|
request
|
||||||
|
.chat_options
|
||||||
|
.template_kwargs
|
||||||
|
.insert("thinking".to_string(), Value::Bool(false));
|
||||||
|
|
||||||
let rendered = render_request(&request);
|
let rendered = render_request(&request);
|
||||||
|
|
||||||
|
|||||||
@@ -76,13 +76,16 @@ def test_deepseek_v4_tokenizer_registered():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_deepseek_v4_defaults_to_chat_mode():
|
def test_deepseek_v4_defaults_to_thinking_with_high_effort():
|
||||||
prompt = _tokenizer().apply_chat_template(
|
prompt = _tokenizer().apply_chat_template(
|
||||||
[{"role": "user", "content": "Hello"}],
|
[{"role": "user", "content": "Hello"}],
|
||||||
tokenize=False,
|
tokenize=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert prompt == ("<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>")
|
assert prompt.startswith(
|
||||||
|
"<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"
|
||||||
|
)
|
||||||
|
assert prompt.endswith("<|Assistant|><think>")
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("kwargs", [{"thinking": True}, {"enable_thinking": True}])
|
@pytest.mark.parametrize("kwargs", [{"thinking": True}, {"enable_thinking": True}])
|
||||||
@@ -93,7 +96,21 @@ def test_deepseek_v4_enables_thinking_with_compatible_kwargs(kwargs):
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert prompt == ("<|begin▁of▁sentence|><|User|>Hello<|Assistant|><think>")
|
assert prompt.startswith(
|
||||||
|
"<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"
|
||||||
|
)
|
||||||
|
assert prompt.endswith("<|Assistant|><think>")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("kwargs", [{"thinking": False}, {"enable_thinking": False}])
|
||||||
|
def test_deepseek_v4_explicitly_disables_thinking(kwargs):
|
||||||
|
prompt = _tokenizer().apply_chat_template(
|
||||||
|
[{"role": "user", "content": "Hello"}],
|
||||||
|
tokenize=False,
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert prompt == ("<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>")
|
||||||
|
|
||||||
|
|
||||||
def test_deepseek_v4_uses_v4_tool_prompt_from_request_tools():
|
def test_deepseek_v4_uses_v4_tool_prompt_from_request_tools():
|
||||||
@@ -123,7 +140,10 @@ def test_deepseek_v4_uses_v4_tool_prompt_from_request_tools():
|
|||||||
assert "</|DSML|tool_calls>" in prompt
|
assert "</|DSML|tool_calls>" in prompt
|
||||||
assert "function_calls" not in prompt
|
assert "function_calls" not in prompt
|
||||||
assert '"name": "get_weather"' in prompt
|
assert '"name": "get_weather"' in prompt
|
||||||
assert prompt.endswith("<|User|>Weather?<|Assistant|></think>")
|
assert prompt.startswith(
|
||||||
|
"<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"
|
||||||
|
)
|
||||||
|
assert prompt.endswith("<|User|>Weather?<|Assistant|><think>")
|
||||||
|
|
||||||
|
|
||||||
def test_deepseek_v4_renders_parsed_history_tool_arguments():
|
def test_deepseek_v4_renders_parsed_history_tool_arguments():
|
||||||
@@ -183,8 +203,17 @@ def test_deepseek_v4_renders_parsed_history_tool_arguments():
|
|||||||
assert 'parameter name="arguments"' not in prompt
|
assert 'parameter name="arguments"' not in prompt
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("reasoning_effort", ["minimal", "low", "medium", "high"])
|
@pytest.mark.parametrize(
|
||||||
def test_deepseek_v4_accepts_openai_reasoning_effort_values(reasoning_effort):
|
("reasoning_effort", "expected_prefix"),
|
||||||
|
[
|
||||||
|
("low", "<|begin▁of▁sentence|><|User|>Hello"),
|
||||||
|
("high", "<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"),
|
||||||
|
("max", "<|begin▁of▁sentence|>Reasoning Effort: Beyond maximum"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_deepseek_v4_renders_0731_reasoning_effort_prompts(
|
||||||
|
reasoning_effort, expected_prefix
|
||||||
|
):
|
||||||
prompt = _tokenizer().apply_chat_template(
|
prompt = _tokenizer().apply_chat_template(
|
||||||
[{"role": "user", "content": "Hello"}],
|
[{"role": "user", "content": "Hello"}],
|
||||||
tokenize=False,
|
tokenize=False,
|
||||||
@@ -193,7 +222,7 @@ def test_deepseek_v4_accepts_openai_reasoning_effort_values(reasoning_effort):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert prompt.endswith("<|Assistant|><think>")
|
assert prompt.endswith("<|Assistant|><think>")
|
||||||
assert "Reasoning Effort: Absolute maximum" not in prompt
|
assert prompt.startswith(expected_prefix)
|
||||||
|
|
||||||
|
|
||||||
def test_deepseek_v4_none_reasoning_effort_disables_thinking():
|
def test_deepseek_v4_none_reasoning_effort_disables_thinking():
|
||||||
@@ -211,11 +240,11 @@ def test_deepseek_v4_none_reasoning_effort_disables_thinking():
|
|||||||
("reasoning_effort", "expected_mode", "expected_effort"),
|
("reasoning_effort", "expected_mode", "expected_effort"),
|
||||||
[
|
[
|
||||||
("none", "chat", None),
|
("none", "chat", None),
|
||||||
("minimal", "thinking", "high"),
|
("minimal", "thinking", "low"),
|
||||||
("low", "thinking", "high"),
|
("low", "thinking", "low"),
|
||||||
("medium", "thinking", "high"),
|
("medium", "thinking", "low"),
|
||||||
("high", "thinking", "high"),
|
("high", "thinking", "high"),
|
||||||
("xhigh", "thinking", "max"),
|
("xhigh", "thinking", "high"),
|
||||||
("max", "thinking", "max"),
|
("max", "thinking", "max"),
|
||||||
("unexpected", "thinking", "high"),
|
("unexpected", "thinking", "high"),
|
||||||
],
|
],
|
||||||
@@ -248,7 +277,7 @@ def test_deepseek_v4_maps_compatible_thinking_reasoning_effort_values(
|
|||||||
assert captured_kwargs[-1]["reasoning_effort"] == expected_effort
|
assert captured_kwargs[-1]["reasoning_effort"] == expected_effort
|
||||||
|
|
||||||
|
|
||||||
def test_deepseek_v4_preserves_reference_max_reasoning_effort():
|
def test_deepseek_v4_renders_0731_max_reasoning_effort():
|
||||||
prompt = _tokenizer().apply_chat_template(
|
prompt = _tokenizer().apply_chat_template(
|
||||||
[{"role": "user", "content": "Hello"}],
|
[{"role": "user", "content": "Hello"}],
|
||||||
tokenize=False,
|
tokenize=False,
|
||||||
@@ -256,12 +285,10 @@ def test_deepseek_v4_preserves_reference_max_reasoning_effort():
|
|||||||
reasoning_effort="max",
|
reasoning_effort="max",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert prompt.startswith(
|
assert prompt.startswith("<|begin▁of▁sentence|>Reasoning Effort: Beyond maximum")
|
||||||
"<|begin▁of▁sentence|>Reasoning Effort: Absolute maximum"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_deepseek_v4_maps_xhigh_to_reference_max_reasoning_effort():
|
def test_deepseek_v4_maps_xhigh_to_high_reasoning_effort():
|
||||||
prompt = _tokenizer().apply_chat_template(
|
prompt = _tokenizer().apply_chat_template(
|
||||||
[{"role": "user", "content": "Hello"}],
|
[{"role": "user", "content": "Hello"}],
|
||||||
tokenize=False,
|
tokenize=False,
|
||||||
@@ -277,10 +304,10 @@ def test_deepseek_v4_maps_xhigh_to_reference_max_reasoning_effort():
|
|||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("case_id", "kwargs"),
|
("case_id", "kwargs"),
|
||||||
[
|
[
|
||||||
(1, {"thinking": True}),
|
(1, {"thinking": True, "reasoning_effort": "low"}),
|
||||||
(2, {"thinking": True}),
|
(2, {"thinking": True, "reasoning_effort": "low"}),
|
||||||
(3, {"thinking": True}),
|
(3, {"thinking": True, "reasoning_effort": "low"}),
|
||||||
(4, {}),
|
(4, {"thinking": False}),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_deepseek_v4_matches_reference_golden_fixtures(case_id, kwargs):
|
def test_deepseek_v4_matches_reference_golden_fixtures(case_id, kwargs):
|
||||||
|
|||||||
@@ -29,10 +29,12 @@ def get_deepseek_v4_tokenizer(tokenizer: HfTokenizer) -> HfTokenizer:
|
|||||||
tools: list[dict[str, Any]] | None = None,
|
tools: list[dict[str, Any]] | None = None,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> str | list[int]:
|
) -> str | list[int]:
|
||||||
thinking = kwargs.get("thinking", False)
|
thinking = kwargs.get("thinking")
|
||||||
enable_thinking = kwargs.get("enable_thinking", False)
|
enable_thinking = kwargs.get("enable_thinking")
|
||||||
thinking = thinking or enable_thinking
|
thinking_enabled = bool(thinking) or bool(enable_thinking)
|
||||||
thinking_mode = "thinking" if thinking else "chat"
|
if "thinking" not in kwargs and "enable_thinking" not in kwargs:
|
||||||
|
thinking_enabled = True
|
||||||
|
thinking_mode = "thinking" if thinking_enabled else "chat"
|
||||||
|
|
||||||
conversation = kwargs.get("conversation", messages)
|
conversation = kwargs.get("conversation", messages)
|
||||||
messages = conversation.copy()
|
messages = conversation.copy()
|
||||||
@@ -42,12 +44,14 @@ def get_deepseek_v4_tokenizer(tokenizer: HfTokenizer) -> HfTokenizer:
|
|||||||
|
|
||||||
reasoning_effort = kwargs.get("reasoning_effort")
|
reasoning_effort = kwargs.get("reasoning_effort")
|
||||||
if not isinstance(reasoning_effort, str):
|
if not isinstance(reasoning_effort, str):
|
||||||
reasoning_effort = None
|
reasoning_effort = "high" if thinking_enabled else None
|
||||||
elif reasoning_effort == "none":
|
elif reasoning_effort == "none":
|
||||||
thinking_mode = "chat"
|
thinking_mode = "chat"
|
||||||
reasoning_effort = None
|
reasoning_effort = None
|
||||||
elif reasoning_effort in ("max", "xhigh"):
|
elif reasoning_effort == "max":
|
||||||
reasoning_effort = "max"
|
reasoning_effort = "max"
|
||||||
|
elif reasoning_effort in ("low", "minimal", "medium"):
|
||||||
|
reasoning_effort = "low"
|
||||||
else:
|
else:
|
||||||
reasoning_effort = "high"
|
reasoning_effort = "high"
|
||||||
|
|
||||||
|
|||||||
@@ -65,11 +65,20 @@ tool_output_template: str = (
|
|||||||
"<tool_result>{content}</tool_result>"
|
"<tool_result>{content}</tool_result>"
|
||||||
)
|
)
|
||||||
|
|
||||||
REASONING_EFFORT_MAX = (
|
REASONING_EFFORT_PROMPTS: Dict[str, str] = {
|
||||||
"Reasoning Effort: Absolute maximum with no shortcuts permitted.\n"
|
"low": "",
|
||||||
"You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\n"
|
"high": (
|
||||||
"Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n"
|
"Reasoning Effort: Absolute maximum with no shortcuts permitted.\n"
|
||||||
)
|
"You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\n"
|
||||||
|
"Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n"
|
||||||
|
),
|
||||||
|
"max": (
|
||||||
|
"Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.\n"
|
||||||
|
"You MUST reason with the utmost depth and rigor, leaving absolutely nothing to chance: exhaustively decompose the problem into its most fundamental components, trace every causal chain to its root, and resolve the underlying cause rather than any surface symptom.\n"
|
||||||
|
"Do not stop reasoning until you have independently verified the solution from multiple angles and are certain that no assumption remains unchecked and no error remains undiscovered.\n\n"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
DEFAULT_REASONING_EFFORT = "low"
|
||||||
|
|
||||||
TOOLS_TEMPLATE = """## Tools
|
TOOLS_TEMPLATE = """## Tools
|
||||||
|
|
||||||
@@ -202,7 +211,8 @@ def render_message(index: int, messages: List[Dict[str, Any]], thinking_mode: st
|
|||||||
messages: Full list of messages in the conversation.
|
messages: Full list of messages in the conversation.
|
||||||
thinking_mode: Either "chat" or "thinking".
|
thinking_mode: Either "chat" or "thinking".
|
||||||
drop_thinking: Whether to drop reasoning content from earlier turns.
|
drop_thinking: Whether to drop reasoning content from earlier turns.
|
||||||
reasoning_effort: Optional reasoning effort level ("max", "high", or None).
|
reasoning_effort: Reasoning effort level, one of "low", "high", "max".
|
||||||
|
None is treated as "low".
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Encoded string for this message.
|
Encoded string for this message.
|
||||||
@@ -227,10 +237,13 @@ def render_message(index: int, messages: List[Dict[str, Any]], thinking_mode: st
|
|||||||
if tool_calls:
|
if tool_calls:
|
||||||
tool_calls = tool_calls_from_openai_format(tool_calls)
|
tool_calls = tool_calls_from_openai_format(tool_calls)
|
||||||
|
|
||||||
# Reasoning effort prefix (only at index 0 in thinking mode with max effort)
|
reasoning_effort = reasoning_effort or DEFAULT_REASONING_EFFORT
|
||||||
assert reasoning_effort in ['max', None, 'high'], f"Invalid reasoning effort: {reasoning_effort}"
|
assert reasoning_effort in REASONING_EFFORT_PROMPTS, (
|
||||||
if index == 0 and thinking_mode == "thinking" and reasoning_effort == 'max':
|
f"Invalid reasoning effort: {reasoning_effort}, expected one of "
|
||||||
prompt += REASONING_EFFORT_MAX
|
f"{list(REASONING_EFFORT_PROMPTS)}"
|
||||||
|
)
|
||||||
|
if index == 0 and thinking_mode == "thinking":
|
||||||
|
prompt += REASONING_EFFORT_PROMPTS[reasoning_effort]
|
||||||
|
|
||||||
if role == "system":
|
if role == "system":
|
||||||
prompt += system_msg_template.format(content=content or "")
|
prompt += system_msg_template.format(content=content or "")
|
||||||
@@ -497,7 +510,8 @@ def encode_messages(
|
|||||||
drop_thinking: If True, drop reasoning from earlier assistant turns
|
drop_thinking: If True, drop reasoning from earlier assistant turns
|
||||||
(only keep reasoning for messages after the last user message).
|
(only keep reasoning for messages after the last user message).
|
||||||
add_default_bos_token: Whether to prepend BOS token at conversation start.
|
add_default_bos_token: Whether to prepend BOS token at conversation start.
|
||||||
reasoning_effort: Optional reasoning effort level ("max", "high", or None).
|
reasoning_effort: Reasoning effort level, one of "low", "high", "max".
|
||||||
|
Only takes effect in thinking mode. None is treated as "low".
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The encoded prompt string.
|
The encoded prompt string.
|
||||||
|
|||||||
Reference in New Issue
Block a user