Skip to content

Commit

Permalink
Adds tests for OpenAI switching mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
edgararuiz committed Mar 18, 2024
1 parent ceec15f commit 31fac5a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
13 changes: 8 additions & 5 deletions R/chattr-defaults.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ chattr_defaults <- function(type = "default",
)

ret <- chattr_defaults_get(type)
ret$type <- type
as_ch_model(ret, type)
}

as_ch_model <- function(x, type) {
x$type <- type

provider <- tolower(ret$provider)
provider <- tolower(x$provider)

sp_provider <- unlist(strsplit(provider, " - "))
if (length(sp_provider) > 1) {
Expand All @@ -117,13 +121,12 @@ chattr_defaults <- function(type = "default",
first_cl <- NULL
}

class(ret) <- c(
class(x) <- c(
paste0("ch_", prep_class_name(provider)),
first_cl,
"ch_model"
)

ret
x
}

prep_class_name <- function(x) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ch_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
Code
ch_context_data_files(file_types = "R")
Output
[1] "Data files available: \n|- setup.R\n|- test-app_server.R\n|- test-app_ui.R\n|- test-app_utils.R\n|- test-backend-llamagpt.R\n|- test-backend-openai-core.R\n|- test-backend-openai.R\n|- test-ch-defaults-save.R\n|- test-ch-history.R\n|- test-ch-submit.R\n|- test-ch_context.R\n|- test-ch_defaults.R\n|- test-chattr-test.R\n|- test-chattr-use.R\n|- test-chattr.R\n|- test-ide.R"
[1] "Data files available: \n|- setup.R\n|- test-app_server.R\n|- test-app_ui.R\n|- test-app_utils.R\n|- test-backend-llamagpt.R\n|- test-backend-openai-core.R\n|- test-backend-openai-switch.R\n|- test-backend-openai.R\n|- test-ch-defaults-save.R\n|- test-ch-history.R\n|- test-ch-submit.R\n|- test-ch_context.R\n|- test-ch_defaults.R\n|- test-chattr-test.R\n|- test-chattr-use.R\n|- test-chattr.R\n|- test-ide.R"

---

Expand Down
64 changes: 64 additions & 0 deletions tests/testthat/test-backend-openai-switch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
test_that("Using 'chat'", {
withr::with_envvar(
new = c("OPENAI_API_KEY" = "test"),
{
local_mocked_bindings(
req_perform = httr2::req_dry_run
)
defaults <- yaml::read_yaml(package_file("configs", "gpt35.yml"))
defaults <- as_ch_model(defaults$default, "chat")
expect_error(
openai_switch(
prompt = "test",
req_body = defaults$model_arguments,
defaults = defaults,
r_file_stream = tempfile(),
r_file_complete = tempfile()
)
)
}
)
})

test_that("Using 'console'", {
withr::with_envvar(
new = c("OPENAI_API_KEY" = "test"),
{
local_mocked_bindings(
req_perform_stream = function(...) {
ch_env$stream$response <- "test"
}
)
defaults <- yaml::read_yaml(package_file("configs", "gpt35.yml"))
defaults <- as_ch_model(defaults$default, "console")
expect_silent(
openai_switch(
prompt = "test",
req_body = defaults$model_arguments,
defaults = defaults,
r_file_stream = tempfile(),
r_file_complete = tempfile()
)
)
}
)
})



















0 comments on commit 31fac5a

Please sign in to comment.