Chatbot User Interfaces (Shiny)

Shiny (R/Python)

Demo: shinychat Python

from shiny.express import render, ui
from shinychat.express import Chat

# Set some Shiny page options
ui.page_opts(title="Hello Chat")

# Create a chat instance, with an initial message
chat = Chat(
    id="chat",
    messages=[
        {"content": "Hello! How can I help you today?", "role": "assistant"},
    ],
)

# Display the 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):
    await chat.append_message(f"You said: {user_input}")

"Message state:"

@render.code
def message_state():
    return str(chat.messages())

Chatting with your data

Demo: querychat Python

import querychat
from chatlas import ChatAnthropic
from seaborn import load_dataset
from shiny.express import render

# data -----
titanic = load_dataset("titanic")

# chatbot setup -----
def create_chat_callback(system_prompt):
    return ChatAnthropic(system_prompt=system_prompt)


querychat_config = querychat.init(
    titanic,
    "titanic",
    greeting="""Hello! I'm here to help you explore the Titanic dataset.""",
    create_chat_callback=create_chat_callback,
)

chat = querychat.server("chat", querychat_config)

# shiny application -----

# querychat UI
querychat.sidebar("chat")

# querychat filtered dataframe
@render.data_frame
def data_table():
    return chat["df"]()

Your turn: Change querrychat LLM

  • Modify one of the querychat examples and swap it with another model
  • Try using one of the local Ollama models and compare with your neighbor
10:00

Sidebot demo

Python template example code:

shiny create --mode core --github jcheng5/py-databot