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

Chat history management #207

Merged
merged 5 commits into from
May 14, 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
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
### UI updates

- The chat app has now a sidebar where users can see their conversation history, start new chats and change the settings. Because of this, the chat interface has more room for showing messages.
- All saved chats are created with a placeholder title that the user can edit later.
- All chats are saved and automatically updated after every assistant's message. They are created with a placeholder title built using the first user message in the conversation. Titles are editable and users are able to delete any conversation (or all of them at once).
- We have a shorter welcome message, but we have added lots of tooltips to help with navigation.

### Local models
Expand Down
6 changes: 6 additions & 0 deletions R/mod_chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ mod_chat_server <- function(id,

history$chat_history <- response$history

append_to_conversation_history(
id = history$selected_conversation$id %||% ids::random_id(),
title = history$selected_conversation$title %||% find_placeholder_title(history$chat_history), # nolint
messages = history$chat_history
)

if (settings$stream) {
rv$reset_streaming_message <- rv$reset_streaming_message + 1L
}
Expand Down
18 changes: 10 additions & 8 deletions R/mod_history.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ mod_history_server <- function(id, settings) {
rv$chat_history <- list()

conversation_history <- reactive(read_conversation_history()) %>%
bindEvent(rv$reload_conversation_history)
bindEvent(rv$reload_conversation_history, rv$chat_history)

output$conversation_history <- renderUI({
conversation_history() %>%
Expand All @@ -56,18 +56,12 @@ mod_history_server <- function(id, settings) {
bindEvent(input$settings)

observe({
append_to_conversation_history(
id = rv$selected_conversation$id %||% ids::random_id(),
title = rv$selected_conversation$title %||% "Placeholder title",
messages = rv$chat_history
)

rv$chat_history <- list()
rv$selected_conversation <- NULL

rv$reload_conversation_history <- rv$reload_conversation_history + 1L
}) %>%
bindEvent(input$new_chat, settings$create_new_chat)
bindEvent(input$new_chat, settings$create_new_chat, ignoreInit = TRUE)

observe({
conversation_history <- read_conversation_history()
Expand Down Expand Up @@ -274,3 +268,11 @@ conversation <- function(
}

tooltip_on_hover <- purrr::partial(bslib::tooltip, options = list(trigger = "hover"))

# Finds the first user prompt and returns it truncated
find_placeholder_title <- function(chat_history) {
chat_history %>%
purrr::keep(~(!is.null(.x$name)) && .x$name == "user_message") %>%
purrr::pluck(1L, "content") %>%
stringr::str_trunc(40L)
}
Loading