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

Don't pop ChatInterface variables #6290

Merged
merged 5 commits into from
Feb 1, 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
4 changes: 2 additions & 2 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ def __init__(self, *objects, **params):

# forward message params to ChatMessage for convenience
message_params = params.get("message_params", {})
for param_key in list(params.keys()):
if param_key not in ChatFeed.param and param_key in ChatMessage.param:
for param_key in params.copy():
philippjfr marked this conversation as resolved.
Show resolved Hide resolved
if param_key not in self.param and param_key in ChatMessage.param:
message_params[param_key] = params.pop(param_key)
params["message_params"] = message_params

Expand Down
75 changes: 75 additions & 0 deletions panel/chat/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,78 @@ async def _cleanup_response(self):
"""
await super()._cleanup_response()
await self._update_input_disabled()


def send(
self,
value: ChatMessage | dict | Any,
user: str | None = None,
avatar: str | bytes | BytesIO | None = None,
respond: bool = True,
) -> ChatMessage | None:
"""
Sends a value and creates a new message in the chat log.

If `respond` is `True`, additionally executes the callback, if provided.

Arguments
---------
value : ChatMessage | dict | Any
The message contents to send.
user : str | None
The user to send as; overrides the message message's user if provided.
Will default to the user parameter.
avatar : str | bytes | BytesIO | None
The avatar to use; overrides the message message's avatar if provided.
Will default to the avatar parameter.
respond : bool
Whether to execute the callback.

Returns
-------
The message that was created.
"""
if not isinstance(value, ChatMessage):
if user is None:
user = self.user
if avatar is None:
avatar = self.avatar
return super().send(value, user=user, avatar=avatar, respond=respond)

def stream(
self,
value: str,
user: str | None = None,
avatar: str | bytes | BytesIO | None = None,
message: ChatMessage | None = None,
replace: bool = False,
) -> ChatMessage | None:
"""
Streams a token and updates the provided message, if provided.
Otherwise creates a new message in the chat log, so be sure the
returned message is passed back into the method, e.g.
`message = chat.stream(token, message=message)`.

This method is primarily for outputs that are not generators--
notably LangChain. For most cases, use the send method instead.

Arguments
---------
value : str | dict | ChatMessage
The new token value to stream.
user : str | None
The user to stream as; overrides the message's user if provided.
Will default to the user parameter.
avatar : str | bytes | BytesIO | None
The avatar to use; overrides the message's avatar if provided.
Will default to the avatar parameter.
message : ChatMessage | None
The message to update.
replace : bool
Whether to replace the existing text when streaming a string or dict.

Returns
-------
The message that was updated.
"""
return super().stream(value, user=user or self.user, avatar=avatar or self.avatar, message=message, replace=replace)
6 changes: 6 additions & 0 deletions panel/tests/chat/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ def post_callback(instance, event):
assert chat_interface.objects[1].object == "2"
assert chat_interface.objects[2].object == "3"

def test_manual_user(self):
chat_interface = ChatInterface(user="New User")
assert chat_interface.user == "New User"
hoxbro marked this conversation as resolved.
Show resolved Hide resolved
chat_interface.send("Test")
assert chat_interface.objects[0].user == "New User"

class TestChatInterfaceWidgetsSizingMode:
def test_none(self):
chat_interface = ChatInterface()
Expand Down
Loading