add support QWQ enable_thinking (#2706)

* add support QWQ enable_thinking

* add stream=True

* fix stream=true

* fix qwen

---------

Co-authored-by: lizexu <lizexu@baidu.com>
This commit is contained in:
lizexu123
2025-07-04 20:55:23 +08:00
committed by GitHub
parent dacc46f04c
commit 9cb08e71e8

View File

@@ -67,47 +67,47 @@ class Qwen3ReasoningParser(ReasoningParser):
- 'abc' goes to reasoning_content
- 'xyz' goes to content
"""
# Skip single special tokens
if len(delta_token_ids) == 1 and (delta_token_ids[0] in [
self.think_start_token_id, self.think_end_token_id
]):
return "", ""
if self.think_start_token_id in previous_token_ids:
# </think> in delta
if self.think_end_token_id in delta_token_ids:
#<think> in delta, </think> in delta, extract reasoning content
if self.think_start_token_id in delta_token_ids:
start_index = delta_text.find(self.think_start_token)
end_index = delta_token_ids.find(self.think_end_token)
reasoning_content = delta_text[start_index +
len(self.think_start_token
):end_index]
content = delta_text[end_index+len(self.think_end_token):]
return reasoning_content, content
# <think> in previous, </think> in delta,
# extract reasoning content
else:
end_index = delta_text.find(self.think_end_token)
reasoning_content = delta_text[:end_index]
content = delta_text[end_index + len(self.think_end_token):]
content = content if content else None
return reasoning_content, content
# </think> in previous reasoning content continues
elif self.think_end_token_id in previous_token_ids:
# <think> in previous, </think> in previous,
# reasoning content continues
return "", delta_text
else:
# <think> in previous, no </think> in previous or delta,
# reasoning content continues
# <think> in previous
elif self.think_start_token_id in previous_token_ids:
return delta_text,""
# <think> in delta
elif self.think_start_token_id in delta_token_ids:
if self.think_end_token_id in delta_token_ids:
# <think> in delta, </think> in delta, extract reasoning content
start_index=delta_text.find(self.think_start_token)
end_index = delta_text.find(self.think_end_token)
reasoning_content = delta_text[start_index +
len(self.think_start_token
):end_index]
content = delta_text[end_index + len(self.think_end_token):]
content = content if content else None
reasoning_content=delta_text[start_index + len(self.think_start_token):]
content = ""
return reasoning_content, content
else:
# <think> in delta, no </think> in delta,
# reasoning content continues
return delta_text, ""
else:
# thinking is disabled, just content
return "", delta_text
def extract_reasoning_content(
self, model_output: str, request: ChatCompletionRequest
@@ -115,15 +115,21 @@ class Qwen3ReasoningParser(ReasoningParser):
"""
Extract reasoning content from the model output.
For text abc</think>xyz:
- 'abc' goes to reasoning_content
- 'xyz' goes to content
支持两种格式:
1. <think>abc</think>xyz - 标准格式
2. abc</think>xyz - 缺少起始标签的格式
Returns:
tuple[Optional[str], Optional[str]]: reasoning content and content
"""
# Check if the model output contains the <think> and </think> tokens.
# 检查是否包含结束标签
if self.think_end_token not in model_output:
return None, model_output
# 检查是否有起始标签
if self.think_start_token in model_output:
# 标准格式:<think>content</think>answer
if (self.think_start_token not in model_output
or self.think_end_token not in model_output):
return None, model_output
@@ -143,3 +149,13 @@ class Qwen3ReasoningParser(ReasoningParser):
final_content = content or None
return reasoning_content, final_content
else:
# 缺少起始标签的格式content</think>answer
parts = model_output.split(self.think_end_token, 1)
if len(parts) == 2:
reasoning_content = parts[0].strip()
final_content = parts[1].strip() if parts[1].strip() else None
return reasoning_content, final_content
return None, model_output