Skip to content

Commit

Permalink
format history w/o gptstudio
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Sep 12, 2023
1 parent bbe3542 commit 285ebb8
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions inst/retriever/app.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
rlang::check_installed(
c("shiny", "bsicons", "cli", "glue", "gptstudio", "gpttools", "waiter")
c("shiny", "bslib", "bsicons", "cli", "glue", "gptstudio", "gpttools", "waiter")
)

library(gpttools)
library(gptstudio)

rlang::check_installed(c("bslib", "bsicons"))

window_height_ui <- function(id) {
ns <- shiny::NS(id)
namespaced_id <- ns("window_height")
Expand Down Expand Up @@ -37,6 +35,15 @@ window_height_server <- function(id) {
})
}

make_chat_history <- function(chats) {
purrr::discard(chats, \(x) x$role == "system") |>
purrr::map(\(x) {
list(shiny::strong(stringr::str_to_title(x$role)),
shiny::markdown(x$content))
}) |>
purrr::list_flatten()
}

indices <- gpttools::list_index() |> tools::file_path_sans_ext()

ui <- bslib::page_fluid(
Expand Down Expand Up @@ -73,7 +80,7 @@ ui <- bslib::page_fluid(
"Preferences",
icon = bsicons::bs_icon("gear-wide-connected"),
shiny::selectInput("model", "Model",
choices = c("gpt-3.5-turbo", "gpt-4")
choices = c("gpt-3.5-turbo", "gpt-4")
),
shiny::radioButtons(
"save_history", "Save & Use History",
Expand Down Expand Up @@ -166,19 +173,19 @@ server <- function(input, output, session) {
)
)
)
r$all_chats_formatted <- gptstudio:::prepare_chat_history(r$all_chats)
r$all_chats_formatted <- make_chat_history(r$all_chats)
waiter::waiter_hide()
shiny::updateTextAreaInput(session, "chat_input", value = "")
}) |>
shiny::bindEvent(input$chat)

output$all_chats_box <- renderUI({
cli::cli_inform("height: {height()}")
shiny::req(length(r$context_links) > 0)
bslib::card(
height = height() - 300,
bslib::card_header("Chat History", class = "bg-primary"),
bslib::card_body(
fill = FALSE,
r$all_chats_formatted,
shiny::markdown("**Sources**"),
shiny::markdown(paste0("* ", unique(r$context_links), collapse = "\n"))
Expand Down

0 comments on commit 285ebb8

Please sign in to comment.