Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add messages parameter to chat_ui() #8

Merged
merged 5 commits into from
Oct 16, 2024
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ License: MIT + file LICENSE
URL: https://github.com/jcheng5/shinychat, https://jcheng5.github.io/shinychat/
BugReports: https://github.com/jcheng5/shinychat/issues
Imports:
rlang,
htmltools,
bslib,
shiny,
Expand Down
51 changes: 43 additions & 8 deletions R/chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,75 @@ chat_deps <- function() {
utils::packageVersion("shinychat"),
package = "shinychat",
src = "chat",
script = "chat.js",
script = list(src = "chat.js", type = "module"),
stylesheet = "chat.css"
)
}

#' Create a chat UI element
#'
#' @param id The ID of the chat element
#' @param ... Extra HTML attributes to include on the chat element
#' @param messages A list of messages to prepopulate the chat with. Each
#' message can be a string or a named list with `content` and `role` fields.
#' @param placeholder The placeholder text for the chat's user input field
#' @param width The CSS width of the chat element
#' @param height The CSS height of the chat element
#' @param fill Whether the chat element should try to vertically fill its
#' container, if the container is
#' [fillable](https://rstudio.github.io/bslib/articles/filling/index.html)
#' @param ... Extra HTML attributes to include on the chat element
#' @returns A Shiny tag object, suitable for inclusion in a Shiny UI
#'
#' @export
chat_ui <- function(
id,
...,
messages = NULL,
placeholder = "Enter a message...",
width = "min(680px, 100%)",
height = "auto",
fill = TRUE,
...) {
res <- tag("shiny-chat-container", list(
fill = TRUE) {

attrs <- rlang::list2(...)
if (!all(nzchar(rlang::names2(attrs)))) {
stop("All arguments in ... must be named.")
}

message_tags <- lapply(messages, function(x) {
if (is.character(x)) {
x <- list(content = x, role = "assistant")
} else if (is.list(x)) {
if (!("content" %in% names(x))) {
stop("Each message must have a 'content' key.")
}
if (!("role" %in% names(x))) {
stop("Each message must have a 'role' key.")
}
} else {
stop("Each message must be a string or a named list.")
}

if (isTRUE(x[["role"]] == "user")) {
tag_name <- "shiny-user-message"
} else {
tag_name <- "shiny-chat-message"
}

tag(tag_name, list(content = x[["content"]]))
})

res <- tag("shiny-chat-container", rlang::list2(
id = id,
style = css(
width = width,
height = height
),
placeholder = placeholder,
fill = if (isTRUE(fill)) NA else NULL,
chat_deps(),
...
...,
tag("shiny-chat-messages", message_tags),
tag("shiny-chat-input", list(id=paste0(id, "_user_input"), placeholder=placeholder)),
chat_deps()
))

if (isTRUE(fill)) {
Expand Down Expand Up @@ -104,7 +138,8 @@ chat_append <- function(id, response, session = getDefaultReactiveDomain()) {
#' @importFrom shiny getDefaultReactiveDomain
#' @export
chat_append_message <- function(id, msg, chunk = FALSE, operation = NULL, session = getDefaultReactiveDomain()) {
if (identical(msg[["role"]], "system")) {
if (!isTRUE(msg[["role"]] %in% c("user", "assistant"))) {
warning("Invalid role argument; must be 'user' or 'assistant'")
return()
}

Expand Down
2 changes: 1 addition & 1 deletion inst/chat/GIT_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1b871931201f3ac864b86c25f49d32c962508538
70c6c99661b6b2d9e015d30e31e88bac7cbcd780
8 changes: 1 addition & 7 deletions inst/chat/chat.js

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

6 changes: 3 additions & 3 deletions inst/chat/chat.js.map

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions man/chat_ui.Rd

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

Loading