From 5db606c4216db82616d0607427a65dc21d5aa604 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Mon, 25 Mar 2024 14:39:39 -0500 Subject: [PATCH 1/8] Adds OS determination functions --- R/utils.R | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/R/utils.R b/R/utils.R index 5e34a2d..7cd23af 100644 --- a/R/utils.R +++ b/R/utils.R @@ -110,3 +110,22 @@ print_provider <- function(x) { cli_li("{.val0 Model:} {.val1 {x$model}}") cli_li("{.val0 Label:} {.val1 {x$label}}") } + +# ------------------------ Determine OS ---------------------------------------- +os_get <- function() { + if (.Platform$OS.type == "windows") { + "win" + } else if (Sys.info()["sysname"] == "Darwin") { + "mac" + } else { + "unix" + } +} + +os_win <- function() { + ifelse(os_get() == "win", TRUE, FALSE) +} + +os_mac <- function() { + ifelse(os_get() == "mac", TRUE, FALSE) +} From 389af7f83fdd44dd83e9e99b473738cdb675030a Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Mon, 25 Mar 2024 15:07:49 -0500 Subject: [PATCH 2/8] Fixes #79 --- R/backend-openai-core.R | 13 +++++++++++++ inst/configs/copilot.yml | 1 - tests/testthat/test-backend-openai-core.R | 6 ++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/R/backend-openai-core.R b/R/backend-openai-core.R index 6f0e1c6..d26c970 100644 --- a/R/backend-openai-core.R +++ b/R/backend-openai-core.R @@ -12,7 +12,20 @@ openai_token_copilot <- function(defaults = NULL, fail = TRUE) { if (ch_debug_get()) { return("") } + hosts_path <- defaults$hosts_path + + if(is.null(hosts_path)) { + if(os_win()) { + possible_path <- path(Sys.getenv("localappdata"), "github-copilot") + } else { + possible_path <- "~/.config/github-copilot" + } + if(dir_exists(possible_path)) { + hosts_path <- possible_path + } + } + token_url <- defaults$token_url if(is.null(hosts_path) && fail) { abort( diff --git a/inst/configs/copilot.yml b/inst/configs/copilot.yml index bca587b..9714cbe 100644 --- a/inst/configs/copilot.yml +++ b/inst/configs/copilot.yml @@ -8,7 +8,6 @@ default: include_doc_contents: FALSE system_msg: You are a helpful coding assistant token_url: "https://api.github.com/copilot_internal/v2/token" - hosts_path: "~/.config/github-copilot" model_arguments: stream: TRUE chat: diff --git a/tests/testthat/test-backend-openai-core.R b/tests/testthat/test-backend-openai-core.R index 1719db7..7a6b825 100644 --- a/tests/testthat/test-backend-openai-core.R +++ b/tests/testthat/test-backend-openai-core.R @@ -172,8 +172,10 @@ test_that("Copilot token finder works", { ) expect_equal(out, "12345") def_errors <- defaults - def_errors$hosts_path <- NULL - expect_error(openai_token_copilot(def_errors), "There is no default") + if(!is.na(Sys.getenv("CI", unset = NA))) { + def_errors$hosts_path <- NULL + expect_error(openai_token_copilot(def_errors), "There is no default") + } def_errors$hosts_path <- "" def_errors$token_url <- NULL expect_error(openai_token_copilot(def_errors), "There is no default GH") From 2bcee0de620718197c4b6fd8af2692a783259d18 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Mon, 25 Mar 2024 15:08:50 -0500 Subject: [PATCH 3/8] styler updates, version bump --- DESCRIPTION | 2 +- R/backend-openai-core.R | 18 ++++++------ R/backend-openai-submit.R | 3 +- R/chattr-app.R | 2 +- R/chattr-defaults.R | 3 +- R/chattr-use.R | 17 ++++++----- R/chattr.R | 3 +- tests/testthat/test-app_server.R | 2 +- tests/testthat/test-backend-llamagpt.R | 5 ++-- tests/testthat/test-backend-openai-core.R | 32 ++++++++++----------- tests/testthat/test-backend-openai-switch.R | 25 ++-------------- tests/testthat/test-chattr-use.R | 1 - 12 files changed, 46 insertions(+), 67 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9428ac1..7346d95 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: chattr Title: Integrates LLM's with the RStudio IDE -Version: 0.0.0.9007 +Version: 0.0.0.9008 Authors@R: c( person("Edgar", "Ruiz", , "edgar@posit.co", role = c("aut", "cre")), person(given = "Posit Software, PBC", role = c("cph", "fnd")) diff --git a/R/backend-openai-core.R b/R/backend-openai-core.R index d26c970..5f7df9b 100644 --- a/R/backend-openai-core.R +++ b/R/backend-openai-core.R @@ -15,31 +15,33 @@ openai_token_copilot <- function(defaults = NULL, fail = TRUE) { hosts_path <- defaults$hosts_path - if(is.null(hosts_path)) { - if(os_win()) { + if (is.null(hosts_path)) { + if (os_win()) { possible_path <- path(Sys.getenv("localappdata"), "github-copilot") } else { possible_path <- "~/.config/github-copilot" } - if(dir_exists(possible_path)) { + if (dir_exists(possible_path)) { hosts_path <- possible_path } } token_url <- defaults$token_url - if(is.null(hosts_path) && fail) { + if (is.null(hosts_path) && fail) { abort( c( "There is no default for the RStudio GitHub Copilot configuration folder", "Please add a 'hosts_path' to your YAML file, or to chattr_defaults() " - )) + ) + ) } - if(is.null(token_url) && fail) { + if (is.null(token_url) && fail) { abort( c( "There is no default GH Copilot token URL", "Please add a 'token_url' to your YAML file, or to chattr_defaults() " - )) + ) + ) } gh_path <- path_expand(hosts_path) if (dir_exists(gh_path)) { @@ -51,7 +53,7 @@ openai_token_copilot <- function(defaults = NULL, fail = TRUE) { x_json <- resp_body_json(x) ret <- x_json$token } else { - if(fail) { + if (fail) { abort("Please setup GitHub Copilot for RStudio first") } } diff --git a/R/backend-openai-submit.R b/R/backend-openai-submit.R index ccfd192..e255a10 100644 --- a/R/backend-openai-submit.R +++ b/R/backend-openai-submit.R @@ -110,8 +110,7 @@ openai_completion <- function( prompt, new_prompt, r_file_stream, - r_file_complete - ) { + r_file_complete) { UseMethod("openai_completion") } diff --git a/R/chattr-app.R b/R/chattr-app.R index b2ba515..5a5b165 100644 --- a/R/chattr-app.R +++ b/R/chattr-app.R @@ -14,7 +14,7 @@ chattr_app <- function(viewer = c("viewer", "dialog"), as_job_host = getOption("shiny.host", "127.0.0.1")) { td <- chattr_defaults(type = "chat") show_init <- TRUE - if(interactive() && is.null(td$provider)) { + if (interactive() && is.null(td$provider)) { chattr_use() td <- chattr_defaults(type = "chat") show_init <- FALSE diff --git a/R/chattr-defaults.R b/R/chattr-defaults.R index 5713325..548cb86 100644 --- a/R/chattr-defaults.R +++ b/R/chattr-defaults.R @@ -45,8 +45,7 @@ chattr_defaults <- function(type = "default", yaml_file = "chattr.yml", force = FALSE, label = NULL, - ... - ) { + ...) { function_args <- c(as.list(environment()), ...) sys_type <- Sys.getenv("CHATTR_TYPE", NA) diff --git a/R/chattr-use.R b/R/chattr-use.R index d219acb..b723db3 100644 --- a/R/chattr-use.R +++ b/R/chattr-use.R @@ -42,7 +42,7 @@ ch_get_ymls <- function(menu = TRUE) { copilot_token <- openai_token_copilot( defaults = copilot_defaults$default, fail = FALSE - ) + ) copilot_exists <- !is.null(copilot_token) gpt_token <- openai_token_chat(fail = FALSE) @@ -53,7 +53,7 @@ ch_get_ymls <- function(menu = TRUE) { read_yaml() llama_exists <- file_exists(llama_defaults$default$path) && - file_exists(llama_defaults$default$model) + file_exists(llama_defaults$default$model) prep_files <- files %>% map(read_yaml) %>% @@ -71,25 +71,25 @@ ch_get_ymls <- function(menu = TRUE) { path_ext_remove() ) - if(!copilot_exists) { + if (!copilot_exists) { prep_files$copilot <- NULL } - if(!gpt_exists) { + if (!gpt_exists) { prep_files$gpt35 <- NULL prep_files$gpt4 <- NULL prep_files$davinci <- NULL } - if(!llama_exists) { + if (!llama_exists) { prep_files$llamagpt <- NULL } - if(length(prep_files) == 0) { + if (length(prep_files) == 0) { abort( "No model setup found. Please use `?chattr_use` to get started", call = NULL - ) + ) } orig_names <- names(prep_files) @@ -106,7 +106,7 @@ ch_get_ymls <- function(menu = TRUE) { }) %>% set_names(orig_names) - if(menu) { + if (menu) { cli_h3("chattr - Available models") cli_text("Select the number of the model you would like to use: ") model_no <- menu(prep_files) @@ -115,5 +115,4 @@ ch_get_ymls <- function(menu = TRUE) { } else { prep_files } - } diff --git a/R/chattr.R b/R/chattr.R index 38b34c7..61c696e 100644 --- a/R/chattr.R +++ b/R/chattr.R @@ -6,12 +6,11 @@ chattr <- function(prompt = NULL, preview = FALSE, prompt_build = TRUE, stream = NULL) { - ui <- ui_current() if (ui == "") ui <- "console" defaults <- chattr_defaults(type = ui) - if(is.null(defaults$provider)) { + if (is.null(defaults$provider)) { chattr_use() defaults <- chattr_defaults(type = ui) } diff --git a/tests/testthat/test-app_server.R b/tests/testthat/test-app_server.R index 4e4fbe0..ece16d5 100644 --- a/tests/testthat/test-app_server.R +++ b/tests/testthat/test-app_server.R @@ -43,7 +43,7 @@ test_that("app_server() function runs", { session$sendCustomMessage <- function(...) {} expect_silent( app_server(list(), list(), session = session) - ) + ) }) test_that("Adding to history works", { diff --git a/tests/testthat/test-backend-llamagpt.R b/tests/testthat/test-backend-llamagpt.R index 9ed994e..657a630 100644 --- a/tests/testthat/test-backend-llamagpt.R +++ b/tests/testthat/test-backend-llamagpt.R @@ -65,7 +65,9 @@ test_that("Submit works", { local_mocked_bindings( ch_llamagpt_prompt = function(...) invisible(), ch_llamagpt_session = function(...) invisible(), - ch_llamagpt_output = function(...) return("test") + ch_llamagpt_output = function(...) { + return("test") + } ) defaults <- yaml::read_yaml(package_file("configs", "llamagpt.yml")) defaults <- as_ch_model(defaults$default, "chat") @@ -84,4 +86,3 @@ test_that("Submit works", { test_that("Restore to previews defaults", { expect_snapshot(chattr_use("gpt35")) }) - diff --git a/tests/testthat/test-backend-openai-core.R b/tests/testthat/test-backend-openai-core.R index 7a6b825..468c255 100644 --- a/tests/testthat/test-backend-openai-core.R +++ b/tests/testthat/test-backend-openai-core.R @@ -172,7 +172,7 @@ test_that("Copilot token finder works", { ) expect_equal(out, "12345") def_errors <- defaults - if(!is.na(Sys.getenv("CI", unset = NA))) { + if (!is.na(Sys.getenv("CI", unset = NA))) { def_errors$hosts_path <- NULL expect_error(openai_token_copilot(def_errors), "There is no default") } @@ -182,19 +182,19 @@ test_that("Copilot token finder works", { }) test_that("Completions works", { - local_mocked_bindings( - openai_switch = function(...) "test" - ) - defaults <- yaml::read_yaml(package_file("configs", "copilot.yml")) - defaults <- as_ch_model(defaults$default, "chat") - expect_equal( - openai_completion.ch_openai_github_copilot_chat( - prompt = "test", - new_prompt = "newtest", - defaults = defaults, - r_file_stream = tempfile(), - r_file_complete = tempfile() - ), - "test" - ) + local_mocked_bindings( + openai_switch = function(...) "test" + ) + defaults <- yaml::read_yaml(package_file("configs", "copilot.yml")) + defaults <- as_ch_model(defaults$default, "chat") + expect_equal( + openai_completion.ch_openai_github_copilot_chat( + prompt = "test", + new_prompt = "newtest", + defaults = defaults, + r_file_stream = tempfile(), + r_file_complete = tempfile() + ), + "test" + ) }) diff --git a/tests/testthat/test-backend-openai-switch.R b/tests/testthat/test-backend-openai-switch.R index fda3f71..69c9213 100644 --- a/tests/testthat/test-backend-openai-switch.R +++ b/tests/testthat/test-backend-openai-switch.R @@ -12,7 +12,7 @@ test_that("Using 'chat'", { prompt = "test", req_body = defaults$model_arguments, defaults = defaults, - r_file_stream = tempfile(), + r_file_stream = tempfile(), r_file_complete = tempfile() ) ) @@ -32,33 +32,14 @@ test_that("Using 'console'", { defaults <- yaml::read_yaml(package_file("configs", "gpt35.yml")) defaults <- as_ch_model(defaults$default, "console") expect_silent( - openai_switch( + openai_switch( prompt = "test", req_body = defaults$model_arguments, defaults = defaults, - r_file_stream = tempfile(), + r_file_stream = tempfile(), r_file_complete = tempfile() ) ) } ) }) - - - - - - - - - - - - - - - - - - - diff --git a/tests/testthat/test-chattr-use.R b/tests/testthat/test-chattr-use.R index f815371..4ec94d3 100644 --- a/tests/testthat/test-chattr-use.R +++ b/tests/testthat/test-chattr-use.R @@ -26,4 +26,3 @@ test_that("Menu works", { } ) }) - From f7eacc8f7e0e0c5d7b0a37df89ba879aa1e54946 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Mon, 25 Mar 2024 15:48:46 -0500 Subject: [PATCH 4/8] Adds output/returns to all exported functions --- R/chattr-app.R | 1 + R/chattr-defaults-save.R | 1 + R/chattr-defaults.R | 2 ++ R/chattr-test.R | 1 + R/chattr.R | 1 + man/chattr.Rd | 3 +++ man/chattr_app.Rd | 3 +++ man/chattr_defaults.Rd | 4 ++++ man/chattr_defaults_save.Rd | 3 +++ man/chattr_test.Rd | 3 +++ 10 files changed, 22 insertions(+) diff --git a/R/chattr-app.R b/R/chattr-app.R index 5a5b165..e67c03f 100644 --- a/R/chattr-app.R +++ b/R/chattr-app.R @@ -7,6 +7,7 @@ #' is set to TRUE. #' @param as_job_host Host IP to use for the Shiny app. Applicable only if `as_job` #' is set to TRUE. +#' @returns A chat interface inside the 'RStudio' IDE #' @export chattr_app <- function(viewer = c("viewer", "dialog"), as_job = getOption("chattr.as_job", FALSE), diff --git a/R/chattr-defaults-save.R b/R/chattr-defaults-save.R index 35072e2..d0d73e5 100644 --- a/R/chattr-defaults-save.R +++ b/R/chattr-defaults-save.R @@ -4,6 +4,7 @@ #' @param overwrite Indicates to replace the file if it exists #' @param type The type of UI to save the defaults for. It defaults to NULL which #' will save whatever types had been used during the current R session +#' @returns It creates a YAML file with the defaults set in the current R session. #' @export chattr_defaults_save <- function(path = "chattr.yml", overwrite = FALSE, diff --git a/R/chattr-defaults.R b/R/chattr-defaults.R index 548cb86..7998c29 100644 --- a/R/chattr-defaults.R +++ b/R/chattr-defaults.R @@ -29,6 +29,8 @@ #' @param force Re-process the base and any work space level file defaults #' @param label Label to display in the Shiny app, and other locations #' @param ... Additional model arguments that are not standard for all models/backends +#' @returns An 'ch_model' object that contains the current defaults that will be +#' used to communicate with the LLM. #' @inheritParams chattr chattr_defaults <- function(type = "default", diff --git a/R/chattr-test.R b/R/chattr-test.R index e1ae722..90fcffe 100644 --- a/R/chattr-test.R +++ b/R/chattr-test.R @@ -1,5 +1,6 @@ #' Confirms conectivity to LLM interface #' @inheritParams ch_submit +#' @returns It returns console massages with the status of the test. #' @export chattr_test <- function(defaults = NULL) { if (is.null(defaults)) defaults <- chattr_defaults() diff --git a/R/chattr.R b/R/chattr.R index 61c696e..3597f65 100644 --- a/R/chattr.R +++ b/R/chattr.R @@ -1,6 +1,7 @@ #' Submits prompt to LLM #' @param prompt Request to send to LLM. Defaults to NULL #' @inheritParams ch_submit +#' @returns The output of the LLM to the console, document or script. #' @export chattr <- function(prompt = NULL, preview = FALSE, diff --git a/man/chattr.Rd b/man/chattr.Rd index 947b7d1..0e9b039 100644 --- a/man/chattr.Rd +++ b/man/chattr.Rd @@ -19,6 +19,9 @@ request} \item{stream}{To output the response from the LLM as it happens, or wait until the response is complete. Defaults to TRUE.} } +\value{ +The output of the LLM to the console, document or script. +} \description{ Submits prompt to LLM } diff --git a/man/chattr_app.Rd b/man/chattr_app.Rd index 5dce71c..9a84072 100644 --- a/man/chattr_app.Rd +++ b/man/chattr_app.Rd @@ -24,6 +24,9 @@ is set to TRUE.} \item{as_job_host}{Host IP to use for the Shiny app. Applicable only if \code{as_job} is set to TRUE.} } +\value{ +A chat interface inside the 'RStudio' IDE +} \description{ Starts a Shiny app interface to the LLM } diff --git a/man/chattr_defaults.Rd b/man/chattr_defaults.Rd index e6cdea9..9ccb83c 100644 --- a/man/chattr_defaults.Rd +++ b/man/chattr_defaults.Rd @@ -63,6 +63,10 @@ defaults to use in a session} \item{...}{Additional model arguments that are not standard for all models/backends} } +\value{ +An 'ch_model' object that contains the current defaults that will be +used to communicate with the LLM. +} \description{ Default arguments to use when making requests to the LLM } diff --git a/man/chattr_defaults_save.Rd b/man/chattr_defaults_save.Rd index 59b30b3..02700f5 100644 --- a/man/chattr_defaults_save.Rd +++ b/man/chattr_defaults_save.Rd @@ -15,6 +15,9 @@ chattr_defaults_save(path = "chattr.yml", overwrite = FALSE, type = NULL) \item{type}{The type of UI to save the defaults for. It defaults to NULL which will save whatever types had been used during the current R session} } +\value{ +It creates a YAML file with the defaults set in the current R session. +} \description{ Saves the current defaults in a yaml file that is compatible with the config package diff --git a/man/chattr_test.Rd b/man/chattr_test.Rd index 4f6daa8..8bd7edc 100644 --- a/man/chattr_test.Rd +++ b/man/chattr_test.Rd @@ -12,6 +12,9 @@ ch_test(defaults = NULL) \arguments{ \item{defaults}{Defaults object, generally puled from \code{chattr_defaults()}} } +\value{ +It returns console massages with the status of the test. +} \description{ Confirms conectivity to LLM interface } From 1805a6d63519d3aefb39dffa279e2e561ec7fca0 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Tue, 26 Mar 2024 09:07:15 -0500 Subject: [PATCH 5/8] First linter pass --- R/app_server.R | 1 - R/app_theme.R | 2 +- R/backend-llamagpt.R | 4 +++- R/chattr-app.R | 4 ++-- R/chattr-defaults-save.R | 7 ++++--- R/chattr-defaults.R | 18 ++++++++++-------- R/chattr-use.R | 1 - R/ide.R | 2 -- inst/prompt/script.R | 2 -- 9 files changed, 20 insertions(+), 21 deletions(-) diff --git a/R/app_server.R b/R/app_server.R index a56caca..a1edc56 100644 --- a/R/app_server.R +++ b/R/app_server.R @@ -136,7 +136,6 @@ app_add_user <- function(content) { } app_add_assistant <- function(content, input) { - style <- app_theme_style("ui_assistant") len_hist <- length(ch_env$content_hist) ch <- app_split_content(content) diff --git a/R/app_theme.R b/R/app_theme.R index 0953dd9..32a8e35 100644 --- a/R/app_theme.R +++ b/R/app_theme.R @@ -1,5 +1,5 @@ app_theme_style <- function(x = NULL) { - if ((ide_is_rstudio() && !ch_debug_get()) | running_as_job()) { + if ((ide_is_rstudio() && !ch_debug_get()) || running_as_job()) { ti <- getThemeInfo() color_bg <- app_theme_rgb_to_hex(ti$background) color_fg <- app_theme_rgb_to_hex(ti$foreground) diff --git a/R/backend-llamagpt.R b/R/backend-llamagpt.R index df9dd75..62c7f5b 100644 --- a/R/backend-llamagpt.R +++ b/R/backend-llamagpt.R @@ -114,7 +114,9 @@ ch_llamagpt_output <- function( if (stop_stream) { if (stream_to == "chat") { - if (!is.null(output_file)) saveRDS(all_output, output_file, compress = FALSE) + if (!is.null(output_file)) { + saveRDS(all_output, output_file, compress = FALSE) + } file_delete(stream_file) return(NULL) } else { diff --git a/R/chattr-app.R b/R/chattr-app.R index e67c03f..f022d6e 100644 --- a/R/chattr-app.R +++ b/R/chattr-app.R @@ -5,8 +5,8 @@ #' the document, or console, in the IDE. #' @param as_job_port Port to use for the Shiny app. Applicable only if `as_job` #' is set to TRUE. -#' @param as_job_host Host IP to use for the Shiny app. Applicable only if `as_job` -#' is set to TRUE. +#' @param as_job_host Host IP to use for the Shiny app. Applicable only if +#' `as_job` is set to TRUE. #' @returns A chat interface inside the 'RStudio' IDE #' @export chattr_app <- function(viewer = c("viewer", "dialog"), diff --git a/R/chattr-defaults-save.R b/R/chattr-defaults-save.R index d0d73e5..7e02d43 100644 --- a/R/chattr-defaults-save.R +++ b/R/chattr-defaults-save.R @@ -2,9 +2,10 @@ #' the config package #' @param path Path to the file to save the configuration to #' @param overwrite Indicates to replace the file if it exists -#' @param type The type of UI to save the defaults for. It defaults to NULL which -#' will save whatever types had been used during the current R session -#' @returns It creates a YAML file with the defaults set in the current R session. +#' @param type The type of UI to save the defaults for. It defaults to NULL +#' which will save whatever types had been used during the current R session +#' @returns It creates a YAML file with the defaults set in the current R +#' session. #' @export chattr_defaults_save <- function(path = "chattr.yml", overwrite = FALSE, diff --git a/R/chattr-defaults.R b/R/chattr-defaults.R index 7998c29..92bd0f5 100644 --- a/R/chattr-defaults.R +++ b/R/chattr-defaults.R @@ -2,8 +2,9 @@ #' @details The idea is that because we will use addin shortcut to execute the #' request, all of the other arguments can be controlled via this function. By #' default, it will try to load defaults from a `config` YAML file, if none are -#' found, then the defaults for GPT 3.5 will be used. The defaults can be modified -#' by calling this function, even after the interactive session has started. +#' found, then the defaults for GPT 3.5 will be used. The defaults can be +#' modified by calling this function, even after the interactive session has +#' started. #' @export #' @param max_data_files Sets the maximum number of data files to send to the #' model. It defaults to 20. To send all, set to NULL @@ -21,14 +22,15 @@ #' part of the request #' @param yaml_file The path to a valid `config` YAML file that contains the #' defaults to use in a session -#' @param model_arguments Additional arguments to pass to the model as part of the -#' request, it requires a list. Examples of arguments: temperature, top_p, +#' @param model_arguments Additional arguments to pass to the model as part of +#' the request, it requires a list. Examples of arguments: temperature, top_p, #' max_tokens -#' @param type Entry point to interact with the model. Accepted values: 'notebook', -#' 'chat' +#' @param type Entry point to interact with the model. Accepted values: +#' 'notebook', chat' #' @param force Re-process the base and any work space level file defaults #' @param label Label to display in the Shiny app, and other locations -#' @param ... Additional model arguments that are not standard for all models/backends +#' @param ... Additional model arguments that are not standard for all +#' models/backends #' @returns An 'ch_model' object that contains the current defaults that will be #' used to communicate with the LLM. #' @inheritParams chattr @@ -91,7 +93,7 @@ chattr_defaults <- function(type = "default", for (i in seq_along(check_defaults)) { td <- td_defaults[[check_defaults[i]]] if (!is.null(td)) { - if (length(td$prompt) > 0 & any(grepl("\n", td$prompt))) { + if (length(td$prompt) > 0 && any(grepl("\n", td$prompt))) { td$prompt <- unlist(strsplit(td$prompt, split = "\n")) } chattr_defaults_set( diff --git a/R/chattr-use.R b/R/chattr-use.R index b723db3..834ebae 100644 --- a/R/chattr-use.R +++ b/R/chattr-use.R @@ -24,7 +24,6 @@ #' @export chattr_use <- function(model_label = NULL) { interactive_label <- is_interactive() && is.null(model_label) - overwrite <- FALSE if (interactive_label) { model_label <- ch_get_ymls() } diff --git a/R/ide.R b/R/ide.R index 5d467c2..d525c15 100644 --- a/R/ide.R +++ b/R/ide.R @@ -66,9 +66,7 @@ ide_comment_selection <- function() { text_range <- active_doc$selection[[1]]$range start_row <- text_range$start[[1]] - start_col <- text_range$start[[2]] end_row <- text_range$end[[1]] - end_col <- text_range$end[[2]] selected <- active_doc$contents[start_row:end_row] end_size <- nchar(selected[length(selected)]) diff --git a/inst/prompt/script.R b/inst/prompt/script.R index 7f1d85d..f84c9f4 100644 --- a/inst/prompt/script.R +++ b/inst/prompt/script.R @@ -8,5 +8,3 @@ writeLines( ), here::here("inst", "prompt", "base.txt") ) - - From b2022a30f47f89fe47c132dcdaea4984f1385171 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Tue, 26 Mar 2024 09:54:00 -0500 Subject: [PATCH 6/8] Reduce cyclomatic complexity of openai_parse_ide() --- R/backend-openai-switch.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/backend-openai-switch.R b/R/backend-openai-switch.R index 0f558a6..ad2408b 100644 --- a/R/backend-openai-switch.R +++ b/R/backend-openai-switch.R @@ -85,12 +85,12 @@ openai_parse_ide <- function(x, defaults, testing = FALSE) { has_error <- substr(current, 1, 9) == "{{error}}" - if (!is.null(current)) { + if (!is.null(current) && !testing && !has_error) { if (is.null(ch_env$stream$response)) { if (ui_current_console()) { - if (!testing && !has_error) cat(current) + cat(current) } else { - if (!testing && !has_error) ide_paste_text(current) + ide_paste_text(current) } } else { if (nchar(current) != nchar(ch_env$stream$response)) { @@ -100,10 +100,10 @@ openai_parse_ide <- function(x, defaults, testing = FALSE) { nchar(current) ) if (ui_current_console()) { - if (!testing && !has_error) cat(delta) + cat(delta) } else { for (i in 1:nchar(delta)) { - if (!testing && !has_error) ide_paste_text(substr(delta, i, i)) + ide_paste_text(substr(delta, i, i)) } } } From 8bb36724878d3f3cc1f39db47addd614b41380d9 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Tue, 26 Mar 2024 10:28:51 -0500 Subject: [PATCH 7/8] Reducing complexity a bit --- R/chattr-defaults.R | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/chattr-defaults.R b/R/chattr-defaults.R index 92bd0f5..2b47553 100644 --- a/R/chattr-defaults.R +++ b/R/chattr-defaults.R @@ -86,9 +86,7 @@ chattr_defaults <- function(type = "default", for (j in seq_along(check_files)) { td_defaults <- read_yaml(file = check_files[j]) loaded_default <- chattr_defaults_get(type = "default") - if (!is.null(loaded_default)) { - td_defaults$default <- loaded_default - } + td_defaults$default <- loaded_default %||% td_defaults$default check_defaults <- c("default", type) for (i in seq_along(check_defaults)) { td <- td_defaults[[check_defaults[i]]] From 474e24450dea69f3b5b71f93b8bf02c03c313b38 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Wed, 27 Mar 2024 10:00:48 -0500 Subject: [PATCH 8/8] Fixes issue on CI, updates Rds --- R/backend-openai-core.R | 3 +++ man/chattr_app.Rd | 4 ++-- man/chattr_defaults.Rd | 16 +++++++++------- man/chattr_defaults_save.Rd | 7 ++++--- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/R/backend-openai-core.R b/R/backend-openai-core.R index 5f7df9b..62c6dfa 100644 --- a/R/backend-openai-core.R +++ b/R/backend-openai-core.R @@ -43,6 +43,9 @@ openai_token_copilot <- function(defaults = NULL, fail = TRUE) { ) ) } + if(is.null(hosts_path)) { + return(NULL) + } gh_path <- path_expand(hosts_path) if (dir_exists(gh_path)) { hosts <- jsonlite::read_json(path(gh_path, "hosts.json")) diff --git a/man/chattr_app.Rd b/man/chattr_app.Rd index 9a84072..d737206 100644 --- a/man/chattr_app.Rd +++ b/man/chattr_app.Rd @@ -21,8 +21,8 @@ the document, or console, in the IDE.} \item{as_job_port}{Port to use for the Shiny app. Applicable only if \code{as_job} is set to TRUE.} -\item{as_job_host}{Host IP to use for the Shiny app. Applicable only if \code{as_job} -is set to TRUE.} +\item{as_job_host}{Host IP to use for the Shiny app. Applicable only if +\code{as_job} is set to TRUE.} } \value{ A chat interface inside the 'RStudio' IDE diff --git a/man/chattr_defaults.Rd b/man/chattr_defaults.Rd index 9ccb83c..ab8bde9 100644 --- a/man/chattr_defaults.Rd +++ b/man/chattr_defaults.Rd @@ -23,8 +23,8 @@ chattr_defaults( ) } \arguments{ -\item{type}{Entry point to interact with the model. Accepted values: 'notebook', -'chat'} +\item{type}{Entry point to interact with the model. Accepted values: +'notebook', chat'} \item{prompt}{Request to send to LLM. Defaults to NULL} @@ -47,8 +47,8 @@ is available} \item{model}{The name or path to the model to use.} -\item{model_arguments}{Additional arguments to pass to the model as part of the -request, it requires a list. Examples of arguments: temperature, top_p, +\item{model_arguments}{Additional arguments to pass to the model as part of +the request, it requires a list. Examples of arguments: temperature, top_p, max_tokens} \item{system_msg}{For OpenAI GPT 3.5 or above, the system message to send as @@ -61,7 +61,8 @@ defaults to use in a session} \item{label}{Label to display in the Shiny app, and other locations} -\item{...}{Additional model arguments that are not standard for all models/backends} +\item{...}{Additional model arguments that are not standard for all +models/backends} } \value{ An 'ch_model' object that contains the current defaults that will be @@ -74,6 +75,7 @@ Default arguments to use when making requests to the LLM The idea is that because we will use addin shortcut to execute the request, all of the other arguments can be controlled via this function. By default, it will try to load defaults from a \code{config} YAML file, if none are -found, then the defaults for GPT 3.5 will be used. The defaults can be modified -by calling this function, even after the interactive session has started. +found, then the defaults for GPT 3.5 will be used. The defaults can be +modified by calling this function, even after the interactive session has +started. } diff --git a/man/chattr_defaults_save.Rd b/man/chattr_defaults_save.Rd index 02700f5..232bdef 100644 --- a/man/chattr_defaults_save.Rd +++ b/man/chattr_defaults_save.Rd @@ -12,11 +12,12 @@ chattr_defaults_save(path = "chattr.yml", overwrite = FALSE, type = NULL) \item{overwrite}{Indicates to replace the file if it exists} -\item{type}{The type of UI to save the defaults for. It defaults to NULL which -will save whatever types had been used during the current R session} +\item{type}{The type of UI to save the defaults for. It defaults to NULL +which will save whatever types had been used during the current R session} } \value{ -It creates a YAML file with the defaults set in the current R session. +It creates a YAML file with the defaults set in the current R +session. } \description{ Saves the current defaults in a yaml file that is compatible with