fix: When the thinking process label is empty, do not process the thinking process#2179
fix: When the thinking process label is empty, do not process the thinking process#2179shaohuzhang1 merged 1 commit intomainfrom
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| return {'content': '', 'reasoning_content': chunk.content} | ||
| self.all_content += chunk.content | ||
| if not self.reasoning_content_is_start and len(self.all_content) >= self.reasoning_content_start_tag_len: | ||
| if self.all_content.startswith(self.reasoning_content_start_tag): |
There was a problem hiding this comment.
The provided Python code includes some minor improvements, such as handling cases where either start or end tags might be missing by setting appropriate attributes to zero or an empty string when necessary. However, there is room for further optimization and clarity:
-
Simplify Logic: The logic for determining start/end of the reasoning content can be simplified by checking conditions once and directly returning results based on these checks.
-
Avoid Redundant Checks: The redundant
len()calls for both tags within the same condition can be removed. -
Use Default Parameters: Consider using default parameters in method signatures that accept optional arguments with meaningful names, which improves readability and maintainability.
Here's an improved version of the code:
def __init__(self, reasoning_content_start="", reasoning_content_end=""):
self.all_content = ""
self.reasoning_content_start_tag = reasoning_content_start
self.reasoning_content_end_tag = reasoning_content_end
self.start_prefix_length = len(reasoning_content_start) if reasoning_content_start else 0
self.end_suffix_length = len(reasoning_content_end) if reasoning_content_end else 0
self.end_tag_prefix = reasoning_content_end[0] if self.end_suffix_length > 0 else ''
def get_reasoning_content(self, chunk):
all_content = self.all_content + chunk.content
# Start of reasoning content detected
if all_content.startswith(self.reasoning_content_start_tag):
result = {
'content': '',
'reasoning_content': ''.join(all_content.split(chunk.content)[1:])
}
return result
# End of reasoning content detected
elif all_content.endswith(self.reasoning_content_end_tag):
reason_text = "".join(all_content.split(chunk.content))
return {
'content': chunk.content,
'reasoning_content': reason_text[len(self.reasoning_content_end_tag):].strip()
}
# No tag matches (either at the beginning or the middle)
else:
self.all_content = ""
return {'content': chunk.content, 'reasoning_content': ''}Key Improvements:
- Single Check for Tags: The code now uses only one check (
startswithfollowed byendswith) instead of two separate checks, reducing redundancy. - Default Parameter Names: Added descriptive parameter names (
reasoning_content_start,reasoning_content_end) to improve understanding of their purpose. - String Manipulation Simplified: Used
.split()and slicing to simplify extracting text between tags. - Return Statement Clarity: Each branch of the logic returns a dictionary with clear keys indicating whether it contains main content or reasoning content.
fix: When the thinking process label is empty, do not process the thinking process