-
Notifications
You must be signed in to change notification settings - Fork 12
feat: Add chat_enable_bookmarking(id, client, ..., update_on_input = TRUE, update_on_response = TRUE)
#28
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
base: main
Are you sure you want to change the base?
Conversation
…om server. Remove debug statements Update chat.R
…session state instead!
Talking with This change below ( library(shiny)
library(bslib)
library(shinychat)
ui <- function(...) {
page_fillable(
chat_ui("chat", fill = TRUE)
)
}
server <- function(input, output, session) {
chat_client <- ellmer::chat_ollama(
system_prompt = "Important: Always respond in a limerick",
model = "qwen2.5-coder:1.5b",
echo = TRUE
)
# Let the UI know that shinychat should bookmark the chat_client
shinychat::chat_bookmark("chat", chat_client) # update_on_input = TRUE, update_on_response = TRUE
observeEvent(input$chat_user_input, {
stream_promise <- chat_client$stream_async(input$chat_user_input)
shinychat::chat_append("chat", stream_promise)
})
}
# Enable bookmarking!
shinyApp(ui, server, enableBookmarking = "server") If other features were to be made, they should have their own |
set_chat_model(id, model, ..., bookmark = TRUE)
chat_bookmark(id, client, ..., update_on_input = TRUE, update_on_response = TRUE)
chat_bookmark(id, client, ..., update_on_input = TRUE, update_on_response = TRUE)
chat_bookmark(id, client)
chat_bookmark(id, client)
chat_bookmark(id, client, ..., update_on_input = TRUE, update_on_response = TRUE)
R/chat_bookmark.R
Outdated
# TODO: Q - I feel this should be removed. Since we are only adding hooks, it doesn't matter if it's enabled or not. If the user diables chat, it would be very annoying to receive error messages for code they may not own. | ||
if (bookmark_store == "disable") { | ||
rlang::abort( | ||
paste0( | ||
"Error: Shiny bookmarking is not enabled. ", | ||
"Please enable bookmarking in your Shiny app either by calling ", | ||
"`shiny::enableBookmarking(\"server\")` or by setting the parameter in ", | ||
"`shiny::shinyApp(enableBookmarking = \"server\")`" | ||
) | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remaining question
@wch Looking for a review on the API: These default parameters hide away the multiple calls to Ex equivalent server code: shinychat::chat_bookmark("chat", chat_client, update_on_input = FALSE, update_on_response = FALSE)
observeEvent(input$chat_user_input, {
session$doBookmark()
stream_promise <- chat_client$stream_async(input$chat_user_input)
stream_promise$then(function(stream_value) { session$doBookmark() })
shinychat::chat_append("chat", stream_promise)
}) Final server code: shinychat::chat_bookmark("chat", chat_client)
observeEvent(input$chat_user_input, {
stream_promise <- chat_client$stream_async(input$chat_user_input)
shinychat::chat_append("chat", stream_promise)
}) |
The new API does look better. A few questions:
|
✅ |
If the chat is done within a single chat object, no. The data storage required is more complex than what a single ellmer object wants. If the chat is done within a module where given the sidebar's chat selected id, we could have a dynamic chat module that loads a chat given the chat ID. I believe this is a plausible approach |
chat_bookmark(id, client, ..., update_on_input = TRUE, update_on_response = TRUE)
chat_enable_bookmarking(id, client, ..., update_on_input = TRUE, update_on_response = TRUE)
R/chat_enable_bookmark.R
Outdated
}) | ||
state$values[[id]] <- turns_list | ||
} | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to save the UI here
R/chat_enable_bookmark.R
Outdated
# Set the UI | ||
# TODO-barret-future; In shinychat, make this a single/internal custom message call to send all the messages at once (and then scroll) | ||
lapply(client$get_turns(), function(turn) { | ||
chat_append( | ||
id, | ||
# Use `contents_markdown()` as it handles image serialization | ||
ellmer::contents_markdown(turn), | ||
# turn_info$contents, | ||
role = turn@role | ||
) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to independently restore the UI here (not directly from ellmer)
R/chat_enable_bookmark.R
Outdated
# Use `contents_markdown()` as it handles image serialization | ||
ellmer::contents_markdown(turn), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should ideally be a {s7}
JSON "pack" method, similar to jsonlite:::pack()
Fixes #27
Reprex app: