Skip to content
Open
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
4 changes: 3 additions & 1 deletion fastchat/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,9 @@ def get_prompt(self) -> str:
ret += message + "<n>"
else:
ret += ""
ret = ret.rstrip("<n>") + seps[0]
if ret.endswith("<n>"):
ret = ret[: -len("<n>")]
ret = ret + seps[0]
return ret
elif self.sep_style == SeparatorStyle.GEMMA:
ret = "<bos>"
Expand Down
38 changes: 38 additions & 0 deletions tests/test_conversation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
Usage:
python3 -m unittest tests.test_conversation
"""

import unittest

from fastchat.conversation import get_conv_template


class TestYuan2Template(unittest.TestCase):
def test_message_ending_in_n_is_preserved(self):
"""The YUAN2 template must only strip the trailing ``<n>`` separator,
not characters (``<``, ``n``, ``>``) that belong to the message."""
conv = get_conv_template("yuan2")
conv.append_message(conv.roles[0], "What is the value of n")
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
self.assertIn("What is the value of n", prompt)
self.assertEqual(prompt, "What is the value of n<sep>")

def test_message_ending_in_angle_bracket_is_preserved(self):
conv = get_conv_template("yuan2")
conv.append_message(conv.roles[0], "compare a < b")
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
self.assertIn("compare a < b", prompt)

def test_separator_between_messages_is_kept(self):
conv = get_conv_template("yuan2")
conv.append_message(conv.roles[0], "hello")
conv.append_message(conv.roles[1], "world")
prompt = conv.get_prompt()
self.assertEqual(prompt, "hello<n>world<sep>")


if __name__ == "__main__":
unittest.main()
Loading