Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 68 additions & 30 deletions pkg-r/R/chat_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@
#' new user input. Takes the same arguments as [update_chat_user_input()],
#' except for `id` and `session`, which are supplied automatically.
#' * `clear()`: A function to clear the chat history and the chat UI.
#' Takes a single argument, `clear_history`, which indicates whether to
#' also clear the chat history in the `client` object. Defaults to `TRUE`.
#' `clear()` takes an optional list of `messages` used to initialize the
#' chat after clearing. `messages` should be a list of messages, where
#' each message is a list with `role` and `content` fields. The
#' `client_history` argument controls how the chat client's history is
#' updated after clearing. It can be one of: `"clear"` the chat history;
#' `"set"` the chat history to `messages`; `"append"` `messages` to the
#' existing chat history; or `"keep"` the existing chat history.
#' * `client`: The chat client object, which is mutated as you chat.
#'
#' @describeIn chat_app A simple Shiny app for live chatting. Note that this
Expand Down Expand Up @@ -201,40 +206,73 @@ chat_mod_server <- function(
}
})

list(
last_turn = shiny::reactive(last_turn()),
last_input = shiny::reactive(last_input()),
client = client,
update_user_input = function(
value = NULL,
chat_update_user_input <- function(
value = NULL,
...,
placeholder = NULL,
submit = FALSE,
focus = FALSE
) {
update_chat_user_input(
"chat",
value = value,
placeholder = placeholder,
submit = submit,
focus = focus,
...,
placeholder = NULL,
submit = FALSE,
focus = FALSE
) {
update_chat_user_input(
"chat",
value = value,
placeholder = placeholder,
submit = submit,
focus = focus,
...,
session = session
)
},
clear = function(clear_history = TRUE) {
if (!rlang::is_bool(clear_history)) {
session = session
)
}

client_clear <- function(
messages = NULL,
client_history = c("clear", "set", "append", "keep")
) {
client_history <- arg_match(client_history)

if (!is.null(messages)) {
if (rlang::is_string(messages)) {
# Promote strings to single assistant message
messages <- list(list(role = "assistant", content = messages))
}
if (!rlang::is_list(messages)) {
cli::cli_abort(
"{.var clear_history} must be {.or {.val {c(TRUE, FALSE)}}}."
"{.var messages} must be a list of messages, and each message must be a list with {.field role} and {.field content}."
)
}
chat_clear("chat", session = session)
if (isTRUE(clear_history)) {
client$set_turns(list())
if (length(intersect(c("role", "content"), names(messages))) == 2) {
# Catch the single-message case and promote it to a list of messages
messages <- list(messages)
}
}

chat_clear("chat", session = session)
if (!is.null(messages)) {
for (msg in messages) {
chat_append("chat", msg$content, role = msg$role, session = session)
}
last_turn(NULL)
last_input(NULL)
}

if (client_history == "clear") {
client$set_turns(list())
} else if (client_history == "set") {
client$set_turns(as_ellmer_turns(messages))
} else if (client_history == "append") {
turns <- client$get_turns()
turns <- c(turns, as_ellmer_turns(messages))
client$set_turns(turns)
}

last_turn(NULL)
last_input(NULL)
}

list(
last_turn = shiny::reactive(last_turn()),
last_input = shiny::reactive(last_input()),
client = client,
update_user_input = chat_update_user_input,
clear = client_clear
)
})
}
12 changes: 12 additions & 0 deletions pkg-r/R/utils-ellmer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
as_ellmer_turns <- function(messages) {
if (is.null(messages) || length(messages) == 0) {
return(list())
}

map(messages, function(msg) {
ellmer::Turn(
role = msg$role,
contents = list(ellmer::ContentText(msg$content))
)
})
}
9 changes: 7 additions & 2 deletions pkg-r/man/chat_app.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.