Skip to content
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

Adds __repr__ to Message #1757

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions tests/torchtune/data/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ def test_text_content(self, text_message, image_message):
assert text_message.text_content == "hello world"
assert image_message.text_content == "hello world"

def test_repr_text(self, text_message):
expected_repr = "Message(role='user', content=['hello world'])"
assert str(text_message) == expected_repr
assert repr(text_message) == expected_repr

def test_repr_image(self, image_message, test_image):
img_repr = str(test_image)
expected_repr = f"Message(role='user', content=['hello', {img_repr}, ' world'])"
assert str(image_message) == expected_repr
assert repr(image_message) == expected_repr


class TestInputOutputToMessages:
@pytest.fixture
Expand Down
4 changes: 4 additions & 0 deletions torchtune/data/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def _validate_message(self) -> None:
f"Only assistant messages can be tool calls. Found role {self.role} in message: {self.text_content}"
)

def __repr__(self) -> str:
content_only = [content["content"] for content in self.content]
return f"Message(role='{self.role}', content={content_only!r})"


class InputOutputToMessages(Transform):
"""
Expand Down
Loading