Skip to content

Commit

Permalink
[feat] settings app
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Feb 12, 2024
1 parent 65f5b19 commit 41335fe
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 43 deletions.
10 changes: 0 additions & 10 deletions R/chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,8 @@ ghost_chat <- function(service = getOption("gpttools.service", "openai"),
stream = TRUE,
where = "source") {

cli::cli_alert_info("Getting context")

context <- get_cursor_context()

cli::cli_alert_info("Got context")

cli::cat_print(context)

instructions <- glue::glue(
"You are an expert coding assistant that provides brief code suggestions
directly into the files as code. Your response will go directly into an
Expand Down Expand Up @@ -163,10 +157,6 @@ ghost_chat <- function(service = getOption("gpttools.service", "openai"),
{context$above}
{context$below}"
)

cli::cli_alert_info("Here are the instructions")
cli::cat_print(instructions)

stream_chat(
prompt = instructions,
service = service,
Expand Down
8 changes: 6 additions & 2 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#' @param sources The sources to use, default is "All".
#' @param run_code Whether to execute generated code with `reprex::reprex()`,
#' default is FALSE.
#' @param add_context Whether to add context to the query. Options are
#' `"always"`, `"sometimes"`, and `"never"`. The default is `"sometimes"`.
#' @param persist Logical indicating whether to persist the settings, default
#' is TRUE.
#' @return Invisible NULL.
Expand All @@ -30,12 +32,13 @@ save_user_config <- function(service = "openai",
k_context = 4,
k_history = 4,
save_history = TRUE,
add_context = "sometimes",
sources = "All",
run_code = FALSE,
persist = TRUE) {
ops <- tibble::tibble(
service, model, task, local_embed, openai_embed_model, local_embed_model,
k_context, k_history, sources, run_code, save_history
k_context, k_history, sources, run_code, save_history, add_context
)

if (persist == TRUE) {
Expand Down Expand Up @@ -79,7 +82,8 @@ set_user_config <- function(path = NULL) {
gpttools.sources = ops$sources,
gpttools.openai_embed_model = ops$openai_embed_model,
gpttools.local_embed_model = ops$local_embed_model,
gpttools.run_code = ops$run_code
gpttools.run_code = ops$run_code,
gpttools.add_context = ops$add_context
)
invisible(TRUE)
} else {
Expand Down
15 changes: 12 additions & 3 deletions R/history.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ check_context <- function(context) {
#' @param service Name of the AI service to use, defaults to openai.
#' @param model Name of the openai model to use, defaults to gpt-3.5-turbo
#' @param index Index to look for context.
#' @param add_context Whether to add context to the query or not. Default is
#' TRUE.
#' @param add_context Whether to add context to the query. Options are
#' `"always"`, `"sometimes"`, and `"never"`. The default is `"sometimes"`.
#' @param check_context Whether to check if context is needed. Default is FALSE.
#' @param chat_history Chat history dataframe for reference.
#' @param history_name Name of the file where chat history is stored.
Expand Down Expand Up @@ -192,7 +192,6 @@ chat_with_context <- function(query,
model = "gpt-4-turbo-preview",
index = NULL,
add_context = TRUE,
check_context = FALSE,
chat_history = NULL,
history_name = "chat_history",
session_history = NULL,
Expand All @@ -208,6 +207,16 @@ chat_with_context <- function(query,
rv = NULL) {
arg_match(task, c("Context Only", "Permissive Chat"))

if (add_context == "sometimes") {
check_context <- TRUE
} else if (add_context == "never") {
check_context <- FALSE
add_context <- FALSE
} else if (add_context == "always") {
check_context <- FALSE
add_context <- TRUE
}

if (rlang::is_true(check_context)) {
need_context <- is_context_needed(
user_prompt = query,
Expand Down
3 changes: 2 additions & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
gpttools.save_history = FALSE,
gpttools.sources = "All",
gpttools.openai_embed_model = "text-embedding-3-small",
gpttools.run_code = FALSE
gpttools.run_code = FALSE,
gpttools.add_context = "sometimes"
)

toset <- !(names(op_gpttools) %in% names(op))
Expand Down
43 changes: 21 additions & 22 deletions inst/retriever/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ api_services <-
ui <- page_fillable(
useWaiter(),
waiterOnBusy(
html = spin_3circles(),
color = transparent(0.5)
html = spin_3circles(),
color = transparent(0.5)
),
shinyjs::useShinyjs(),
window_height_ui("height"),
Expand Down Expand Up @@ -112,7 +112,14 @@ ui <- page_fillable(
"task", "Task",
choices = c("Context Only", "Permissive Chat"),
selected = getOption("gpttools.task", "Permissive Chat")
)
),
radioButtons(
"test_code", "Test Code",
choiceNames = c("Yes", "No"),
choiceValues = c(TRUE, FALSE),
inline = TRUE,
selected = getOption("gpttools.test_code", FALSE)
),
),
br(),
accordion_panel(
Expand All @@ -124,7 +131,7 @@ ui <- page_fillable(
selected = getOption("gpttools.service", "openai")
),
selectInput("model", "Model",
choices = NULL
choices = NULL
),
selectInput(
"embed_model", "OpenAI Embedding Model",
Expand All @@ -145,13 +152,6 @@ ui <- page_fillable(
selected = TRUE,
inline = TRUE
),
radioButtons(
"test_code", "Test Code",
choiceNames = c("Yes", "No"),
choiceValues = c(TRUE, FALSE),
inline = TRUE,
selected = getOption("gpttools.test_code", FALSE)
),
radioButtons(
"local", "Local Embeddings",
choiceNames = c("Yes", "No"),
Expand All @@ -170,19 +170,18 @@ ui <- page_fillable(
"gpttools.local_embed_model",
"BAAI/bge-small-en-v1.5"
)
),
radioButtons(
"always_add_context", "Always Add Context",
choiceNames = c("Yes", "No"),
choiceValues = c(TRUE, FALSE),
selected = getOption("gpttools.always_add_context", TRUE),
inline = TRUE
)
),
br(),
accordion_panel(
"History & Context",
icon = bs_icon("book", class = "ms-auto"),
radioButtons(
"add_context", "Always Add Context",
choices = c("sometimes", "always", "never"),
selected = getOption("gpttools.add_context", "sometimes"),
inline = FALSE
),
radioButtons(
"save_history", "Save & Use History",
choiceNames = c("Yes", "No"),
Expand Down Expand Up @@ -313,8 +312,8 @@ server <- function(input, output, session) {
)
)
observe(updateSelectInput(session, "source",
choices = c("All", indices()),
selected = getOption("gpttools.sources", "All")
choices = c("All", indices()),
selected = getOption("gpttools.sources", "All")
))
observe({
toggle_popover("settings", show = FALSE)
Expand All @@ -330,6 +329,7 @@ server <- function(input, output, session) {
save_history = input$save_history,
sources = input$source,
run_code = input$test_code,
add_context = input$add_context,
persist = TRUE
)
}) |> bindEvent(input$save_settings)
Expand All @@ -339,8 +339,7 @@ server <- function(input, output, session) {
service = input$service,
model = input$model,
index = index(),
add_context = TRUE,
check_context = input$always_add_context,
add_context = input$add_context,
chat_history = read_history(local = input$local),
session_history = r$all_chats,
add_history = input$save_history,
Expand Down
2 changes: 2 additions & 0 deletions inst/scripts/test.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
library(tidyverse)
library(tidymodels)
tidymodels_prefer()

# create a glm workflow using mtcars
Loading

0 comments on commit 41335fe

Please sign in to comment.