Skip to contents

Create a simple Shiny app for live chatting using an ellmer::Chat object. Note that these functions will mutate the input client object as you chat because your turns will be appended to the history.

Usage

chat_app(client, ...)

chat_mod_ui(id, ..., client = NULL, messages = NULL)

chat_mod_server(id, client)

Arguments

client

A chat object created by ellmer, e.g. ellmer::chat_openai() and friends.

...

In chat_app(), additional arguments are passed to shiny::shinyApp(). In chat_mod_ui(), additional arguments are passed to chat_ui().

id

The chat module ID.

messages

Initial messages shown in the chat, used when client is not provided or when the chat client doesn't already contain turns. Passed to messages in chat_ui().

Value

  • chat_app() returns a shiny::shinyApp() object.

  • chat_mod_ui() returns the UI for a shinychat module.

  • chat_mod_server() includes the shinychat module server logic, and and returns the last turn upon successful chat completion.

Functions

  • chat_app(): A simple Shiny app for live chatting.

  • chat_mod_ui(): A simple chat app module UI.

  • chat_mod_server(): A simple chat app module server.

Examples

if (FALSE) { # \dontrun{
# Interactive in the console ----
client <- ellmer::chat_claude()
chat_app(client)

# Inside a Shiny app ----
library(shiny)
library(bslib)
library(shinychat)

ui <- page_fillable(
  titlePanel("shinychat example"),

  layout_columns(
    card(
      card_header("Chat with Claude"),
      chat_mod_ui(
        "claude",
        messages = list(
          "Hi! Use this chat interface to chat with Anthropic's `claude-3-5-sonnet`."
        )
      )
    ),
    card(
      card_header("Chat with ChatGPT"),
      chat_mod_ui(
        "openai",
        messages = list(
          "Hi! Use this chat interface to chat with OpenAI's `gpt-4o`."
        )
      )
    )
  )
)

server <- function(input, output, session) {
  claude <- ellmer::chat_claude(model = "claude-3-5-sonnet-latest") # Requires ANTHROPIC_API_KEY
  openai <- ellmer::chat_openai(model = "gpt-4o") # Requires OPENAI_API_KEY

  chat_mod_server("claude", claude)
  chat_mod_server("openai", openai)
}

shinyApp(ui, server)
} # }