Enables force reasoning based on chat template for Qwen3-Thinking (#8369)
Signed-off-by: Xinyuan Tong <xinyuantong.cs@gmail.com> Signed-off-by: Xinyuan Tong <justinning0323@outlook.com> Co-authored-by: Chang Su <csu272@usc.edu>
This commit is contained in:
@@ -332,6 +332,8 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
prompt = prompt[: -len(conv.sep2)]
|
||||
else:
|
||||
prompt = conv.get_prompt()
|
||||
if self._get_enable_thinking_from_request(request):
|
||||
prompt += "<think>" # Note(Xinyuan): hard code thinking token
|
||||
|
||||
image_data = conv.image_data if conv.image_data else None
|
||||
video_data = conv.video_data if conv.video_data else None
|
||||
@@ -840,7 +842,9 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
if reasoning_parser and request.separate_reasoning:
|
||||
try:
|
||||
parser = ReasoningParser(
|
||||
model_type=reasoning_parser, stream_reasoning=False
|
||||
model_type=reasoning_parser,
|
||||
stream_reasoning=False,
|
||||
force_reasoning=self.template_manager.force_reasoning,
|
||||
)
|
||||
reasoning_text, text = parser.parse_non_stream(text)
|
||||
except Exception as e:
|
||||
@@ -1006,11 +1010,12 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
reasoning_parser_dict[index] = ReasoningParser(
|
||||
self.tokenizer_manager.server_args.reasoning_parser,
|
||||
request.stream_reasoning,
|
||||
self.template_manager.force_reasoning,
|
||||
)
|
||||
reasoning_parser = reasoning_parser_dict[index]
|
||||
return reasoning_parser.parse_stream_chunk(delta)
|
||||
|
||||
def _get_enable_thinking_from_request(request: ChatCompletionRequest) -> bool:
|
||||
def _get_enable_thinking_from_request(self, request: ChatCompletionRequest) -> bool:
|
||||
"""Extracts the 'enable_thinking' flag from request chat_template_kwargs.
|
||||
|
||||
NOTE: This parameter is only useful for models that support enable_thinking
|
||||
@@ -1019,7 +1024,7 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
Args:
|
||||
request_obj: The request object (or an item from a list of requests).
|
||||
Returns:
|
||||
The boolean value of 'enable_thinking' if found and not True, otherwise True.
|
||||
The boolean value of 'enable_thinking' if found, otherwise False.
|
||||
"""
|
||||
if (
|
||||
hasattr(request, "chat_template_kwargs")
|
||||
@@ -1027,7 +1032,7 @@ class OpenAIServingChat(OpenAIServingBase):
|
||||
and request.chat_template_kwargs.get("enable_thinking") is not None
|
||||
):
|
||||
return request.chat_template_kwargs.get("enable_thinking")
|
||||
return True
|
||||
return False
|
||||
|
||||
async def _process_tool_call_stream(
|
||||
self,
|
||||
|
||||
@@ -21,6 +21,7 @@ and code completion templates, eliminating global state and improving modularity
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
from typing import Optional
|
||||
|
||||
from sglang.srt.code_completion_parser import (
|
||||
@@ -54,6 +55,7 @@ class TemplateManager:
|
||||
self._chat_template_name: Optional[str] = None
|
||||
self._completion_template_name: Optional[str] = None
|
||||
self._jinja_template_content_format: Optional[str] = "openai"
|
||||
self._force_reasoning: bool = False
|
||||
|
||||
@property
|
||||
def chat_template_name(self) -> Optional[str]:
|
||||
@@ -70,6 +72,31 @@ class TemplateManager:
|
||||
"""Get the detected template content format ('string' or 'openai' or None)."""
|
||||
return self._jinja_template_content_format
|
||||
|
||||
@property
|
||||
def force_reasoning(self) -> bool:
|
||||
"""
|
||||
Check if the current chat template enforces reasoning/thinking.
|
||||
|
||||
Returns:
|
||||
True if the template contains reasoning patterns like <think> tags
|
||||
"""
|
||||
return self._force_reasoning
|
||||
|
||||
def _detect_reasoning_pattern(self, template: str) -> bool:
|
||||
"""
|
||||
Detect if the chat template contains reasoning/thinking patterns.
|
||||
"""
|
||||
if template is None:
|
||||
return False
|
||||
|
||||
force_reasoning_pattern = r"<\|im_start\|>assistant\\n<think>\\n"
|
||||
has_reasoning = re.search(force_reasoning_pattern, template) is not None
|
||||
|
||||
if has_reasoning:
|
||||
logger.info("Detected the force reasoning pattern in chat template.")
|
||||
|
||||
return has_reasoning
|
||||
|
||||
def load_chat_template(
|
||||
self, tokenizer_manager, chat_template_arg: Optional[str], model_path: str
|
||||
) -> None:
|
||||
@@ -93,7 +120,8 @@ class TemplateManager:
|
||||
hf_template = self._resolve_hf_chat_template(tokenizer_manager)
|
||||
if hf_template:
|
||||
# override the chat template
|
||||
tokenizer_manager.tokenizer.chat_template = hf_template
|
||||
if tokenizer_manager.tokenizer:
|
||||
tokenizer_manager.tokenizer.chat_template = hf_template
|
||||
self._jinja_template_content_format = (
|
||||
detect_jinja_template_content_format(hf_template)
|
||||
)
|
||||
@@ -106,6 +134,12 @@ class TemplateManager:
|
||||
self._jinja_template_content_format = "string"
|
||||
logger.info("No chat template found, defaulting to 'string' content format")
|
||||
|
||||
# Detect reasoning pattern from chat template
|
||||
if tokenizer_manager.tokenizer:
|
||||
self._force_reasoning = self._detect_reasoning_pattern(
|
||||
tokenizer_manager.tokenizer.chat_template
|
||||
)
|
||||
|
||||
def _load_explicit_chat_template(
|
||||
self, tokenizer_manager, chat_template_arg: str
|
||||
) -> None:
|
||||
|
||||
@@ -131,7 +131,7 @@ class DeepSeekR1Detector(BaseReasoningFormatDetector):
|
||||
If True, streams reasoning content as it arrives.
|
||||
"""
|
||||
|
||||
def __init__(self, stream_reasoning: bool = True):
|
||||
def __init__(self, stream_reasoning: bool = True, force_reasoning: bool = True):
|
||||
# DeepSeek-R1 is assumed to be reasoning until `</think>` token
|
||||
super().__init__(
|
||||
"<think>",
|
||||
@@ -144,7 +144,7 @@ class DeepSeekR1Detector(BaseReasoningFormatDetector):
|
||||
|
||||
class Qwen3Detector(BaseReasoningFormatDetector):
|
||||
"""
|
||||
Detector for standard Qwen3 models (e.g., Qwen/Qwen3-235B-A22B).
|
||||
Detector for Qwen3 models (e.g., Qwen/Qwen3-235B-A22B).
|
||||
Assumes reasoning format:
|
||||
(<think>)*(.*)</think>
|
||||
|
||||
@@ -153,47 +153,16 @@ class Qwen3Detector(BaseReasoningFormatDetector):
|
||||
- enable_thinking=True: "<think>reasoning content</think>The answer is 42."
|
||||
- enable_thinking=False: "The answer is 42." (no thinking tokens)
|
||||
|
||||
This detector handles both cases.
|
||||
|
||||
NOTE: Do NOT use this detector for Qwen3-Thinking models (e.g., Qwen3-Thinking-2507).
|
||||
Those models always generate thinking content without <think> start tags.
|
||||
Use "qwen3-thinking" parser type for those models instead.
|
||||
|
||||
Args:
|
||||
stream_reasoning (bool): If False, accumulates reasoning content until the end tag.
|
||||
If True, streams reasoning content as it arrives.
|
||||
"""
|
||||
|
||||
def __init__(self, stream_reasoning: bool = True):
|
||||
def __init__(self, stream_reasoning: bool = True, force_reasoning: bool = False):
|
||||
super().__init__(
|
||||
"<think>",
|
||||
"</think>",
|
||||
force_reasoning=False,
|
||||
stream_reasoning=stream_reasoning,
|
||||
)
|
||||
|
||||
|
||||
class Qwen3ThinkingDetector(BaseReasoningFormatDetector):
|
||||
"""
|
||||
Detector for Qwen3-Thinking models (e.g., Qwen3-Thinking-2507).
|
||||
Assumes reasoning format:
|
||||
*(.*)</think>
|
||||
|
||||
These models always generate thinking content without <think> start tag.
|
||||
They do not support the enable_thinking parameter and always think.
|
||||
|
||||
Format: "I need to think about this...</think>The answer is 42."
|
||||
|
||||
Args:
|
||||
stream_reasoning (bool): If False, accumulates reasoning content until the end tag.
|
||||
If True, streams reasoning content as it arrives.
|
||||
"""
|
||||
|
||||
def __init__(self, stream_reasoning: bool = True):
|
||||
super().__init__(
|
||||
"<think>",
|
||||
"</think>",
|
||||
force_reasoning=True,
|
||||
force_reasoning=force_reasoning,
|
||||
stream_reasoning=stream_reasoning,
|
||||
)
|
||||
|
||||
@@ -207,7 +176,7 @@ class KimiDetector(BaseReasoningFormatDetector):
|
||||
and the rest of the text as `normal_text`.
|
||||
"""
|
||||
|
||||
def __init__(self, stream_reasoning: bool = True):
|
||||
def __init__(self, stream_reasoning: bool = True, force_reasoning: bool = False):
|
||||
super().__init__(
|
||||
"◁think▷",
|
||||
"◁/think▷",
|
||||
@@ -230,13 +199,18 @@ class ReasoningParser:
|
||||
DetectorMap: Dict[str, Type[BaseReasoningFormatDetector]] = {
|
||||
"deepseek-r1": DeepSeekR1Detector,
|
||||
"qwen3": Qwen3Detector,
|
||||
"qwen3-thinking": Qwen3ThinkingDetector,
|
||||
"qwen3-thinking": Qwen3Detector,
|
||||
"glm45": Qwen3Detector,
|
||||
"kimi": KimiDetector,
|
||||
"step3": DeepSeekR1Detector,
|
||||
}
|
||||
|
||||
def __init__(self, model_type: Optional[str] = None, stream_reasoning: bool = True):
|
||||
def __init__(
|
||||
self,
|
||||
model_type: Optional[str] = None,
|
||||
stream_reasoning: bool = True,
|
||||
force_reasoning: bool = False,
|
||||
):
|
||||
if not model_type:
|
||||
raise ValueError("Model type must be specified")
|
||||
|
||||
@@ -244,7 +218,12 @@ class ReasoningParser:
|
||||
if not detector_class:
|
||||
raise ValueError(f"Unsupported model type: {model_type}")
|
||||
|
||||
self.detector = detector_class(stream_reasoning=stream_reasoning)
|
||||
if model_type.lower() == "qwen3-thinking":
|
||||
force_reasoning = True
|
||||
|
||||
self.detector = detector_class(
|
||||
stream_reasoning=stream_reasoning, force_reasoning=force_reasoning
|
||||
)
|
||||
|
||||
def parse_non_stream(self, full_text: str) -> Tuple[str, str]:
|
||||
"""Non-streaming call: one-time parsing"""
|
||||
|
||||
Reference in New Issue
Block a user