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

Propagate serialize_kwargs to ChatMessage #7146

Merged
merged 1 commit into from
Aug 15, 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
5 changes: 3 additions & 2 deletions panel/chat/feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,8 @@ def _serialize_for_transformers(
messages: list[ChatMessage],
role_names: dict[str, str | list[str]] | None = None,
default_role: str | None = "assistant",
custom_serializer: Callable = None
custom_serializer: Callable | None = None,
**serialize_kwargs
) -> list[dict[str, Any]]:
"""
Exports the chat log for use with transformers.
Expand Down Expand Up @@ -914,7 +915,7 @@ def _serialize_for_transformers(
f"it returned a {type(content)} type"
)
else:
content = str(message)
content = message.serialize(**serialize_kwargs)

serialized_messages.append({"role": role, "content": content})
return serialized_messages
Expand Down
8 changes: 6 additions & 2 deletions panel/chat/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ def _serialize_for_transformers(
messages: list[ChatMessage],
role_names: dict[str, str | list[str]] | None = None,
default_role: str | None = "assistant",
custom_serializer: Callable = None
custom_serializer: Callable = None,
**serialize_kwargs
) -> list[dict[str, Any]]:
"""
Exports the chat log for use with transformers.
Expand All @@ -606,6 +607,8 @@ def _serialize_for_transformers(
A custom function to format the ChatMessage's object. The function must
accept one positional argument, the ChatMessage object, and return a string.
If not provided, uses the serialize method on ChatMessage.
serialize_kwargs : dict
Additional keyword arguments to pass to the serializer.

Returns
-------
Expand All @@ -616,7 +619,8 @@ def _serialize_for_transformers(
"user": [self.user],
"assistant": [self.callback_user],
}
return super()._serialize_for_transformers(messages, role_names, default_role, custom_serializer)
return super()._serialize_for_transformers(
messages, role_names, default_role, custom_serializer, **serialize_kwargs)

@param.depends("_callback_state", watch=True)
async def _update_input_disabled(self):
Expand Down
11 changes: 11 additions & 0 deletions panel/tests/chat/test_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,17 @@ def __repr__(self):
chat_feed.send(Test())
assert chat_feed.serialize() == [{"role": "user", "content": "Test()"}]

def test_serialize_kwargs(self, chat_feed):
chat_feed.send("Hello")
chat_feed.add_step("Hello", "World")
assert chat_feed.serialize(
prefix_with_container_label=False,
prefix_with_viewable_label=False
) == [
{'role': 'user', 'content': 'Hello'},
{'role': 'user', 'content': '((Hello))'}
]


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