Skip to content

Commit ccad084

Browse files
committed
add support QWQ enable_thinking
1 parent 050d965 commit ccad084

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

fastdeploy/reasoning/qwen3_reasoning_parsers.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from typing import Optional, Union
1818

1919
from fastdeploy.entrypoints.openai.protocol import (ChatCompletionRequest,
20-
DeltaMessage)
20+
DeltaMessage)
2121
from fastdeploy.reasoning import ReasoningParser, ReasoningParserManager
2222

2323

@@ -115,31 +115,41 @@ def extract_reasoning_content(
115115
"""
116116
Extract reasoning content from the model output.
117117
118-
For text abc</think>xyz:
119-
- 'abc' goes to reasoning_content
120-
- 'xyz' goes to content
118+
支持两种格式:
119+
1. <think>abc</think>xyz - 标准格式
120+
2. abc</think>xyz - 缺少起始标签的格式
121121
122122
Returns:
123123
tuple[Optional[str], Optional[str]]: reasoning content and content
124124
"""
125125

126-
# Check if the model output contains the <think> and </think> tokens.
127-
if (self.think_start_token not in model_output
128-
or self.think_end_token not in model_output):
129-
return None, model_output
130-
# Check if the <think> is present in the model output, remove it
131-
# if it is present.
132-
model_output_parts = model_output.partition(self.think_start_token)
133-
model_output = model_output_parts[2] if model_output_parts[
134-
1] else model_output_parts[0]
135-
# Check if the model output contains the </think> tokens.
136-
# If the end token is not found, return the model output as is.
126+
# 检查是否包含结束标签
137127
if self.think_end_token not in model_output:
138128
return None, model_output
139129

140-
# Extract reasoning content from the model output.
141-
reasoning_content, _, content = model_output.partition(
142-
self.think_end_token)
130+
# 检查是否有起始标签
131+
if self.think_start_token in model_output:
132+
# 标准格式:<think>content</think>answer
133+
start_idx = model_output.find(self.think_start_token)
134+
end_idx = model_output.find(self.think_end_token)
135+
136+
if start_idx != -1 and end_idx != -1 and start_idx < end_idx:
137+
reasoning_content = model_output[start_idx +
138+
len(self.think_start_token
139+
):end_idx].strip()
140+
final_content = model_output[end_idx +
141+
len(self.think_end_token):].strip(
142+
)
143+
final_content = final_content if final_content else None
144+
145+
return reasoning_content, final_content
146+
else:
147+
# 缺少起始标签的格式:content</think>answer
148+
parts = model_output.split(self.think_end_token, 1)
149+
150+
if len(parts) == 2:
151+
reasoning_content = parts[0].strip()
152+
final_content = parts[1].strip() if parts[1].strip() else None
153+
return reasoning_content, final_content
143154

144-
final_content = content or None
145-
return reasoning_content, final_content
155+
return None, model_output

0 commit comments

Comments
 (0)