From dcce7d0e49e3003722ed8e54db3e5e7dda100060 Mon Sep 17 00:00:00 2001 From: Romain Francois <romain@tada.science> Date: Sat, 6 Apr 2024 22:01:42 +0200 Subject: [PATCH 1/4] %!% \(err) --- R/chat.R | 11 ----------- R/httr2.R | 16 +++++----------- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/R/chat.R b/R/chat.R index a42d32a..2ae70d2 100644 --- a/R/chat.R +++ b/R/chat.R @@ -24,17 +24,6 @@ chat <- function(messages, model = "mistral-tiny", ..., error_call = current_env resp <- authenticate(req, error_call = error_call) |> req_mistral_perform(error_call = error_call) - - req_messages <- x$request$body$data$messages - df_req <- map_dfr(req_messages, as.data.frame) - - df_resp <- as.data.frame( - resp_body_json(x)$choices[[1]]$message[c("role", "content")] - ) - - rbind(df_req, df_resp) - - class(resp) <- c("chat", class(resp)) resp } diff --git a/R/httr2.R b/R/httr2.R index dd370ee..ddb5506 100644 --- a/R/httr2.R +++ b/R/httr2.R @@ -1,25 +1,19 @@ req_mistral_perform <- function(req, error_call = caller_env()) { - - withCallingHandlers( - req_perform(req), - error = function(err) { - resp <- err$resp - handler <- mistral_error_handler(err, resp, req) - handler(err, req, resp, error_call = error_call) - } - ) + req_perform(req) %!% \(err) handle_mistral_error(err, req, error_call) } -mistral_error_handler <- function(err, resp, req) { +handle_mistral_error <- function(err, req, error_call) { + resp <- err$resp status <- resp_status(resp) - if (status == 401) { + handler <- if (status == 401) { handle_unauthorized } else if (status == 400 && resp_body_json(resp)$type == "invalid_model") { handle_invalid_model } else { handle_other } + handler(err, req, resp, error_call = error_call) } handle_invalid_model <- function(err, req, resp, error_call = caller_env()) { From 7e167f899c9e10b743dbf981339c1c8cbbf5e0b4 Mon Sep 17 00:00:00 2001 From: Romain Francois <romain@tada.science> Date: Sat, 6 Apr 2024 23:35:23 +0200 Subject: [PATCH 2/4] simplify --- R/httr2.R | 61 +++++++++++++++++++------------------------------------ 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/R/httr2.R b/R/httr2.R index ddb5506..d493c42 100644 --- a/R/httr2.R +++ b/R/httr2.R @@ -1,48 +1,29 @@ req_mistral_perform <- function(req, error_call = caller_env()) { - req_perform(req) %!% \(err) handle_mistral_error(err, req, error_call) -} -handle_mistral_error <- function(err, req, error_call) { - resp <- err$resp - status <- resp_status(resp) + handle_mistral_error <- function(err) { + resp <- err$resp + url <- req$url + status <- resp_status(resp) - handler <- if (status == 401) { - handle_unauthorized - } else if (status == 400 && resp_body_json(resp)$type == "invalid_model") { - handle_invalid_model - } else { - handle_other - } - handler(err, req, resp, error_call = error_call) -} + bullets <- if (status == 401) { + c( + "Unauthorized {.url {url}}.", + 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}" + ) + } else if (status == 400 && resp_body_json(resp)$type == "invalid_model") { + c( + "Invalid mistrai.ai model {.emph {model}}.", + i = "Use one of {.or {models()}}." + ) + } else { + "Error with {.url {url}}." + } -handle_invalid_model <- 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) - } -} - -handle_unauthorized <- function(err, req, resp, error_call = caller_env()) { - status <- resp_status(resp) - url <- req$url - - if (status == 401) { - bullets <- c( - "Unauthorized {.url {url}}.", - 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}" - ) cli_abort(bullets, call = error_call) } -} -handle_other <- function(err, req, resp, error_call = caller_env()) { - url <- req$url - cli_abort("Error with {.url {url}}.", call = error_call, parent = err) + req_perform(req) %!% handle_mistral_error } + From cbaf9ad0f6fd3f43b52bc7be94164505168c93b2 Mon Sep 17 00:00:00 2001 From: Romain Francois <romain@tada.science> Date: Sat, 20 Apr 2024 09:16:43 +0200 Subject: [PATCH 3/4] hdnale_mistral_error() back in --- R/httr2.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/httr2.R b/R/httr2.R index d493c42..a50b44d 100644 --- a/R/httr2.R +++ b/R/httr2.R @@ -1,6 +1,7 @@ req_mistral_perform <- function(req, error_call = caller_env()) { handle_mistral_error <- function(err) { + req <- err$req resp <- err$resp url <- req$url status <- resp_status(resp) @@ -26,4 +27,3 @@ req_mistral_perform <- function(req, error_call = caller_env()) { req_perform(req) %!% handle_mistral_error } - From 341eb1a4299e86712e223cc163ef84e6b62b611b Mon Sep 17 00:00:00 2001 From: Romain Francois <romain@tada.science> Date: Sat, 20 Apr 2024 09:21:24 +0200 Subject: [PATCH 4/4] imporve msg --- R/httr2.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/R/httr2.R b/R/httr2.R index a50b44d..2ba7a36 100644 --- a/R/httr2.R +++ b/R/httr2.R @@ -14,9 +14,10 @@ req_mistral_perform <- function(req, error_call = caller_env()) { i = "Perhaps using {.fn usethis::edit_r_environ}" ) } else if (status == 400 && resp_body_json(resp)$type == "invalid_model") { + model <- req$body$data$model c( - "Invalid mistrai.ai model {.emph {model}}.", - i = "Use one of {.or {models()}}." + "Invalid mistrai.ai model {.val {model}}.", + i = "Available models: {.val {models()}}." ) } else { "Error with {.url {url}}."