Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions swift/llm/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ def _swift_prepare_inputs(self, inputs: StdTemplateInputs):
i += 1
pre_message['content'], tool_content = self.agent_template._format_tool_responses(
pre_content, messages[i_start:i + 1])
# where tool_content is a List.
messages[i_start:i + 1] = [{'role': 'tool', 'content': tool_content}]
i = i_start + 1
elif pre_role == 'assistant' and role == 'assistant' or pre_role == 'user' and role == 'user':
Expand Down
5 changes: 3 additions & 2 deletions swift/llm/template/template/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ def _swift_prepare_inputs(self, inputs):
# Delete the content before '</think>' in all assistant turns except the last round.
if message['role'] == 'assistant' and isinstance(message['content'], str) and i != len(messages) - 1:
if self.with_answer:
message['content'] = message['content'].split('<answer>')[-1].rstrip().rstrip(
'</answer>').strip()
message['content'] = message['content'].split('<answer>')[-1].rstrip()
if message['content'].endswith('</answer>'):
message['content'] = message['content'][:-len('</answer>')].strip()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

While this change correctly fixes the issue with rstrip('</answer>'), it introduces a potential regression. The .strip() method is now only called if the content ends with </answer>. If it doesn't, any leading or trailing whitespace will be preserved, which was not the case in the original implementation which had an unconditional .strip() at the end.

To ensure consistent behavior and match the original intent of stripping whitespace in all cases, the .strip() call should be unconditional.

if message['content'].endswith('</answer>'):
    message['content'] = message['content'][:-len('</answer>')]
message['content'] = message['content'].strip()

else:
message['content'] = self.history_think_prefix + message['content'].split(
'</think>')[-1].strip()
Expand Down
Loading