From 28e71e57c8ca2e34aada0e355c458d6f563c14c5 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Tue, 9 Apr 2024 18:13:55 -0500 Subject: [PATCH 1/7] Updates args for chattr_use(), starts support for paths --- R/chattr-use.R | 28 +++++++++++++++++----------- man/chattr_use.Rd | 11 +++++++---- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/R/chattr-use.R b/R/chattr-use.R index 5d9a985..970794a 100644 --- a/R/chattr-use.R +++ b/R/chattr-use.R @@ -1,7 +1,9 @@ #' Sets the LLM model to use in your session -#' @param model_label The label of the LLM model to use. Valid values are -#' 'copilot', 'gpt4', 'gpt35', and 'llamagpt'. The value 'test' is also -#' acceptable, but it is meant for package examples, and internal testing. +#' @param x The label of the LLM model to use, or the path of a valid YAML +#' default file . Valid values are 'copilot', 'gpt4', 'gpt35', and 'llamagpt'. +#' The value 'test' is also acceptable, but it is meant for package examples, +#' and internal testing. +#' @param ... Default values to modify. #' @details #' If the error "No model setup found" was returned, that is because none of the #' expected setup for Copilot, OpenAI or LLama was automatically detected. Here @@ -23,17 +25,21 @@ #' @returns It returns console messages to allow the user select the model to #' use. #' @export -chattr_use <- function(model_label = NULL) { - interactive_label <- is_interactive() && is.null(model_label) +chattr_use <- function(x = NULL, ...) { + interactive_label <- is_interactive() && is.null(x) if (interactive_label) { - model_label <- ch_get_ymls() + x <- ch_get_ymls() } - if (model_label == "test") { - env_folder <- "apptest" + if (is_file(x)) { + use_switch(path_expand(x)) } else { - env_folder <- "configs" + stop() + env_folder <- ifelse(x == "test", "apptest", "configs") + env_folder %>% + package_file(path_ext_set(x, "yml")) %>% + use_switch() } - use_switch(env_folder, path_ext_set(model_label, "yml")) + chattr_defaults(...) } ch_get_ymls <- function(menu = TRUE) { @@ -124,7 +130,7 @@ ch_get_ymls <- function(menu = TRUE) { use_switch <- function(...) { ch_env$defaults <- NULL ch_env$chat_history <- NULL - file <- package_file(...) + file <- c(...) label <- file %>% path_file() %>% diff --git a/man/chattr_use.Rd b/man/chattr_use.Rd index 997534b..223401c 100644 --- a/man/chattr_use.Rd +++ b/man/chattr_use.Rd @@ -4,12 +4,15 @@ \alias{chattr_use} \title{Sets the LLM model to use in your session} \usage{ -chattr_use(model_label = NULL) +chattr_use(x = NULL, ...) } \arguments{ -\item{model_label}{The label of the LLM model to use. Valid values are -'copilot', 'gpt4', 'gpt35', and 'llamagpt'. The value 'test' is also -acceptable, but it is meant for package examples, and internal testing.} +\item{x}{The label of the LLM model to use, or the path of a valid YAML +default file . Valid values are 'copilot', 'gpt4', 'gpt35', and 'llamagpt'. +The value 'test' is also acceptable, but it is meant for package examples, +and internal testing.} + +\item{...}{Default values to modify.} } \value{ It returns console messages to allow the user select the model to From 16d80876bc6b62134bb9c80341b65998e6d66148 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Wed, 10 Apr 2024 09:12:31 -0500 Subject: [PATCH 2/7] Fixes output --- R/chattr-defaults.R | 11 ----------- R/chattr-use.R | 3 +-- R/utils.R | 8 ++++---- tests/testthat/_snaps/chattr-test.md | 2 +- 4 files changed, 6 insertions(+), 18 deletions(-) diff --git a/R/chattr-defaults.R b/R/chattr-defaults.R index e300fc8..6220dae 100644 --- a/R/chattr-defaults.R +++ b/R/chattr-defaults.R @@ -77,17 +77,6 @@ chattr_defaults <- function(type = "default", } if (is.null(chattr_defaults_get(type))) { - # Uses environment variable if set - env_model <- Sys.getenv("CHATTR_MODEL", unset = NA) - check_files <- NULL - if (!is.na(env_model)) { - if (env_model == "test") { - env_folder <- "apptest" - } else { - env_folder <- "configs" - } - check_files <- package_file(env_folder, path_ext_set(env_model, "yml")) - } # Overrides environment variable if YAML file is present if (file_exists(yaml_file)) { diff --git a/R/chattr-use.R b/R/chattr-use.R index 970794a..08bce02 100644 --- a/R/chattr-use.R +++ b/R/chattr-use.R @@ -33,13 +33,12 @@ chattr_use <- function(x = NULL, ...) { if (is_file(x)) { use_switch(path_expand(x)) } else { - stop() env_folder <- ifelse(x == "test", "apptest", "configs") env_folder %>% package_file(path_ext_set(x, "yml")) %>% use_switch() } - chattr_defaults(...) + invisible(chattr_defaults(...)) } ch_get_ymls <- function(menu = TRUE) { diff --git a/R/utils.R b/R/utils.R index 7cd23af..0404ed2 100644 --- a/R/utils.R +++ b/R/utils.R @@ -105,10 +105,10 @@ ui_validate <- function(x) { print_provider <- function(x) { cli_div(theme = cli_colors()) - cli_li("{.val0 Provider:} {.val1 {x$provider}}") - cli_li("{.val0 Path/URL:} {.val1 {x$path}}") - cli_li("{.val0 Model:} {.val1 {x$model}}") - cli_li("{.val0 Label:} {.val1 {x$label}}") + cli_li("{.val0 Provider:} {.val1 {x[['provider']]}}") + cli_li("{.val0 Path/URL:} {.val1 {x[['path']]}}") + cli_li("{.val0 Model:} {.val1 {x[['model']]}}") + cli_li("{.val0 Label:} {.val1 {x[['label']]}}") } # ------------------------ Determine OS ---------------------------------------- diff --git a/tests/testthat/_snaps/chattr-test.md b/tests/testthat/_snaps/chattr-test.md index 8621341..8cf8398 100644 --- a/tests/testthat/_snaps/chattr-test.md +++ b/tests/testthat/_snaps/chattr-test.md @@ -72,7 +72,7 @@ -- Testing chattr * Provider: OpenAI - GitHub Copilot Chat * Path/URL: https://api.githubcopilot.com/chat/completions - * Model: TRUE + * Model: * Label: Copilot (GitHub) v Connection with GitHub Copilot cofirmed |--Prompt: TEST From a9df2567f608d5df47ccb4d50a9e279068ec201b Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Wed, 10 Apr 2024 09:31:06 -0500 Subject: [PATCH 3/7] Fixes test switching issue, ver bump --- DESCRIPTION | 2 +- R/app-server.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e5e210e..6feca8f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: chattr Title: Integrates LLM's with the RStudio IDE -Version: 0.0.0.9011 +Version: 0.0.0.9012 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/app-server.R b/R/app-server.R index 7f0563e..21377cd 100644 --- a/R/app-server.R +++ b/R/app-server.R @@ -5,7 +5,7 @@ app_server <- function(input, output, session) { app_add_history(input) is_test <- unlist(options("chattr-shiny-test")) %||% FALSE if (is_test) { - use_switch("apptest", path_ext_set("test", "yml")) + chattr_use("test") invalidate_time <- 1000 } else { invalidate_time <- 100 From cc1f617b1b8610cdb0d6555b2f7585899e49747e Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Wed, 10 Apr 2024 12:20:43 -0500 Subject: [PATCH 4/7] Makes sure model label and path works --- R/chattr-use.R | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/R/chattr-use.R b/R/chattr-use.R index 08bce02..faf347c 100644 --- a/R/chattr-use.R +++ b/R/chattr-use.R @@ -31,14 +31,11 @@ chattr_use <- function(x = NULL, ...) { x <- ch_get_ymls() } if (is_file(x)) { - use_switch(path_expand(x)) + x <- path_expand(x) } else { - env_folder <- ifelse(x == "test", "apptest", "configs") - env_folder %>% - package_file(path_ext_set(x, "yml")) %>% - use_switch() + x <- ch_package_file(x) } - invisible(chattr_defaults(...)) + use_switch(.file = x, ...) } ch_get_ymls <- function(menu = TRUE) { @@ -126,20 +123,17 @@ ch_get_ymls <- function(menu = TRUE) { } } -use_switch <- function(...) { +use_switch <- function(..., .file) { ch_env$defaults <- NULL ch_env$chat_history <- NULL - file <- c(...) - label <- file %>% + label <- .file %>% path_file() %>% path_ext_remove() - Sys.setenv("CHATTR_MODEL" = label) - chattr_defaults( type = "default", - yaml_file = file, + yaml_file = .file, force = TRUE ) @@ -148,7 +142,7 @@ use_switch <- function(...) { ~ { chattr_defaults( type = .x, - yaml_file = file + yaml_file = .file ) } ) @@ -157,5 +151,10 @@ use_switch <- function(...) { cli_div(theme = cli_colors()) cli_h3("chattr") - print_provider(chattr_defaults()) + print_provider(chattr_defaults(...)) +} + +ch_package_file <- function(x) { + env_folder <- ifelse(x == "test", "apptest", "configs") + package_file(env_folder, path_ext_set(x, "yml")) } From 95b04049139a0e6dd5843cbe5157da0dfe941856 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Wed, 10 Apr 2024 12:30:44 -0500 Subject: [PATCH 5/7] Starts support of CHATTR_USE --- R/chattr-defaults.R | 2 ++ R/chattr-use.R | 1 + 2 files changed, 3 insertions(+) diff --git a/R/chattr-defaults.R b/R/chattr-defaults.R index 6220dae..3487249 100644 --- a/R/chattr-defaults.R +++ b/R/chattr-defaults.R @@ -81,6 +81,8 @@ chattr_defaults <- function(type = "default", # Overrides environment variable if YAML file is present if (file_exists(yaml_file)) { check_files <- yaml_file + } else { + check_files <- ch_package_file(Sys.getenv("CHATTR_USE", unset = NA)) } for (j in seq_along(check_files)) { diff --git a/R/chattr-use.R b/R/chattr-use.R index faf347c..f05ec20 100644 --- a/R/chattr-use.R +++ b/R/chattr-use.R @@ -155,6 +155,7 @@ use_switch <- function(..., .file) { } ch_package_file <- function(x) { + if(is.na(x)) return(NULL) env_folder <- ifelse(x == "test", "apptest", "configs") package_file(env_folder, path_ext_set(x, "yml")) } From 86c1681cd81c126f9aa8bf90a090a2b267c345c2 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Wed, 10 Apr 2024 12:58:38 -0500 Subject: [PATCH 6/7] Adds support for CHATTR_MODEL --- R/chattr-defaults.R | 9 ++++++--- R/chattr-use.R | 4 +++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/R/chattr-defaults.R b/R/chattr-defaults.R index 3487249..a98d3cb 100644 --- a/R/chattr-defaults.R +++ b/R/chattr-defaults.R @@ -75,20 +75,23 @@ chattr_defaults <- function(type = "default", if (force) { ch_env$defaults <- NULL } - + env_model <- NULL if (is.null(chattr_defaults_get(type))) { - # Overrides environment variable if YAML file is present if (file_exists(yaml_file)) { check_files <- yaml_file } else { check_files <- ch_package_file(Sys.getenv("CHATTR_USE", unset = NA)) } - + env_model <- Sys.getenv("CHATTR_MODEL", unset = NA) + if(is.na(env_model)) { + env_model <- NULL + } for (j in seq_along(check_files)) { td_defaults <- read_yaml(file = check_files[j]) loaded_default <- chattr_defaults_get(type = "default") td_defaults$default <- loaded_default %||% td_defaults$default + td_defaults$default[["model"]] <- env_model %||% td_defaults$default[["model"]] check_defaults <- c("default", type) for (i in seq_along(check_defaults)) { td <- td_defaults[[check_defaults[i]]] diff --git a/R/chattr-use.R b/R/chattr-use.R index f05ec20..ce76be8 100644 --- a/R/chattr-use.R +++ b/R/chattr-use.R @@ -155,7 +155,9 @@ use_switch <- function(..., .file) { } ch_package_file <- function(x) { - if(is.na(x)) return(NULL) + if (is.na(x)) { + return(NULL) + } env_folder <- ifelse(x == "test", "apptest", "configs") package_file(env_folder, path_ext_set(x, "yml")) } From 9266a4fddacf5654dd18bed7aea53d6ad6436716 Mon Sep 17 00:00:00 2001 From: Edgar Ruiz Date: Wed, 10 Apr 2024 13:03:22 -0500 Subject: [PATCH 7/7] Adds test for env vars --- tests/testthat/_snaps/chattr-use.md | 30 +++++++++++++++++++++++++++++ tests/testthat/test-chattr-use.R | 11 +++++++++++ 2 files changed, 41 insertions(+) diff --git a/tests/testthat/_snaps/chattr-use.md b/tests/testthat/_snaps/chattr-use.md index 3649b32..506f740 100644 --- a/tests/testthat/_snaps/chattr-use.md +++ b/tests/testthat/_snaps/chattr-use.md @@ -12,3 +12,33 @@ Output [1] "OpenAI - Chat Completions - gpt-4 (gpt4) \n" +# Menu works + + Code + chattr_defaults(force = TRUE) + Message + + -- chattr ---------------------------------------------------------------------- + + -- Defaults for: Default -- + + -- Prompt: + * Use the R language, the tidyverse, and tidymodels + + -- Model + * Provider: LlamaGPT + * Path/URL: ~/LlamaGPTJ-chat/build/bin/chat + * Model: test/path + * Label: GPT4ALL 1.3 (LlamaGPT) + + -- Model Arguments: + * threads: 4 + * temp: 0.01 + * n_predict: 1000 + + -- Context: + Max Data Files: 0 + Max Data Frames: 0 + x Chat History + x Document contents + diff --git a/tests/testthat/test-chattr-use.R b/tests/testthat/test-chattr-use.R index 6bb7895..5173b19 100644 --- a/tests/testthat/test-chattr-use.R +++ b/tests/testthat/test-chattr-use.R @@ -25,3 +25,14 @@ test_that("Menu works", { } ) }) + + +test_that("Menu works", { + withr::with_envvar( + new = c( + "CHATTR_USE" = "llamagpt", + "CHATTR_MODEL" = "test/path" + ), + expect_snapshot(chattr_defaults(force = TRUE)) + ) +})