Skip to content

Commit

Permalink
fix: inconsistency behavior parsing chat markup
Browse files Browse the repository at this point in the history
feat: add more unit tests for chat module
  • Loading branch information
CNSeniorious000 committed Dec 8, 2023
1 parent 082db2e commit c282eb7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/promplate/prompt/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ def parse_chat_markup(text: str) -> list[Message]:
current_message["content"] = "\n".join(buffer)
messages.append(current_message)

return messages or [{"role": "user", "content": text}]
return messages or [{"role": "user", "content": text.removesuffix("\n")}]
16 changes: 16 additions & 0 deletions python/tests/test_chat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pytest import raises

from promplate.prompt.chat import A, S, U, parse_chat_markup


Expand All @@ -16,3 +18,17 @@ def test_builder_names():
def test_chat_markup():
assert parse_chat_markup("hi") == [U > "hi"]
assert parse_chat_markup("<| user name |>") == [U @ "name" > ""]
assert parse_chat_markup("123\n234\n\n345") == [U > "123\n234\n\n345"]
assert parse_chat_markup("<|user|>\n123\n<|assistant|>\n456") == [U > "123", A > "456"]


def test_auto_trim_trailing_blank_line():
assert [U > "123"] == parse_chat_markup("123")
assert [U > "123"] == parse_chat_markup("123\n")
assert [U > "123"] == parse_chat_markup("<|user|>\n123")
assert [U > "123"] == parse_chat_markup("<|user|>\n123\n")


def test_immutable_constants():
with raises(AssertionError):
A.content = "content"

0 comments on commit c282eb7

Please sign in to comment.