Skip to content

Commit

Permalink
Merge pull request #88 from mlverse/llama2
Browse files Browse the repository at this point in the history
Llama2
  • Loading branch information
edgararuiz authored Apr 10, 2024
2 parents 6688b5c + 9266a4f commit 0d916d1
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 43 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre")),
person(given = "Posit Software, PBC", role = c("cph", "fnd"))
Expand Down
2 changes: 1 addition & 1 deletion R/app-server.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 8 additions & 14 deletions R/chattr-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,23 @@ chattr_defaults <- function(type = "default",
if (force) {
ch_env$defaults <- NULL
}

env_model <- NULL
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)) {
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]]]
Expand Down
43 changes: 25 additions & 18 deletions R/chattr-use.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,17 +25,17 @@
#' @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)) {
x <- path_expand(x)
} else {
env_folder <- "configs"
x <- ch_package_file(x)
}
use_switch(env_folder, path_ext_set(model_label, "yml"))
use_switch(.file = x, ...)
}

ch_get_ymls <- function(menu = TRUE) {
Expand Down Expand Up @@ -121,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 <- package_file(...)

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
)

Expand All @@ -143,7 +142,7 @@ use_switch <- function(...) {
~ {
chattr_defaults(
type = .x,
yaml_file = file
yaml_file = .file
)
}
)
Expand All @@ -152,5 +151,13 @@ use_switch <- function(...) {

cli_div(theme = cli_colors())
cli_h3("chattr")
print_provider(chattr_defaults())
print_provider(chattr_defaults(...))
}

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"))
}
8 changes: 4 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions man/chattr_use.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/chattr-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/_snaps/chattr-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

11 changes: 11 additions & 0 deletions tests/testthat/test-chattr-use.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
})

0 comments on commit 0d916d1

Please sign in to comment.