Skip to content

Consistent type checks for prepend and append tags. #1824

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 4 commits into from
Oct 14, 2024
Merged
Changes from 3 commits
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
32 changes: 21 additions & 11 deletions torchtune/data/_prompt_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ def __call__(
if message.role in self.template:
prepend_tag = self.template[message.role][0]
append_tag = self.template[message.role][1]
content = (
[{"type": "text", "content": prepend_tag}]
+ message.content
+ [{"type": "text", "content": append_tag}]
)
content = message.content

if isinstance(prepend_tag, str) and len(prepend_tag) > 0:
content = [{"type": "text", "content": prepend_tag}] + content

if isinstance(append_tag, str) and len(append_tag) > 0:
content = content + [{"type": "text", "content": append_tag}]
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: you could move content = message.content above the if statement and remove this else entirely

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

content = message.content
formatted_dialogue.append(
Expand Down Expand Up @@ -183,13 +185,21 @@ def __call__(
and index == len(messages) - 1
and len(message.text_content) == 0
):
content = [{"type": "text", "content": prepend_tag}] + message.content
if isinstance(prepend_tag, str) and len(prepend_tag) > 0:
content = [
{"type": "text", "content": prepend_tag}
] + message.content
else:
content = message.content
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar comment here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

else:
content = (
[{"type": "text", "content": prepend_tag}]
+ message.content
+ [{"type": "text", "content": append_tag}]
)
content = message.content

if isinstance(prepend_tag, str) and len(prepend_tag) > 0:
content = [{"type": "text", "content": prepend_tag}] + content

if isinstance(append_tag, str) and len(append_tag) > 0:
content = content + [{"type": "text", "content": append_tag}]

formatted_dialogue.append(
Message(
role=message.role,
Expand Down
Loading