[Frontend] DeepSeek V4 0731 reasoning effort prompts & mappings (#50580)
This commit is contained in:
@@ -29,10 +29,12 @@ def get_deepseek_v4_tokenizer(tokenizer: HfTokenizer) -> HfTokenizer:
|
||||
tools: list[dict[str, Any]] | None = None,
|
||||
**kwargs,
|
||||
) -> str | list[int]:
|
||||
thinking = kwargs.get("thinking", False)
|
||||
enable_thinking = kwargs.get("enable_thinking", False)
|
||||
thinking = thinking or enable_thinking
|
||||
thinking_mode = "thinking" if thinking else "chat"
|
||||
thinking = kwargs.get("thinking")
|
||||
enable_thinking = kwargs.get("enable_thinking")
|
||||
thinking_enabled = bool(thinking) or bool(enable_thinking)
|
||||
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)
|
||||
messages = conversation.copy()
|
||||
@@ -42,12 +44,14 @@ def get_deepseek_v4_tokenizer(tokenizer: HfTokenizer) -> HfTokenizer:
|
||||
|
||||
reasoning_effort = kwargs.get("reasoning_effort")
|
||||
if not isinstance(reasoning_effort, str):
|
||||
reasoning_effort = None
|
||||
reasoning_effort = "high" if thinking_enabled else None
|
||||
elif reasoning_effort == "none":
|
||||
thinking_mode = "chat"
|
||||
reasoning_effort = None
|
||||
elif reasoning_effort in ("max", "xhigh"):
|
||||
elif reasoning_effort == "max":
|
||||
reasoning_effort = "max"
|
||||
elif reasoning_effort in ("low", "minimal", "medium"):
|
||||
reasoning_effort = "low"
|
||||
else:
|
||||
reasoning_effort = "high"
|
||||
|
||||
|
||||
@@ -65,11 +65,20 @@ tool_output_template: str = (
|
||||
"<tool_result>{content}</tool_result>"
|
||||
)
|
||||
|
||||
REASONING_EFFORT_MAX = (
|
||||
"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"
|
||||
)
|
||||
REASONING_EFFORT_PROMPTS: Dict[str, str] = {
|
||||
"low": "",
|
||||
"high": (
|
||||
"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
|
||||
|
||||
@@ -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.
|
||||
thinking_mode: Either "chat" or "thinking".
|
||||
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:
|
||||
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:
|
||||
tool_calls = tool_calls_from_openai_format(tool_calls)
|
||||
|
||||
# Reasoning effort prefix (only at index 0 in thinking mode with max effort)
|
||||
assert reasoning_effort in ['max', None, 'high'], f"Invalid reasoning effort: {reasoning_effort}"
|
||||
if index == 0 and thinking_mode == "thinking" and reasoning_effort == 'max':
|
||||
prompt += REASONING_EFFORT_MAX
|
||||
reasoning_effort = reasoning_effort or DEFAULT_REASONING_EFFORT
|
||||
assert reasoning_effort in REASONING_EFFORT_PROMPTS, (
|
||||
f"Invalid reasoning effort: {reasoning_effort}, expected one of "
|
||||
f"{list(REASONING_EFFORT_PROMPTS)}"
|
||||
)
|
||||
if index == 0 and thinking_mode == "thinking":
|
||||
prompt += REASONING_EFFORT_PROMPTS[reasoning_effort]
|
||||
|
||||
if role == "system":
|
||||
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
|
||||
(only keep reasoning for messages after the last user message).
|
||||
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:
|
||||
The encoded prompt string.
|
||||
|
||||
Reference in New Issue
Block a user