diff --git a/pkg-r/NEWS.md b/pkg-r/NEWS.md index b37fa724..941e15b1 100644 --- a/pkg-r/NEWS.md +++ b/pkg-r/NEWS.md @@ -4,6 +4,8 @@ * Added `chat_enable_bookmarking()` which adds Shiny bookmarking hooks to save and restore the `{ellmer}` chat client. (#28) +* `chat_app()` now correctly restores the chat client state when refreshing the app, e.g. by reloading the page. (#71) + # shinychat 0.2.0 ## New features and improvements diff --git a/pkg-r/R/chat_app.R b/pkg-r/R/chat_app.R index cc59ee18..08cc9957 100644 --- a/pkg-r/R/chat_app.R +++ b/pkg-r/R/chat_app.R @@ -5,10 +5,15 @@ #' Note that these functions will mutate the input `client` object as #' you chat because your turns will be appended to the history. #' +#' The app created by `chat_app()` is suitable for interactive use by a single +#' user. For multi-user Shiny apps, use the Shiny module chat functions -- +#' `chat_mod_ui()` and `chat_mod_server()` -- and be sure to create a new chat +#' client for each user session. +#' #' @examples #' \dontrun{ #' # Interactive in the console ---- -#' client <- ellmer::chat_claude() +#' client <- ellmer::chat_anthropic() #' chat_app(client) #' #' # Inside a Shiny app ---- @@ -42,7 +47,7 @@ #' ) #' #' server <- function(input, output, session) { -#' claude <- ellmer::chat_claude(model = "claude-3-5-sonnet-latest") # Requires ANTHROPIC_API_KEY +#' claude <- ellmer::chat_anthropic(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) @@ -64,20 +69,24 @@ #' * `chat_mod_server()` includes the shinychat module server logic, and #' and returns the last turn upon successful chat completion. #' -#' @describeIn chat_app A simple Shiny app for live chatting. +#' @describeIn chat_app A simple Shiny app for live chatting. Note that this +#' app is suitable for interactive use by a single user; do not use +#' `chat_app()` in a multi-user Shiny app context. #' @export chat_app <- function(client, ...) { check_ellmer_chat(client) - ui <- bslib::page_fillable( - chat_mod_ui("chat", client = client, height = "100%"), - shiny::actionButton( - "close_btn", - label = "", - class = "btn-close", - style = "position: fixed; top: 6px; right: 6px;" + ui <- function(req) { + bslib::page_fillable( + chat_mod_ui("chat", client = client, height = "100%"), + shiny::actionButton( + "close_btn", + label = "", + class = "btn-close", + style = "position: fixed; top: 6px; right: 6px;" + ) ) - ) + } server <- function(input, output, session) { chat_mod_server("chat", client) diff --git a/pkg-r/man/chat_app.Rd b/pkg-r/man/chat_app.Rd index 7a663038..844e77eb 100644 --- a/pkg-r/man/chat_app.Rd +++ b/pkg-r/man/chat_app.Rd @@ -38,10 +38,17 @@ and returns the last turn upon successful chat completion. Create a simple Shiny app for live chatting using an \link[ellmer:Chat]{ellmer::Chat} object. Note that these functions will mutate the input \code{client} object as you chat because your turns will be appended to the history. + +The app created by \code{chat_app()} is suitable for interactive use by a single +user. For multi-user Shiny apps, use the Shiny module chat functions -- +\code{chat_mod_ui()} and \code{chat_mod_server()} -- and be sure to create a new chat +client for each user session. } \section{Functions}{ \itemize{ -\item \code{chat_app()}: A simple Shiny app for live chatting. +\item \code{chat_app()}: A simple Shiny app for live chatting. Note that this +app is suitable for interactive use by a single user; do not use +\code{chat_app()} in a multi-user Shiny app context. \item \code{chat_mod_ui()}: A simple chat app module UI. @@ -51,7 +58,7 @@ you chat because your turns will be appended to the history. \examples{ \dontrun{ # Interactive in the console ---- -client <- ellmer::chat_claude() +client <- ellmer::chat_anthropic() chat_app(client) # Inside a Shiny app ---- @@ -85,7 +92,7 @@ ui <- page_fillable( ) server <- function(input, output, session) { - claude <- ellmer::chat_claude(model = "claude-3-5-sonnet-latest") # Requires ANTHROPIC_API_KEY + claude <- ellmer::chat_anthropic(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)