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 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
50 changes: 39 additions & 11 deletions torchtune/data/_prompt_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,22 @@ 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}]
)
if isinstance(prepend_tag, str) and isinstance(append_tag, str):
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe we should structure this slightly different to avoid the cascade of ifs:

content = message.content
if message.role in self.template:
    prepend_tag = self.template[message.role][0]
    append_tag = self.template[message.role][1]
    if prepend_tag is not None and len(prepend_tag) > 0:
        # add prepend tag
    if append_tag is not None and len(append_tag) > 0:
        # add append tag
formatted_dialogue.append(Message(...))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It depends on if can something "bad" except None can come there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I see now how do you want to do it. Ok, let me fix it like this. But I would like to save isinstance there

content = (
[{"type": "text", "content": prepend_tag}]
+ message.content
+ [{"type": "text", "content": append_tag}]
)
elif not isinstance(prepend_tag, str) and isinstance(append_tag, str):
content = message.content + [
{"type": "text", "content": append_tag}
]
elif isinstance(prepend_tag, str) and not isinstance(append_tag, str):
content = [
{"type": "text", "content": prepend_tag}
] + message.content
else:
content = {message.content}
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 +194,30 @@ 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):
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}]
)
if isinstance(prepend_tag, str) and isinstance(append_tag, str):
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 above

content = (
[{"type": "text", "content": prepend_tag}]
+ message.content
+ [{"type": "text", "content": append_tag}]
)
elif not isinstance(prepend_tag, str) and isinstance(append_tag, str):
content = message.content + [
{"type": "text", "content": append_tag}
]
elif isinstance(prepend_tag, str) and not isinstance(append_tag, str):
content = [
{"type": "text", "content": prepend_tag}
] + message.content
else:
content = {message.content}

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