ui.chat_ui
ui.chat_ui(id,
*,
=None,
messages='Enter a message...',
placeholder='min(680px, 100%)',
width='auto',
height=True,
fill=None,
icon_assistant**kwargs,
)
UI container for a chat component (Shiny Core).
This function is for locating a Chat
instance in a Shiny Core app. If you are using Shiny Express, use the ui
method instead.
Parameters
id : str
-
A unique identifier for the chat UI.
messages : Optional[Sequence[TagChild |
ChatMessageDict
]] = None-
A sequence of messages to display in the chat. A given message can be one of the following: * A string, which is interpreted as markdown and rendered to HTML on the client. * To prevent interpreting as markdown, mark the string as
HTML
. * A UI element (specifically, aTagChild
). * This includesTagList
, which take UI elements (including strings) as children. In this case, strings are still interpreted as markdown as long as they’re not inside HTML. * A dictionary withcontent
androle
keys. Thecontent
key can contain a content as described above, and therole
key can be “assistant” or “user”. NOTE: content may include specially formatted input suggestion links (seeappend_message
for more info). placeholder : str = 'Enter a message…'
-
Placeholder text for the chat input.
width : CssUnit = 'min(680px, 100%)'
-
The width of the chat container.
height : CssUnit = 'auto'
-
The height of the chat container.
fill : bool = True
-
Whether the chat should vertically take available space inside a fillable container.
icon_assistant :
HTML
| Tag | TagList | None = None-
The icon to use for the assistant chat messages. Can be a HTML or a tag in the form of
HTML
orTag
. IfNone
, a default robot icon is used. kwargs : TagAttrValue = {}
-
Additional attributes for the chat container element.
Examples
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
## file: app.py
from shiny import App, ui
app_ui = ui.page_fillable(
ui.panel_title("Hello Shiny Chat"),
ui.chat_ui("chat"),
fillable_mobile=True,
)
# Create a welcome message
welcome = """
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
simply repeat it back to you.
To learn more about chatbots and how to build them with Shiny, check out
[the documentation](https://shiny.posit.co/py/docs/genai-chatbots.html).
"""
def server(input, output, session):
chat = ui.Chat(id="chat", messages=[welcome])
# Define a callback to run when the user submits a message
@chat.on_user_submit
async def handle_user_input(user_input: str):
# Append a response to the chat
await chat.append_message(f"You said: {user_input}")
app = App(app_ui, server)
## file: requirements.txt
shiny
## file: _template.json
{
"type": "app",
"id": "chat-hello",
"title": "Hello Shiny Chat",
"next_steps": [
"Run the app with `shiny run app.py`."
]
}