Skip to content

Commit

Permalink
Create helper for getting key & checking for existance (#34)
Browse files Browse the repository at this point in the history
Mostly prospective change to better support incoming gemini and claude wrappers.
  • Loading branch information
hadley authored Sep 23, 2024
1 parent a499147 commit 9ad4efd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
20 changes: 2 additions & 18 deletions R/api-openai.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,6 @@ openai_result_message <- function(result, streaming) {
}
}

openai_key <- function() {
key <- Sys.getenv("OPENAI_API_KEY")
if (identical(key, "")) {
cli::cli_abort("Can't find env var {.code OPENAI_API_KEY}.")
}
key
}

# https://platform.openai.com/docs/api-reference/chat/create
openai_chat_req <- function(messages,
tools = list(),
Expand Down Expand Up @@ -127,17 +119,9 @@ openai_request <- function(base_url = "https://api.openai.com/v1",
}

openai_key_exists <- function() {
!identical(Sys.getenv("OPENAI_API_KEY"), "")
key_exists("OPENAI_API_KEY")
}

openai_key <- function() {
if (openai_key_exists()) {
Sys.getenv("OPENAI_API_KEY")
} else {
if (is_testing()) {
testthat::skip("OPENAI_API_KEY env var is not configured")
} else {
cli::cli_abort("Can't find env var {.code OPENAI_API_KEY}.")
}
}
key_get("OPENAI_API_KEY")
}
17 changes: 17 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
is_testing <- function() {
identical(Sys.getenv("TESTTHAT"), "true")
}

key_get <- function(name, error_call = caller_env()) {
val <- Sys.getenv(name)
if (!identical(val, "")) {
val
} else {
if (is_testing()) {
testthat::skip(sprintf("%s env var is not configured", name))
} else {
cli::cli_abort("Can't find env var {.code {name}}.", call = error_call)
}
}
}

key_exists <- function(name) {
!identical(Sys.getenv(name), "")
}
8 changes: 0 additions & 8 deletions tests/testthat/_snaps/api-openai.md

This file was deleted.

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# informative error if no key

Code
key_get("FOO")
Condition
Error:
! Can't find env var `FOO`.

5 changes: 0 additions & 5 deletions tests/testthat/test-api-openai.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
test_that("informative error if no key", {
withr::local_envvar(OPENAI_API_KEY = NULL, TESTTHAT = "false")
expect_snapshot(openai_key(), error = TRUE)
})

test_that("system prompt is applied correctly", {
sys_prompt <- "foo"
sys_msg <- list(role = "system", content = sys_prompt)
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
test_that("finds key if set", {
withr::local_envvar(FOO = "abc123")
expect_true(key_exists("FOO"))
expect_equal(key_get("FOO"), "abc123")
})


test_that("informative error if no key", {
withr::local_envvar(FOO = NULL, TESTTHAT = "false")
expect_false(key_exists("FOO"))
expect_snapshot(key_get("FOO"), error = TRUE)
})

0 comments on commit 9ad4efd

Please sign in to comment.