polish code with new pre-commit rule (#2923)

This commit is contained in:
Zero Rains
2025-07-19 23:19:27 +08:00
committed by GitHub
parent b8676d71a8
commit 25698d56d1
424 changed files with 14307 additions and 13518 deletions

View File

@@ -13,11 +13,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""
from collections.abc import Sequence
from typing import Optional, Union
from fastdeploy.entrypoints.openai.protocol import (ChatCompletionRequest,
DeltaMessage)
from fastdeploy.entrypoints.openai.protocol import ChatCompletionRequest, DeltaMessage
from fastdeploy.reasoning import ReasoningParser, ReasoningParserManager
@@ -40,15 +40,13 @@ class Qwen3ReasoningParser(ReasoningParser):
if not self.model_tokenizer:
raise ValueError(
"The model tokenizer must be passed to the ReasoningParser "
"constructor during construction.")
"The model tokenizer must be passed to the ReasoningParser " "constructor during construction."
)
self.think_start_token_id = self.vocab.get(self.think_start_token)
self.think_end_token_id = self.vocab.get(self.think_end_token)
if self.think_end_token_id is None:
raise RuntimeError(
"Qwen3 reasoning parser could not locate think end "
"tokens in the tokenizer!")
raise RuntimeError("Qwen3 reasoning parser could not locate think end " "tokens in the tokenizer!")
def extract_reasoning_content_streaming(
self,
@@ -67,27 +65,23 @@ class Qwen3ReasoningParser(ReasoningParser):
- 'abc' goes to reasoning_content
- 'xyz' goes to content
"""
if len(delta_token_ids) == 1 and (delta_token_ids[0] in [
self.think_start_token_id, self.think_end_token_id
]):
if len(delta_token_ids) == 1 and (delta_token_ids[0] in [self.think_start_token_id, self.think_end_token_id]):
return "", ""
# </think> in delta
if self.think_end_token_id in delta_token_ids:
#<think> in delta, </think> in delta, extract reasoning content
# <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):]
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,
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 = 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
@@ -95,22 +89,18 @@ class Qwen3ReasoningParser(ReasoningParser):
return "", delta_text
# <think> in previous
elif self.think_start_token_id in previous_token_ids:
return delta_text,""
return delta_text, ""
# <think> in delta
elif self.think_start_token_id in delta_token_ids:
start_index=delta_text.find(self.think_start_token)
reasoning_content=delta_text[start_index + len(self.think_start_token):]
start_index = delta_text.find(self.think_start_token)
reasoning_content = delta_text[start_index + len(self.think_start_token) :]
content = ""
return reasoning_content, content
else:
return delta_text, ""
def extract_reasoning_content(
self, model_output: str, request: ChatCompletionRequest
self, model_output: str, request: ChatCompletionRequest
) -> tuple[Optional[str], Optional[str]]:
"""
Extract reasoning content from the model output.
@@ -130,22 +120,19 @@ class Qwen3ReasoningParser(ReasoningParser):
# 检查是否有起始标签
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):
if self.think_start_token not in model_output or self.think_end_token not in model_output:
return None, model_output
# Check if the <think> is present in the model output, remove it
# if it is present.
model_output_parts = model_output.partition(self.think_start_token)
model_output = model_output_parts[2] if model_output_parts[
1] else model_output_parts[0]
model_output = model_output_parts[2] if model_output_parts[1] else model_output_parts[0]
# Check if the model output contains the </think> tokens.
# If the end token is not found, return the model output as is.
if self.think_end_token not in model_output:
return None, model_output
# Extract reasoning content from the model output.
reasoning_content, _, content = model_output.partition(
self.think_end_token)
reasoning_content, _, content = model_output.partition(self.think_end_token)
final_content = content or None
return reasoning_content, final_content