-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
1b10fc6
commit 07de5b0
Showing
1 changed file
with
30 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,41 @@ | ||
req_mistral_perform <- function(req, error_call = caller_env()) { | ||
|
||
url <- req$url | ||
|
||
handle_invalid_model_error <- function(err, resp) { | ||
status <- resp_status(resp) | ||
if (status == 400 && resp_body_json(resp)$type == "invalid_model") { | ||
model <- req$body$data$model | ||
cli_abort(c( | ||
"Invalid mistrai.ai model {.emph {model}}.", | ||
i = "Use one of {.or {models()}}." | ||
), call = error_call) | ||
withCallingHandlers( | ||
req_perform(req), | ||
error = function(err) { | ||
resp <- err$resp | ||
handle_invalid_model_error(err, req, resp, error_call = error_call) | ||
handle_req_perform_error(err, req, resp, error_call = error_call) | ||
} | ||
} | ||
) | ||
} | ||
|
||
handle_req_perform_error <- function(err, resp) { | ||
bullets <- c(x = "with endpoint {.url {url}}") | ||
handle_invalid_model_error <- function(err, req, resp, error_call = caller_env()) { | ||
status <- resp_status(resp) | ||
if (status == 400 && resp_body_json(resp)$type == "invalid_model") { | ||
model <- req$body$data$model | ||
cli_abort(c( | ||
"Invalid mistrai.ai model {.emph {model}}.", | ||
i = "Use one of {.or {models()}}." | ||
), call = error_call) | ||
} | ||
} | ||
|
||
if (!inherits(err, "error_mistral_req_perform")) { | ||
bullets <- c( | ||
bullets, | ||
i = "Make sure your api key is valid {.url https://console.mistral.ai/api-keys/}", | ||
i = "And set the {.envvar MISTRAL_API_KEY} environment variable", | ||
i = "Perhaps using {.fn usethis::edit_r_environ}" | ||
) | ||
} | ||
handle_req_perform_error <- function(err, req, resp, error_call = caller_env()) { | ||
url <- req$url | ||
bullets <- c(x = "with endpoint {.url {url}}") | ||
|
||
cli_abort( | ||
bullets, class = c("error_mistral_req_perform"), | ||
call = error_call, parent = err | ||
if (!inherits(err, "error_mistral_req_perform")) { | ||
bullets <- c( | ||
bullets, | ||
i = "Make sure your api key is valid {.url https://console.mistral.ai/api-keys/}", | ||
i = "And set the {.envvar MISTRAL_API_KEY} environment variable", | ||
i = "Perhaps using {.fn usethis::edit_r_environ}" | ||
) | ||
} | ||
|
||
withCallingHandlers( | ||
req_perform(req), | ||
error = function(err) { | ||
resp <- err$resp | ||
handle_invalid_model_error(err, resp) | ||
handle_req_perform_error(err, resp) | ||
} | ||
cli_abort( | ||
bullets, class = c("error_mistral_req_perform"), | ||
call = error_call, parent = err | ||
) | ||
} |