Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for interactive Entra ID authentication to chat_azure() #273

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
* The `token` argument to `chat_azure()` has been deprecated. Use ambient
credentials or the `credentials` argument instead (#257, @atheriel).

* `chat_azure()` attempts to use interactive Entra ID authentication if no other
credentials are available (#273, @atheriel).

# ellmer 0.1.0

* New `chat_vllm()` to chat with models served by vLLM (#140).
Expand Down
51 changes: 50 additions & 1 deletion R/provider-azure.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ NULL
#' `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, and `AZURE_CLIENT_SECRET` environment
#' variables are set.
#'
#' Finally, in interactive sessions it will also attempt to use Microsoft Entra
#' ID authentication -- much like the Azure CLI -- if no API key has been
#' provided.
#'
#' @param endpoint Azure OpenAI endpoint url with protocol and hostname, i.e.
#' `https://{your-resource-name}.openai.azure.com`. Defaults to using the
#' value of the `AZURE_OPENAI_ENDPOINT` envinronment variable.
Expand Down Expand Up @@ -137,7 +141,27 @@ method(chat_request, ProviderAzure) <- function(provider,
req <- req_retry(req, max_tries = 2)
req <- req_error(req, body = function(resp) {
error <- resp_body_json(resp)$error
paste0(error$code, ": ", error$message)
msg <- paste0(error$code, ": ", error$message)
# Try to be helpful in the (common) case that the user or service
# principal is missing the necessary role.
# See: https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/role-based-access-control
bad_rbac <- identical(
error$message,
"Principal does not have access to API/Operation."
)
if (bad_rbac) {
msg <- c(
"*" = msg,
"i" = cli::format_inline(
"Your user or service principal likely needs one of the following
roles: {.emph Cognitive Services OpenAI User},
{.emph Cognitive Services OpenAI Contributor}, or
{.emph Cognitive Services Contributor}.",
keep_whitespace = FALSE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need that? I think it will get re-wrapped anyway?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing it doesn't get re-wrapped without this 😭.

)
)
}
msg
})

messages <- compact(unlist(as_json(provider, turns), recursive = FALSE))
Expand Down Expand Up @@ -214,6 +238,31 @@ default_azure_credentials <- function(api_key = NULL, token = NULL) {
return(function() list())
}

# Masquerade as the Azure CLI.
client_id <- "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
if (is_interactive() && !is_hosted_session()) {
client <- oauth_client(
client_id,
token_url = "https://login.microsoftonline.com/common/oauth2/v2.0/token",
secret = "",
auth = "body",
name = paste0("ellmer-", client_id)
)
return(function() {
token <- oauth_token_cached(
client,
oauth_flow_auth_code,
flow_params = list(
auth_url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
scope = "https://cognitiveservices.azure.com/.default offline_access",
redirect_uri = "http://localhost:8400",
auth_params = list(prompt = "select_account")
)
)
list(Authorization = paste("Bearer", token$access_token))
})
}

if (is_testing()) {
testthat::skip("no Azure credentials available")
}
Expand Down
4 changes: 4 additions & 0 deletions man/chat_azure.Rd

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

Loading