For advanced users who want to control the message chunking behavior. Most
users should use chat_append() instead.
Usage
chat_append_message(
id,
msg,
chunk = TRUE,
operation = c("append", "replace"),
icon = NULL,
session = getDefaultReactiveDomain()
)Arguments
- id
The ID of the chat element
- msg
The message to append. Should be a named list with
roleandcontentfields. Therolefield should be either "user" or "assistant". Thecontentfield should be a string containing the message content, in Markdown format.- chunk
Whether
msgis just a chunk of a message, and if so, what type. IfFALSE, thenmsgis a complete message. If"start", thenmsgis the first chunk of a multi-chunk message. If"end", thenmsgis the last chunk of a multi-chunk message. IfTRUE, thenmsgis an intermediate chunk of a multi-chunk message. Default isFALSE.- operation
The operation to perform on the message. If
"append", then the new content is appended to the existing message content. If"replace", then the existing message content is replaced by the new content. Ignored ifchunkisFALSE.- icon
An optional icon to display next to the message, currently only used for assistant messages. The icon can be any HTML element (e.g.,
htmltools::img()tag) or a string of HTML.- session
The Shiny session object
Examples
if (FALSE) { # interactive()
library(shiny)
library(coro)
library(bslib)
library(shinychat)
# Dumbest chatbot in the world: ignores user input and chooses
# a random, vague response.
fake_chatbot <- async_generator(function(id, input) {
responses <- c(
"What does that suggest to you?",
"I see.",
"I'm not sure I understand you fully.",
"What do you think?",
"Can you elaborate on that?",
"Interesting question! Let's examine thi... **See more**"
)
# Use low-level chat_append_message() to temporarily set a progress message
chat_append_message(id, list(role = "assistant", content = "_Thinking..._ "))
await(async_sleep(1))
# Clear the progress message
chat_append_message(id, list(role = "assistant", content = ""), operation = "replace")
for (chunk in strsplit(sample(responses, 1), "")[[1]]) {
yield(chunk)
await(async_sleep(0.02))
}
})
ui <- page_fillable(
chat_ui("chat", fill = TRUE)
)
server <- function(input, output, session) {
observeEvent(input$chat_user_input, {
response <- fake_chatbot("chat", input$chat_user_input)
chat_append("chat", response)
})
}
shinyApp(ui, server)
}