Skip to content

Commit

Permalink
slightly improvements in behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang11 committed Apr 15, 2024
1 parent 2d02b5d commit 9b4f8ed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def _replace_placeholder(self, message: ChatMessage | None = None) -> None:

if index is not None:
if message is not None:
self[index] = message
self._chat_log.objects[index] = message
elif message is None:
self.remove(self._placeholder)
elif message is not None:
Expand Down Expand Up @@ -699,7 +699,7 @@ def undo(self, count: int = 1) -> List[Any]:
return []
messages = self._chat_log.objects
undone_entries = messages[-count:]
self[:] = messages[:-count]
self._chat_log.objects[:] = messages[:-count]
return undone_entries

def clear(self) -> List[Any]:
Expand Down Expand Up @@ -812,9 +812,11 @@ def serialize(
exclude_users = ["help"]
else:
exclude_users = [user.lower() for user in exclude_users]

messages = [
message for message in self._chat_log.objects
if message.user.lower() not in exclude_users
and message is not self._placeholder
]

if filter_by is not None:
Expand Down
30 changes: 30 additions & 0 deletions panel/tests/chat/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,21 @@ async def callback(msg, user, instance):
time.sleep(1)
assert chat_feed.objects[-1].object == "A"

def test_callback_short_time(self, chat_feed):
def callback(contents, user, instance):
time.sleep(1)
message = None
string = ""
for c in "helloooo":
string += c
time.sleep(0.001)
message = instance.stream(string, message=message, replace=True)

feed = ChatFeed(callback=callback)
feed.send("Message", respond=True)
assert feed.objects[-1].object == "helloooo"
assert chat_feed._placeholder not in chat_feed._chat_log


@pytest.mark.xdist_group("chat")
class TestChatFeedSerializeForTransformers:
Expand Down Expand Up @@ -947,6 +962,21 @@ def say_hi(contents, user, instance):
{"role": "user", "content": "Hello there!"},
]

def test_serialize_exclude_placeholder(self):
def say_hi(contents, user, instance):
assert len(instance.serialize()) == 1
return f"Hi {user}!"

chat_feed = ChatFeed(
help_text="This chat feed will respond by saying hi!",
callback=say_hi
)

chat_feed.send("Hello there!")
assert chat_feed.serialize() == [
{"role": "user", "content": "Hello there!"},
{"role": "assistant", "content": "Hi User!"}
]

@pytest.mark.xdist_group("chat")
class TestChatFeedSerializeBase:
Expand Down

0 comments on commit 9b4f8ed

Please sign in to comment.