Skip to content

Commit

Permalink
Remove .ui() method from Shiny Core's Chat component (#1840)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert authored Feb 11, 2025
1 parent 3d1108c commit 05dc9f5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 46 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)

## [1.2.1] - 2024-11-14

### Bug fixes
Expand Down
3 changes: 2 additions & 1 deletion shiny/express/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
AccordionPanel,
AnimationOptions,
CardItem,
Chat,
ShowcaseLayout,
Sidebar,
SliderStepArg,
Expand Down Expand Up @@ -146,6 +145,8 @@
popover,
)

from ...ui._chat import ChatExpress as Chat

from ...ui._markdown_stream import (
ExpressMarkdownStream as MarkdownStream,
)
Expand Down
92 changes: 47 additions & 45 deletions shiny/ui/_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

__all__ = (
"Chat",
"ChatExpress",
"chat_ui",
"ChatMessage",
)
Expand Down Expand Up @@ -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_: ...

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 05dc9f5

Please sign in to comment.