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

Do not include placeholder in serialize and properly replace placeholder for streams #6734

Merged
merged 4 commits into from
Apr 17, 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
20 changes: 8 additions & 12 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,15 @@ def _replace_placeholder(self, message: ChatMessage | None = None) -> None:
if placeholder, otherwise simply append the message.
Replacing helps lessen the chat log jumping around.
"""
index = None
if self.placeholder_threshold > 0:
with param.parameterized.batch_call_watchers(self):
if message is not None:
self.append(message)

try:
index = self.index(self._placeholder)
self.remove(self._placeholder)
except ValueError:
pass

if index is not None:
if message is not None:
self[index] = message
elif message is None:
self.remove(self._placeholder)
elif message is not None:
self.append(message)

def _build_message(
self,
value: dict,
Expand Down Expand Up @@ -699,7 +693,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 +806,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
Loading