Chat
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [viewer]
#| viewerHeight: 350
from shiny.express import ui
# Set some Shiny page options
ui.page_opts(
title="Hello Shiny Chat",
fillable=True,
fillable_mobile=True,
)
# Create a chat instance and display it
chat = ui.Chat(id="chat")
chat.ui()
# 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}")
from shiny.express import ui
ui.page_opts(
title="Hello Shiny Chat",
fillable=True,
fillable_mobile=True,
)
# Create a chat instance and display it
chat = ui.Chat(id="chat")
chat.ui()
# Define a callback to run when the user submits a message
@chat.on_user_submit
async def handle_user_input(user_input: str):
# Simply echo the user's input back to them
await chat.append_message(f"You said: {user_input}")
from shiny import App, ui
app_ui = ui.page_fillable(
ui.panel_title("Hello Shiny Chat"),
ui.chat_ui("chat"),
fillable_mobile=True,
)
def server(input):
# Create a chat instance and display it
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):
# Simply echo the user's input back to them
await chat.append_message(f"You said: {user_input}")
app = App(app_ui, server)
The example above is a bare-bones chat interface where the “assistant” simply echoes back the user’s input. Visit the article on chatbots to learn how to get Chat()
working with an LLM provider of your choice. That article also covers features of the Chat()
component in more detail.
Relevant Functions
-
chat = ui.Chat()
ui.Chat(id, on_error="auto")
-
chat.ui()
chat.ui(placeholder="Enter a message...", width="min(680px, 100%)", height="auto", fill=True)
-
@chat.on_user_submit
chat.on_user_submit(fn)
-
chat.append_message_stream()
chat.append_message_stream(message)
-
chat.append_message()
chat.append_message(message)