17
17
from typing import Optional , Union
18
18
19
19
from fastdeploy .entrypoints .openai .protocol import (ChatCompletionRequest ,
20
- DeltaMessage )
20
+ DeltaMessage )
21
21
from fastdeploy .reasoning import ReasoningParser , ReasoningParserManager
22
22
23
23
@@ -115,31 +115,41 @@ def extract_reasoning_content(
115
115
"""
116
116
Extract reasoning content from the model output.
117
117
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 - 缺少起始标签的格式
121
121
122
122
Returns:
123
123
tuple[Optional[str], Optional[str]]: reasoning content and content
124
124
"""
125
125
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
+ # 检查是否包含结束标签
137
127
if self .think_end_token not in model_output :
138
128
return None , model_output
139
129
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
143
154
144
- final_content = content or None
145
- return reasoning_content , final_content
155
+ return None , model_output
0 commit comments