Skip to content

Commit

Permalink
Invalid format (#5180)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 authored Jul 18, 2023
1 parent 64f8e62 commit d84d043
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
24 changes: 24 additions & 0 deletions panel/tests/widgets/test_chatbox.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from panel.depends import bind
from panel.layout import Column
from panel.pane import JPG
Expand All @@ -21,6 +23,28 @@ def test_chat_box(document, comm):
assert rows[0]._bubble[0].object == "Hello"


def test_chat_box_role_content_format(document, comm):
value = [
{"role": "user1", "content": "Hello"},
]
chat_box = ChatBox(value=value.copy())
assert chat_box.value == value
assert len(chat_box) == 1
rows = chat_box.rows
assert all(isinstance(row, ChatRow) for row in rows)
assert len(rows) == 1
assert rows[0].value == ["Hello"]
assert rows[0]._bubble[0].object == "Hello"


def test_chat_box_invalid_format(document, comm):
value = [
{"a_user": "user1", "message": "Hello"},
]
with pytest.raises(ValueError, match="Expected a dictionary with one key-value"):
ChatBox(value=value.copy())


def test_chat_box_list(document, comm):
value = [
{"user2": ["Hi", "Greetings", "Salutations"]},
Expand Down
16 changes: 11 additions & 5 deletions panel/widgets/chatbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,20 @@ def _separate_user_message(
"""
Separate the user and message from a dictionary.
"""
if len(user_message) != 1:
if len(user_message) == 1:
user = self._get_name(user_message)
message_contents = user_message[user]
elif "role" in user_message and "content" in user_message:
user = user_message["role"]
message_contents = user_message["content"]
else:
raise ValueError(
f"Expected a dictionary with one key-value pair, e.g. "
f"{{'User': 'Message'}} , but got {user_message}"
f"{{'User': 'Message'}} or two key-value pairs with "
f"'role' and 'content' as keys, e.g. "
f"{{'role': 'User', 'content': 'Message'}}, "
f"but got {user_message}"
)

user = self._get_name(user_message)
message_contents = user_message[user]
return user, message_contents

def _instantiate_message_row(
Expand Down

0 comments on commit d84d043

Please sign in to comment.