Skip to content

Commit

Permalink
Move cortex_credentials() out of the public API. (#218)
Browse files Browse the repository at this point in the history
This commit parcels out the refactoring portion of #183 so that we don't
need to make breaking changes to the CRAN release.

Signed-off-by: Aaron Jacobs <[email protected]>
  • Loading branch information
atheriel authored Dec 13, 2024
1 parent f9474f6 commit 0a83696
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export(content_image_url)
export(contents_html)
export(contents_markdown)
export(contents_text)
export(cortex_credentials)
export(create_tool_def)
export(interpolate)
export(interpolate_file)
Expand Down
51 changes: 26 additions & 25 deletions R/provider-cortex.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ NULL
#' previous messages. Nor does it support registering tools, and attempting to
#' do so will result in an error.
#'
#' ## Authentication
#'
#' `chat_cortex()` picks up the following ambient Snowflake credentials:
#'
#' - A static OAuth token defined via the `SNOWFLAKE_TOKEN` environment
#' variable.
#' - Key-pair authentication credentials defined via the `SNOWFLAKE_USER` and
#' `SNOWFLAKE_PRIVATE_KEY` (which can be a PEM-encoded private key or a path
#' to one) environment variables.
#' - Posit Workbench-managed Snowflake credentials for the corresponding
#' `account`.
#'
#' @param account A Snowflake [account identifier](https://docs.snowflake.com/en/user-guide/admin-account-identifier),
#' e.g. `"testorg-test_account"`.
#' @param credentials A list of authentication headers to pass into
#' [`httr2::req_headers()`] or a function that returns them when passed
#' `account` as a parameter. The default [`cortex_credentials()`] function
#' picks up ambient Snowflake OAuth and key-pair authentication credentials
#' and handles refreshing them automatically.
#' [`httr2::req_headers()`], a function that returns them when passed
#' `account` as a parameter, or `NULL` to use ambient credentials.
#' @param model_spec A semantic model specification, or `NULL` when
#' using `model_file` instead.
#' @param model_file Path to a semantic model file stored in a Snowflake Stage,
Expand All @@ -41,7 +51,7 @@ NULL
#' chat$chat("What questions can I ask?")
#' @export
chat_cortex <- function(account = Sys.getenv("SNOWFLAKE_ACCOUNT"),
credentials = cortex_credentials,
credentials = NULL,
model_spec = NULL,
model_file = NULL,
api_args = list(),
Expand All @@ -56,7 +66,7 @@ chat_cortex <- function(account = Sys.getenv("SNOWFLAKE_ACCOUNT"),
static_credentials <- force(credentials)
credentials <- function(account) static_credentials
}
check_function(credentials)
check_function(credentials, allow_null = TRUE)

provider <- ProviderCortex(
account = account,
Expand Down Expand Up @@ -88,7 +98,7 @@ ProviderCortex <- new_class(
},
properties = list(
account = prop_string(),
credentials = class_function,
credentials = class_function | NULL,
extra_args = class_list
)
)
Expand All @@ -110,9 +120,8 @@ method(chat_request, ProviderCortex) <- function(provider,

req <- request(provider@base_url)
req <- req_url_path_append(req, "/api/v2/cortex/analyst/message")
req <- httr2::req_headers(req,
!!!provider@credentials(provider@account), .redact = "Authorization"
)
creds <- cortex_credentials(provider@account, provider@credentials)
req <- httr2::req_headers(req, !!!creds, .redact = "Authorization")
req <- req_retry(req, max_tries = 2)
req <- req_timeout(req, 60)

Expand Down Expand Up @@ -356,21 +365,13 @@ cortex_credentials_exist <- function(...) {
tryCatch(is_list(cortex_credentials(...)), error = function(e) FALSE)
}

#' @details
#' `cortex_credentials()` picks up the following ambient Snowflake credentials:
#'
#' - A static OAuth token defined via the `SNOWFLAKE_TOKEN` environment
#' variable.
#' - Key-pair authentication credentials defined via the `SNOWFLAKE_USER` and
#' `SNOWFLAKE_PRIVATE_KEY` (which can be a PEM-encoded private key or a path
#' to one) environment variables.
#' - Posit Workbench-managed Snowflake credentials for the corresponding
#' `account`.
#'
#' @inheritParams chat_cortex
#' @export
#' @rdname chat_cortex
cortex_credentials <- function(account = Sys.getenv("SNOWFLAKE_ACCOUNT")) {
cortex_credentials <- function(account = Sys.getenv("SNOWFLAKE_ACCOUNT"),
credentials = NULL) {
# User-supplied credentials.
if (!is.null(credentials)) {
return(credentials(account))
}

token <- Sys.getenv("SNOWFLAKE_TOKEN")
if (nchar(token) != 0) {
return(
Expand Down
2 changes: 2 additions & 0 deletions R/provider-databricks.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#' outputs. Tool calling support is also very limited at present; too limited
#' for `elmer`'s tool calling features to work properly at all.
#'
#' ## Authentication
#'
#' `chat_databricks()` picks up on ambient Databricks credentials for a subset
#' of the [Databricks client unified
#' authentication](https://docs.databricks.com/en/dev-tools/auth/unified-auth.html)
Expand Down
18 changes: 7 additions & 11 deletions man/chat_cortex.Rd

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

2 changes: 2 additions & 0 deletions man/chat_databricks.Rd

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

0 comments on commit 0a83696

Please sign in to comment.