Skip to content

Commit

Permalink
Merge pull request #153 from calderonsamuel/export-history
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade authored Nov 16, 2023
2 parents fee59f9 + d686f34 commit 841ae98
Show file tree
Hide file tree
Showing 15 changed files with 593 additions and 157 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ Imports:
cli,
colorspace,
curl,
fontawesome,
glue,
grDevices,
htmltools,
htmlwidgets,
httr2,
ids,
jsonlite,
magrittr,
purrr,
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# gptstudio (development version)

## 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.
- We have a shorter welcome message, but we have added lots of tooltips to help with navigation.

## Internal

- Reverted back to use an R6 class for OpenAI streaming. This doesn't affect how the users interact with the addins.
- Fixed a bug in retrieval of OpenAI models

# gptstudio 0.3.1

Expand Down
19 changes: 9 additions & 10 deletions R/check_api.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,22 @@ check_api_connection <- function(api_key, verbose = FALSE) {
#' success message is printed. If the API key is not in the correct format,
#' an error message is printed and the function aborts.
check_api_key <- function(api_key) {
set_key_instructions <-
cli_inform(
c(
"!" = "OPENAI_API_KEY is not valid.",
"i" = "Generate a key at {.url https://platform.openai.com/account/api-keys}",
"i" = "Set the key in your .Renviron file {.run usethis::edit_r_environ()}"
)
key_instructions <-
c(
"!" = "OPENAI_API_KEY is not valid.",
"i" = "Generate a key at {.url https://platform.openai.com/account/api-keys}",
"i" = "Set the key in your .Renviron file {.run usethis::edit_r_environ()}"
)

if (is.null(api_key)) {
set_key_instructions
cli::cli_inform(key_instructions)
invisible(FALSE)
}

if (stringr::str_detect(api_key, "^[a-zA-Z0-9-]{30,60}$")) {
if (stringr::str_detect(api_key, "^([a-zA-Z0-9]|-){30,60}$")) {
invisible(TRUE)
} else {
set_key_instructions
cli::cli_inform(key_instructions)
invisible(FALSE)
}
}
Expand Down
39 changes: 29 additions & 10 deletions R/mod_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@ mod_app_ui <- function(id, ide_colors = get_ide_theme_info()) {
bslib::page_fluid(
theme = create_chat_app_theme(ide_colors),
title = "ChatGPT from gptstudio",
class = "vh-100 p-3 m-0",
class = "vh-100 p-0 m-0",
html_dependencies(),
div(
class = "row justify-content-center h-100",

bslib::layout_sidebar(
class = "vh-100",
sidebar = bslib::sidebar(
open = "closed",
width = 300,
class = "p-0",
padding = "0.5rem",

mod_sidebar_ui(ns("sidebar"), translator)
),
div(
class = "col h-100",
style = htmltools::css(`max-width` = "800px"),
mod_chat_ui(ns("chat"), translator)
class = "row justify-content-center h-100",
div(
class = "col h-100",
style = htmltools::css(`max-width` = "800px"),
mod_chat_ui(ns("chat"), translator)
)
)
)
)
Expand All @@ -35,7 +47,14 @@ mod_app_ui <- function(id, ide_colors = get_ide_theme_info()) {
#'
mod_app_server <- function(id, ide_colors = get_ide_theme_info()) {
moduleServer(id, function(input, output, session) {
mod_chat_server("chat", ide_colors)
sidebar <- mod_sidebar_server("sidebar")
mod_chat_server(
id = "chat",
ide_colors = ide_colors,
translator = NULL,
settings = sidebar$settings,
history = sidebar$history
)
})
}

Expand Down Expand Up @@ -69,7 +88,7 @@ create_chat_app_theme <- function(ide_colors = get_ide_theme_info()) {
version = 5,
bg = ide_colors$bg,
fg = ide_colors$fg,
font_scale = 0.9
font_scale = 0.9,
)
}

Expand Down Expand Up @@ -109,10 +128,10 @@ get_ide_theme_info <- function() {

html_dependencies <- function() {
htmltools::htmlDependency(
name = "gptstudio-assets", version = "0.2.0",
name = "gptstudio-assets", version = "0.4.0",
package = "gptstudio",
src = "assets",
script = c("js/copyToClipboard.js", "js/shiftEnter.js"),
script = c("js/copyToClipboard.js", "js/shiftEnter.js", "js/conversation.js"),
stylesheet = c("css/mod_app.css")
)
}
Expand Down
58 changes: 20 additions & 38 deletions R/mod_chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,34 @@ mod_chat_ui <- function(id, translator = create_translator()) {
),
div(
class = "mt-auto",
style = css(
"margin-left" = "40px",
"margin-right" = "40px"
),
htmltools::div(
class = "d-flex p-3",
class = "position-relative",
style = css(
"width" = "100%"
),
div(
class = "flex-grow-1 pe-3",
text_area_input_wrapper(
inputId = ns("chat_input"),
label = NULL,
width = "100%",
placeholder = translator$t("Write your prompt here"),
value = "",
resize = "vertical",
rows = 5,
resize = "none",
textarea_class = "chat-prompt"
)
),
div(
style = htmltools::css(width = "50px"),
class = "position-absolute top-50 end-0 translate-middle",
actionButton(
inputId = ns("chat"),
label = icon("fas fa-paper-plane"),
class = "w-100 btn-primary p-1 chat-send-btn"
),
actionButton(
inputId = ns("clear_history"),
label = icon("eraser"),
class = "w-100 btn-primary mt-2 p-1"
),
actionButton(
inputId = ns("settings"),
label = icon("gear"),
class = "w-100 btn-primary mt-2 p-1")
) %>%
bslib::tooltip("Send (click or Enter)")
)
)
)
Expand All @@ -64,11 +61,14 @@ mod_chat_ui <- function(id, translator = create_translator()) {
#'
#' @param id id of the module
#' @param translator Translator from `shiny.i18n::Translator`
#' @param settings,history Reactive values from the settings and history module
#' @inheritParams run_chatgpt_app
#'
mod_chat_server <- function(id,
ide_colors = get_ide_theme_info(),
translator = create_translator()) {
translator = create_translator(),
settings,
history) {
# This is where changes will focus
moduleServer(id, function(input, output, session) {

Expand All @@ -77,12 +77,9 @@ mod_chat_server <- function(id,
ns <- session$ns

rv <- reactiveValues()
rv$chat_history <- list()
rv$reset_welcome_message <- 0L
rv$reset_streaming_message <- 0L

settings <- mod_settings_server("settings")

# UI outputs ----

output$welcome <- renderWelcomeMessage({
Expand All @@ -92,7 +89,7 @@ mod_chat_server <- function(id,


output$history <- renderUI({
rv$chat_history %>%
history$chat_history %>%
style_chat_history(ide_colors = ide_colors)
})

Expand All @@ -108,10 +105,9 @@ mod_chat_server <- function(id,
# Observers ----

observe({
rv$chat_history <- list()
rv$reset_welcome_message <- rv$reset_welcome_message + 1L
}) %>%
bindEvent(input$clear_history)
bindEvent(history$create_new_chat)


observe({
Expand All @@ -120,7 +116,7 @@ mod_chat_server <- function(id,
service = settings$service,
model = settings$model,
prompt = input$chat_input,
history = rv$chat_history,
history = history$chat_history,
stream = settings$stream
) %>%
gptstudio_skeleton_build(
Expand All @@ -136,7 +132,7 @@ mod_chat_server <- function(id,
) %>%
gptstudio_response_process()

rv$chat_history <- response$history
history$chat_history <- response$history

if (settings$stream) {
rv$reset_streaming_message <- rv$reset_streaming_message + 1L
Expand All @@ -147,19 +143,5 @@ mod_chat_server <- function(id,
}) %>%
bindEvent(input$chat)


observe({
showModal(
modalDialog(
title = "Settings",
easyClose = TRUE,
footer = modalButton("Save"),
size = "l",

mod_settings_ui(ns("settings"), translator = translator)

))
}) %>% bindEvent(input$settings)

})
}
Loading

0 comments on commit 841ae98

Please sign in to comment.