express.ui.Chat

express.ui.Chat(self, id, *, messages=(), on_error='auto', tokenizer=None)

Methods

Name Description
enable_bookmarking Enable bookmarking for the chat instance.
ui Create a UI element for this Chat.

enable_bookmarking

express.ui.Chat.enable_bookmarking(
    client,
    /,
    *,
    bookmark_store=None,
    bookmark_on='response',
)

Enable bookmarking for the chat instance.

This method registers on_bookmark and on_restore hooks on session.bookmark (shiny.bookmark.Bookmark) to save/restore chat state on both the Chat and client= instances. In order for this method to actually work correctly, a bookmark_store= must be specified in shiny.express.app_opts().

Parameters

client : ClientWithState | chatlas.Chat[Any, Any]

The chat client instance to use for bookmarking. This can be a Chat model provider from chatlas, or more generally, an instance following the ClientWithState protocol.

bookmark_store : Optional[BookmarkStore] = None

A convenience parameter to set the shiny.express.app_opts(bookmark_store=) which is required for bookmarking (and .enable_bookmarking()). If None, no value will be set.

bookmark_on : Optional[Literal[‘response’]] = 'response'

The event to trigger the bookmarking on. Supported values include: - "response" (the default): a bookmark is triggered when the assistant is done responding. - None: no bookmark is triggered When this method triggers a bookmark, it also updates the URL query string to reflect the bookmarked state.

Raises

: ValueError

If the Shiny App does have bookmarking enabled.

Returns

: CancelCallback

A callback to cancel the bookmarking hooks.

ui

express.ui.Chat.ui(
    messages=None,
    placeholder='Enter a message...',
    width='min(680px, 100%)',
    height='auto',
    fill=True,
    icon_assistant=None,
    **kwargs,
)

Create a UI element for this Chat.

Parameters

messages : Optional[Sequence[str | ChatMessageDict]] = None

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 : str = 'Enter a message…'

Placeholder text for the chat input.

width : CssUnit = 'min(680px, 100%)'

The width of the UI element.

height : CssUnit = 'auto'

The height of the UI element.

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 or Tag. If None, a default robot icon is used.

kwargs : TagAttrValue = {}

Additional attributes for the chat container element.