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

using cli_abrt(parent=) for chained errors #15

Merged
merged 4 commits into from
Mar 9, 2024
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: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Version: 0.0.0.9000
Authors@R: c(
person("Romain", "François", email = "[email protected]", role = c("aut", "cre")),
person("Felix", "Mil", email = "[email protected]", role = "aut"),
person("James", "Wade", email = "[email protected]", role="aut")
person("James", "Wade", email = "[email protected]", role = "aut")
)
Description: Interact with the 'Mistral.AI' api.
License: MIT + file LICENSE
Expand All @@ -21,3 +21,4 @@ Imports:
stringr,
jsonlite,
rlang
Remotes: r-lib/httr2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ import(tibble)
importFrom(jsonlite,fromJSON)
importFrom(purrr,map_chr)
importFrom(purrr,map_dfr)
importFrom(purrr,pluck)
42 changes: 21 additions & 21 deletions R/chat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
#' Chat with the Mistral api
#'
#' @param text some text
#' @param model which model to use. See [models()] for more information about which models are available
#' @param ... ignored
#' @inheritParams httr2::req_perform
#'
#' @return Result text from Mistral
#'
#' @examples
#' \dontrun{
#' chat("Top 5 R packages")
#' }
#'
#' @export
chat <- function(text = "What are the top 5 R packages ?", model = "mistral-tiny", ..., error_call = current_env()) {
req_chat(text, model, error_call = error_call) |>
req_mistral_perform(error_call = error_call) |>
resp_chat(error_call = error_call)
}

req_chat <- function(text = "What are the top 5 R packages ?", model = "mistral-tiny", stream = FALSE, error_call = caller_env()) {
check_model(model, error_call = error_call)
request(mistral_base_url) |>
Expand Down Expand Up @@ -38,24 +59,3 @@ print.chat_tibble <- function(x, ...) {
}
invisible(x)
}

#' Chat with the Mistral api
#'
#' @param text some text
#' @param model which model to use. See [models()] for more information about which models are available
#' @param ... ignored
#' @inheritParams httr2::req_perform
#'
#' @return Result text from Mistral
#'
#' @examples
#' \dontrun{
#' chat("Top 5 R packages")
#' }
#'
#' @export
chat <- function(text = "What are the top 5 R packages ?", model = "mistral-tiny", ..., error_call = current_env()) {
req <- req_chat(text, model, error_call = error_call)
resp <- req_perform(req, error_call = error_call)
resp_chat(resp, error_call = error_call)
}
26 changes: 26 additions & 0 deletions R/httr2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
req_mistral_perform <- function(req, error_call = caller_env()) {

url <- req$url
handle_req_perform_error <- function(err) {
bullets <- c(x = "with endpoint {.url {url}}")

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}"
)
}

cli::cli_abort(
bullets, class = c("error_mistral_req_perform"),
call = error_call, parent = err
)
}

withCallingHandlers(
req_perform(req),
error = handle_req_perform_error
)
}
34 changes: 16 additions & 18 deletions R/models.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
check_model <- function(model, error_call = caller_env()) {
available_models <- models(error_call = error_call)

if (!(model %in% available_models)) {
cli_abort(call = error_call, c(
"The model {model} is not available.",
"i" = "Please use the {.code models()} function to see the available models."
))
}

invisible(model)
}

#' Retrieve all models available in the Mistral API
#'
#' @inheritParams httr2::req_perform
Expand All @@ -23,7 +10,7 @@ check_model <- function(model, error_call = caller_env()) {
#' }
#'
#' @export
models <- function(error_call = caller_env()) {
models <- function(error_call = current_env()) {

req <- request(mistral_base_url) |>
req_url_path_append("v1", "models") |>
Expand All @@ -32,9 +19,20 @@ models <- function(error_call = caller_env()) {
use_on_error = TRUE,
max_age = 2 * 60 * 60) # 2 hours

resp <- req_perform(req) |>
resp_body_json(simplifyVector = TRUE)
req_mistral_perform(req, error_call = error_call) |>
resp_body_json(simplifyVector = TRUE) |>
pluck("data","id")
}

check_model <- function(model, error_call = caller_env()) {
available_models <- models(error_call = error_call)

if (!(model %in% available_models)) {
cli_abort(call = error_call, c(
"The model {model} is not available.",
"i" = "Please use the {.code models()} function to see the available models."
))
}

resp |>
purrr::pluck("data","id")
invisible(model)
}
1 change: 0 additions & 1 deletion R/stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ stream <- function(text, model = "mistral-tiny", ..., error_call = current_env()
invisible(resp)
}

#' @importFrom purrr map_chr
#' @importFrom jsonlite fromJSON
stream_callback <- function(x) {
txt <- rawToChar(x)
Expand Down
2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @import httr2
#' @import tibble
#' @import stringr
#' @importFrom purrr map_dfr
#' @importFrom purrr map_dfr map_chr pluck
NULL

mistral_base_url <- "https://api.mistral.ai"
Expand Down
2 changes: 1 addition & 1 deletion man/models.Rd

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