Skip to content

Commit

Permalink
Merge pull request #207 from MichelNivard/202-fr-set-save-chat-conver…
Browse files Browse the repository at this point in the history
…sation-option-in-settings

Chat history management
  • Loading branch information
JamesHWade authored May 14, 2024
2 parents 9169c62 + 6eb5b02 commit 44ee5d7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
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)
}

0 comments on commit 44ee5d7

Please sign in to comment.