ui.chat_ui

ui.chat_ui(
    id
    *
    messages=None
    placeholder='Enter a message...'
    width='min(680px, 100%)'
    height='auto'
    fill=True
    **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[str | ChatMessage]] = None

A sequence of messages to display in the chat. Each message can be either a string or a dictionary with a content and role key. The content key should contain the message text, and the role key can be “assistant” or “user”.

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.

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 = ui.markdown(
    """
    Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
    simply repeat it back to you. For more examples, see this
    [folder of examples](https://github.com/posit-dev/py-shiny/tree/main/examples/chat).
    """
)


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 _():
        # Get the user's input
        user = chat.user_input()
        # Append a response to the chat
        await chat.append_message(f"You said: {user}")


app = App(app_ui, server)