ui.chat_ui

ui.chat_ui(
    id,
    *,
    messages=None,
    greeting=None,
    placeholder='Enter a message...',
    width='min(680px, 100%)',
    height='auto',
    fill=True,
    icon_assistant=None,
    enable_cancel=False,
    footer=None,
    **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 :

A unique identifier for the chat UI.

messages :

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, a TagChild). * This includes TagList, 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 with content and role keys. The content key can contain a content as described above, and the role key can be “assistant” or “user”. * More generally, any type registered with shinychat.message_content. NOTE: content may include specially formatted input suggestion links (see append_message for more info).

greeting :

An optional greeting to display at the top of the chat before any conversation messages. Can be a markdown string or a chat_greeting object. For a dynamic or streaming greeting, use set_greeting from the server instead. When no greeting is set and the chat is visible with no messages, an input named {id}_greeting_requested fires. Use this input with @reactive.event(input.{id}_greeting_requested) to generate a greeting on demand from the server. It fires again after clear_messages is called with greeting=True.

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.

icon_assistant :

The icon to use for the assistant chat messages. Can be a HTML or a tag in the form of HTML or Tag. If None, a default robot icon is used.

enable_cancel :

Whether to show a stop button during streaming that allows the user to cancel the in-progress response. When True, the chat UI shows a stop button in place of the send button while streaming. You must observe input.<id>_cancel on the server and call ctrl.cancel() on a chatlas StreamController to actually stop the stream. Defaults to False.

footer :

Optional HTML content to display below the chat input. This can be any HTML content (tags, tag lists, or strings). Useful for adding disclaimers, attribution, or other information. The footer text is styled slightly smaller and lighter than body text by default. Customize with CSS properties --shiny-chat-footer-font-size and --shiny-chat-footer-color on the chat container or footer element.

kwargs :

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"),
    ui.chat_ui(
        "chat",
        messages=["""
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).
"""],
    ),
    fillable_mobile=True,
)


def server(input, output, session):
    chat = ui.Chat(id="chat")

    # 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`."
  ]
}