[Frontend] DeepSeek V4 0731 reasoning effort prompts & mappings (#50580)

This commit is contained in:
Bugen Zhao
2026-08-04 08:51:32 -07:00
committed by GitHub
parent f9c74b4b9c
commit 7743486190
5 changed files with 205 additions and 51 deletions
@@ -24,11 +24,16 @@ const THINKING_END_TOKEN: &str = "</think>";
const DSML_TOKEN: &str = "|DSML|";
const USER_SP_TOKEN: &str = "<|User|>";
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",
"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",
);
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)]
enum ThinkingMode {
@@ -47,7 +52,7 @@ struct RenderedToolSchema<'a> {
/// Render one chat request into the final prompt 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 synthetic_tool_system = needs_synthetic_tool_system(request, request_tools);
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 =
find_last_user_render_index(request.messages.as_slice(), synthetic_tool_system);
let mut out = String::from(BOS_TOKEN);
if thinking_mode == ThinkingMode::Thinking && max_reasoning_effort {
out.push_str(REASONING_EFFORT_MAX);
if thinking_mode == ThinkingMode::Thinking {
out.push_str(reasoning_effort_prompt);
}
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
/// `reasoning_effort`; the generic template-kwargs map is left for HF
/// templates.
fn resolve_thinking_options(request: &ChatRequest) -> Result<(ThinkingMode, bool)> {
let mut thinking_mode = match request.enable_thinking()?.unwrap_or(false) {
fn resolve_thinking_options(request: &ChatRequest) -> Result<(ThinkingMode, &'static str)> {
let mut thinking_mode = match request.enable_thinking()?.unwrap_or(true) {
true => ThinkingMode::Thinking,
false => ThinkingMode::Chat,
};
let mut max_reasoning_effort = false;
let mut reasoning_effort_prompt = REASONING_EFFORT_HIGH;
match request.chat_options.reasoning_effort {
Some(ReasoningEffort::None) => thinking_mode = ThinkingMode::Chat,
Some(ReasoningEffort::Max | ReasoningEffort::XHigh) => max_reasoning_effort = true,
Some(_) | None => {}
Some(ReasoningEffort::Max) => {
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.
+100 -4
View File
@@ -22,7 +22,9 @@ fn render_request(request: &ChatRequest) -> String {
}
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 {
@@ -75,6 +77,29 @@ fn reasoning_effort_max_adds_prefix_when_thinking_is_enabled() {
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#"
<|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.
@@ -84,6 +109,66 @@ fn reasoning_effort_max_adds_prefix_when_thinking_is_enabled() {
.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]
fn reasoning_effort_none_disables_thinking() {
let mut request = ChatRequest {
@@ -119,12 +204,13 @@ fn reasoning_effort_template_kwarg_is_ignored() {
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]
fn tool_results_are_sorted_by_previous_assistant_tool_call_order() {
let request = ChatRequest {
let mut request = ChatRequest {
messages: vec![
ChatMessage::assistant_blocks(vec![
AssistantContentBlock::ToolCall(AssistantToolCall {
@@ -144,6 +230,11 @@ fn tool_results_are_sorted_by_previous_assistant_tool_call_order() {
..ChatRequest::for_test()
};
request
.chat_options
.template_kwargs
.insert("thinking".to_string(), Value::Bool(false));
let rendered = render_request(&request);
expect![[r#"
@@ -186,6 +277,7 @@ fn drop_thinking_false_keeps_prior_assistant_reasoning() {
.chat_options
.template_kwargs
.insert("drop_thinking".to_string(), Value::Bool(false));
request.chat_options.reasoning_effort = Some(ReasoningEffort::Low);
let rendered = render_request(&request);
@@ -197,7 +289,7 @@ fn drop_thinking_false_keeps_prior_assistant_reasoning() {
#[test]
fn continue_final_assistant_omits_final_eos() {
let request = ChatRequest {
let mut request = ChatRequest {
messages: vec![
ChatMessage::user("write"),
ChatMessage::assistant_text("partial answer"),
@@ -208,6 +300,10 @@ fn continue_final_assistant_omits_final_eos() {
},
..ChatRequest::for_test()
};
request
.chat_options
.template_kwargs
.insert("thinking".to_string(), Value::Bool(false));
let rendered = render_request(&request);