Repository navigation
Retain newlines in chat template when continue_final_message=True
#34253
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3dcaee1
Retain newlines in chat template when
lewtun aeeee98
Merge branch 'main' into fix-continue-msg
lewtun 0fd98e1
Add try/except
lewtun 92fed23
Merge branch 'main' into fix-continue-msg
lewtun 39d4f42
Merge branch 'main' into fix-continue-msg
lewtun 138ee7b
Add regression test
lewtun 2cbbc1f
Simplify test
lewtun 24ac783
Merge branch 'main' into fix-continue-msg
lewtun a1f99aa
Apply suggestions from code review
lewtun 242a073
Merge branch 'main' into fix-continue-msg
lewtun 4d00a03
Merge branch 'main' into fix-continue-msg
lewtun a245df1
Merge branch 'main' into fix-continue-msg
lewtun 0f05f9e
Merge branch 'main' into fix-continue-msg
lewtun 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
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 |
|---|---|---|
|
|
@@ -1461,6 +1461,38 @@ def test_continue_final_message(self): | |
| "<|im_start|>system\nsystem message<|im_end|>\n<|im_start|>user\nuser message<|im_end|>\n<|im_start|>assistant\nassistant message", | ||
| ) | ||
|
|
||
| @require_jinja | ||
| def test_continue_final_message_with_trim(self): | ||
| """Regression test for chat templates with trimming: https://github.com/huggingface/transformers/pull/34214""" | ||
|
|
||
| dummy_template = """ | ||
| {%- for message in messages %} | ||
| {{- "<|im_start|>" + message['role'] + "\n" + message['content'] | trim + "<|im_end|>" + "\n"}} | ||
| {%- endfor %}""" | ||
| dummy_conversation = [ | ||
| {"role": "system", "content": "system message"}, | ||
| {"role": "user", "content": "user message"}, | ||
| {"role": "assistant", "content": "assistant message "}, # Note the trailing whitespace | ||
| ] | ||
| tokenizers = self.get_tokenizers() | ||
| for tokenizer in tokenizers: | ||
| with self.subTest(f"{tokenizer.__class__.__name__}"): | ||
| output = tokenizer.apply_chat_template( | ||
| dummy_conversation, chat_template=dummy_template, tokenize=False, continue_final_message=False | ||
| ) | ||
| self.assertEqual( | ||
| output, | ||
| "<|im_start|>system\nsystem message<|im_end|>\n<|im_start|>user\nuser message<|im_end|>\n<|im_start|>assistant\nassistant message<|im_end|>\n", | ||
| ) | ||
| prefill_output = tokenizer.apply_chat_template( | ||
| dummy_conversation, chat_template=dummy_template, tokenize=False, continue_final_message=True | ||
| ) | ||
| # Assert that the final message is unterminated | ||
| self.assertEqual( | ||
| prefill_output, | ||
| "<|im_start|>system\nsystem message<|im_end|>\n<|im_start|>user\nuser message<|im_end|>\n<|im_start|>assistant\nassistant message", | ||
|
Member
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. Nice! This is a clean test. |
||
| ) | ||
|
|
||
| @require_jinja | ||
| def test_chat_template_dict(self): | ||
| dummy_template_1 = "{{'a'}}" | ||
|
|
||
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.
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.
Added a regression test here @Rocketknight1