From de7e75e590fa9740ddc968df63f29ce6a88569af Mon Sep 17 00:00:00 2001 From: Carson Date: Tue, 4 Feb 2025 15:04:39 -0600 Subject: [PATCH 1/3] Remove .ui() method from Shiny Core's Chat component --- shiny/express/ui/__init__.py | 3 +- shiny/ui/_chat.py | 92 ++++++++++++++++++------------------ 2 files changed, 49 insertions(+), 46 deletions(-) diff --git a/shiny/express/ui/__init__.py b/shiny/express/ui/__init__.py index 5a71d797f..6d9a2d47f 100644 --- a/shiny/express/ui/__init__.py +++ b/shiny/express/ui/__init__.py @@ -41,7 +41,6 @@ AccordionPanel, AnimationOptions, CardItem, - Chat, ShowcaseLayout, Sidebar, SliderStepArg, @@ -146,6 +145,8 @@ popover, ) +from ...ui._chat import ChatExpress as Chat + from ...ui._markdown_stream import ( ExpressMarkdownStream as MarkdownStream, ) diff --git a/shiny/ui/_chat.py b/shiny/ui/_chat.py index 9b3488152..fb9aaadaf 100644 --- a/shiny/ui/_chat.py +++ b/shiny/ui/_chat.py @@ -44,6 +44,7 @@ __all__ = ( "Chat", + "ChatExpress", "chat_ui", "ChatMessage", ) @@ -246,51 +247,6 @@ async def _on_user_input(): instance.destroy() CHAT_INSTANCES[instance_id] = self - def ui( - self, - *, - messages: Optional[Sequence[str | ChatMessage]] = None, - placeholder: str = "Enter a message...", - width: CssUnit = "min(680px, 100%)", - height: CssUnit = "auto", - fill: bool = True, - **kwargs: TagAttrValue, - ) -> Tag: - """ - Place a chat component in the UI. - - This method is only relevant fpr Shiny Express. In Shiny Core, use - :func:`~shiny.ui.chat_ui` instead to insert the chat UI. - - Parameters - ---------- - messages - A sequence of messages to display in the chat. Each message can be either a - string or a dictionary with `content` and `role` keys. The `content` key - should contain the message text, and the `role` key can be "assistant" or - "user". - placeholder - Placeholder text for the chat input. - width - The width of the chat container. - height - The height of the chat container. - fill - Whether the chat should vertically take available space inside a fillable - container. - kwargs - Additional attributes for the chat container element. - """ - return chat_ui( - id=self.id, - messages=messages, - placeholder=placeholder, - width=width, - height=height, - fill=fill, - **kwargs, - ) - @overload def on_user_submit(self, fn: UserSubmitFunction) -> reactive.Effect_: ... @@ -1048,6 +1004,52 @@ async def _send_custom_message(self, handler: str, obj: ClientMessage | None): ) +@add_example(ex_dir="../templates/chat/starters/hello") +class ChatExpress(Chat): + + def ui( + self, + *, + messages: Optional[Sequence[str | ChatMessage]] = None, + placeholder: str = "Enter a message...", + width: CssUnit = "min(680px, 100%)", + height: CssUnit = "auto", + fill: bool = True, + **kwargs: TagAttrValue, + ) -> Tag: + """ + Create a UI element for this `Chat`. + + Parameters + ---------- + messages + A sequence of messages to display in the chat. Each message can be either a + string or a dictionary with `content` and `role` keys. The `content` key + should contain the message text, and the `role` key can be "assistant" or + "user". + placeholder + Placeholder text for the chat input. + width + The width of the UI element. + height + The height of the UI element. + fill + Whether the chat should vertically take available space inside a fillable + container. + kwargs + Additional attributes for the chat container element. + """ + return chat_ui( + id=self.id, + messages=messages, + placeholder=placeholder, + width=width, + height=height, + fill=fill, + **kwargs, + ) + + @add_example(ex_dir="../templates/chat/starters/hello") def chat_ui( id: str, From edd0a4e4f6c1b0b3c0ed4f1b3292b071b80d013c Mon Sep 17 00:00:00 2001 From: Carson Date: Tue, 4 Feb 2025 15:05:34 -0600 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37a8cd616..8f823d0f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * `ui.Chat()` now correctly handles new `ollama.chat()` return value introduced in `ollama` v0.4. (#1787) +### Changes + +* The `shiny.ui.Chat()` class no longer has a `.ui()` method. This method +was never intended to be used in Shiny Core (in that case, use `shiny.ui.chat_ui()`) to create the UI element. Note that the `shiny.express.ui.Chat()` +class still has a `.ui()` method. (#1840) + ## [1.2.1] - 2024-11-14 ### Bug fixes From 113f4115e2baf992c6c6762c30ecf49061d4ccb4 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Mon, 10 Feb 2025 11:10:37 -0600 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f823d0f3..8bad9f3a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changes -* The `shiny.ui.Chat()` class no longer has a `.ui()` method. This method +* The Shiny Core component `shiny.ui.Chat()` no longer has a `.ui()` method. This method was never intended to be used in Shiny Core (in that case, use `shiny.ui.chat_ui()`) to create the UI element. Note that the `shiny.express.ui.Chat()` class still has a `.ui()` method. (#1840)