-
Notifications
You must be signed in to change notification settings - Fork 613
fix: qwen3 nonstream parse with no or uncompleted think content #3748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ywx217
wants to merge
1
commit into
InternLM:main
Choose a base branch
from
ywx217:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+73
−33
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,22 +77,8 @@ def extract_reasoning_content_streaming( | |
| # reasoning content continues | ||
| return DeltaMessage(reasoning_content=delta_text) | ||
| else: | ||
| # No <think> in previous or delta, also need to check for </think>. | ||
| # Because the model may have generated </think> without <think> | ||
| # Ref https://huggingface.co/deepseek-ai/DeepSeek-R1/commit/8a58a132790c9935686eb97f042afa8013451c9f | ||
| if self.think_end_token in delta_text: | ||
| # </think> in delta with more tokens, | ||
| # extract reasoning content and content | ||
| 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):] | ||
| return DeltaMessage(reasoning_content=reasoning_content, content=content if content else None) | ||
| elif self.think_end_token in previous_text: | ||
| # </think> in previous, thinking content ends | ||
| return DeltaMessage(content=delta_text) | ||
| else: | ||
| # no </think> in previous or delta, reasoning content continues | ||
| return DeltaMessage(reasoning_content=delta_text) | ||
| # no <think> in previous or delta, all content | ||
| return DeltaMessage(content=delta_text) | ||
|
|
||
| def extract_reasoning_content(self, model_output: str, request: ChatCompletionRequest, | ||
| **kwargs) -> Tuple[Optional[str], Optional[str]]: | ||
|
|
@@ -109,26 +95,35 @@ def extract_reasoning_content(self, model_output: str, request: ChatCompletionRe | |
| reasoning_content (str | None): The reasoning content. | ||
| final_output (str | None): The content. | ||
| """ | ||
| # DeepSeek R1 doesn't generate <think> now. | ||
| start_index = model_output.find(self.think_start_token) | ||
| end_index = model_output.find(self.think_end_token) | ||
| # Thus we assume the reasoning content is always at the start. | ||
| # Ref https://huggingface.co/deepseek-ai/DeepSeek-R1/commit/8a58a132790c9935686eb97f042afa8013451c9f | ||
| if self.think_end_token not in model_output: | ||
| if end_index < 0: | ||
| # for qwen3 model, the reasoning content is wrapped by <think> </think> xml tags | ||
| return None, model_output | ||
| # Add a start token if it's missing to keep compatibility. | ||
| if self.think_start_token not in model_output: | ||
| model_output = f'{self.think_start_token}{model_output}' | ||
| # Use a regex to find the reasoning content | ||
| reasoning_content = self.reasoning_regex.findall(model_output)[0] | ||
|
|
||
| end_index = len(f'{self.think_start_token}{reasoning_content}{self.think_end_token}') | ||
| final_output = model_output[end_index:] | ||
| if reasoning_content.startswith('\n'): | ||
| reasoning_content = reasoning_content[1:] | ||
| if reasoning_content.endswith('\n'): | ||
| reasoning_content = reasoning_content[:-1] | ||
| if start_index < 0: | ||
| return None, model_output | ||
| reasoning_content = model_output[start_index + len(self.think_start_token):] | ||
| reasoning_content = self._trim_newlines(reasoning_content) | ||
| return reasoning_content, None | ||
|
|
||
| if start_index >= 0 and start_index < end_index: | ||
| reasoning_content = model_output[start_index + len(self.think_start_token):end_index] | ||
| else: | ||
| reasoning_content = model_output[:end_index] | ||
| reasoning_content = self._trim_newlines(reasoning_content) | ||
|
|
||
| final_output = model_output[end_index + len(self.think_end_token):] | ||
| final_output = self._trim_newlines(final_output) | ||
|
|
||
| if len(final_output) == 0: | ||
| return reasoning_content, None | ||
|
|
||
| return reasoning_content, final_output | ||
|
|
||
| @classmethod | ||
| def _trim_newlines(cls, text: str): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why perform There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
to remove |
||
| """Trim newlines from the start and end of a string.""" | ||
| while text.startswith('\n'): | ||
| text = text[1:] | ||
| while text.endswith('\n'): | ||
| text = text[:-1] | ||
| return text | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the model does not have reasoning ability, but the output becomes reasoning_content?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any specific model name and model output for this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the model does not have reasoning ability, the output should be normal content, not reasoning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi, when enable_thinking=False, the parser is disabled in main branch
lmdeploy/lmdeploy/serve/openai/api_server.py
Line 508 in 5f0647f