From f3aa6656e14dea66b49b53ee2b52a9b3153d8835 Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Sat, 9 Mar 2024 11:28:44 +0100 Subject: [PATCH 1/6] add chat(dry_run=) --- NAMESPACE | 1 + R/chat.R | 21 +++++++++++++++------ man/chat.Rd | 10 ++++++++-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index c964e37..4b6c332 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -12,3 +12,4 @@ import(tibble) importFrom(jsonlite,fromJSON) importFrom(purrr,map_chr) importFrom(purrr,map_dfr) +importFrom(purrr,pluck) diff --git a/R/chat.R b/R/chat.R index d6aaa92..74914cb 100644 --- a/R/chat.R +++ b/R/chat.R @@ -1,5 +1,7 @@ -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) +req_chat <- function(text = "What are the top 5 R packages ?", model = "mistral-tiny", stream = FALSE, dry_run = FALSE, error_call = caller_env()) { + if (!is_true(dry_run)) { + check_model(model, error_call = error_call) + } request(mistral_base_url) |> req_url_path_append("v1", "chat", "completions") |> authenticate(error_call = error_call) |> @@ -43,19 +45,26 @@ print.chat_tibble <- function(x, ...) { #' #' @param text some text #' @param model which model to use. See [models()] for more information about which models are available +#' @param dry_run if TRUE the request is not performed #' @param ... ignored #' @inheritParams httr2::req_perform #' -#' @return Result text from Mistral +#' @return A tibble with columns `role` and `content` with class `chat_tibble` or a request +#' if this is a `dry_run` #' #' @examples +#' chat("Top 5 R packages", dry_run = TRUE) +#' #' \dontrun{ -#' chat("Top 5 R packages") +#' 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) +chat <- function(text = "What are the top 5 R packages ?", model = "mistral-tiny", dry_run = FALSE, ..., error_call = current_env()) { + req <- req_chat(text, model, error_call = error_call, dry_run = dry_run) + if (is_true(dry_run)) { + return(req) + } resp <- req_perform(req, error_call = error_call) resp_chat(resp, error_call = error_call) } diff --git a/man/chat.Rd b/man/chat.Rd index e76f9ea..6643858 100644 --- a/man/chat.Rd +++ b/man/chat.Rd @@ -7,6 +7,7 @@ chat( text = "What are the top 5 R packages ?", model = "mistral-tiny", + dry_run = FALSE, ..., error_call = current_env() ) @@ -16,6 +17,8 @@ chat( \item{model}{which model to use. See \code{\link[=models]{models()}} for more information about which models are available} +\item{dry_run}{if TRUE the request is not performed} + \item{...}{ignored} \item{error_call}{The execution environment of a currently @@ -24,14 +27,17 @@ mentioned in error messages as the source of the error. See the \code{call} argument of \code{\link[rlang:abort]{abort()}} for more information.} } \value{ -Result text from Mistral +A tibble with columns \code{role} and \code{content} with class \code{chat_tibble} or a request +if this is a \code{dry_run} } \description{ Chat with the Mistral api } \examples{ +chat("Top 5 R packages", dry_run = TRUE) + \dontrun{ -chat("Top 5 R packages") + chat("Top 5 R packages") } } From 45c460e2b3f0c28c15e1e323fc4bf7cc6dafc010 Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Sat, 9 Mar 2024 11:31:46 +0100 Subject: [PATCH 2/6] models(dry_run) --- R/models.R | 8 ++++++-- man/models.Rd | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/R/models.R b/R/models.R index 17e560e..0711fe5 100644 --- a/R/models.R +++ b/R/models.R @@ -13,7 +13,7 @@ check_model <- function(model, error_call = caller_env()) { #' Retrieve all models available in the Mistral API #' -#' @inheritParams httr2::req_perform +#' @inheritParams chat #' #' @return A character vector with the models available in the Mistral API #' @@ -23,7 +23,7 @@ check_model <- function(model, error_call = caller_env()) { #' } #' #' @export -models <- function(error_call = caller_env()) { +models <- function(error_call = caller_env(), dry_run = FALSE) { req <- request(mistral_base_url) |> req_url_path_append("v1", "models") |> @@ -32,6 +32,10 @@ models <- function(error_call = caller_env()) { use_on_error = TRUE, max_age = 2 * 60 * 60) # 2 hours + if (is_true(dry_run)) { + return(req) + } + resp <- req_perform(req, error_call = error_call) |> resp_body_json(simplifyVector = TRUE) diff --git a/man/models.Rd b/man/models.Rd index 19d5e51..ace584a 100644 --- a/man/models.Rd +++ b/man/models.Rd @@ -4,13 +4,15 @@ \alias{models} \title{Retrieve all models available in the Mistral API} \usage{ -models(error_call = caller_env()) +models(error_call = caller_env(), dry_run = FALSE) } \arguments{ \item{error_call}{The execution environment of a currently running function, e.g. \code{caller_env()}. The function will be mentioned in error messages as the source of the error. See the \code{call} argument of \code{\link[rlang:abort]{abort()}} for more information.} + +\item{dry_run}{if TRUE the request is not performed} } \value{ A character vector with the models available in the Mistral API From dcff55a6d859693cdee447e8f7084406415046ae Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Sat, 9 Mar 2024 11:33:49 +0100 Subject: [PATCH 3/6] stream(dry_run) --- R/stream.R | 9 +++++++-- man/stream.Rd | 10 +++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/R/stream.R b/R/stream.R index 0524af2..aaf793b 100644 --- a/R/stream.R +++ b/R/stream.R @@ -1,11 +1,16 @@ #' stream #' #' @inheritParams chat +#' #' @export -stream <- function(text, model = "mistral-tiny", ..., error_call = current_env()) { +stream <- function(text, model = "mistral-tiny", dry_run = FALSE, ..., error_call = current_env()) { check_model(model, error_call = error_call) - req <- req_chat(text, model, stream = TRUE, error_call = error_call) + req <- req_chat(text, model, stream = TRUE, error_call = error_call, dry_run = dry_run) + if (is_true(dry_run)) { + return(req) + } + resp <- req_perform_stream(req, callback = stream_callback, round = "line", diff --git a/man/stream.Rd b/man/stream.Rd index a5db2af..7e7ae64 100644 --- a/man/stream.Rd +++ b/man/stream.Rd @@ -4,13 +4,21 @@ \alias{stream} \title{stream} \usage{ -stream(text, model = "mistral-tiny", ..., error_call = current_env()) +stream( + text, + model = "mistral-tiny", + dry_run = FALSE, + ..., + error_call = current_env() +) } \arguments{ \item{text}{some text} \item{model}{which model to use. See \code{\link[=models]{models()}} for more information about which models are available} +\item{dry_run}{if TRUE the request is not performed} + \item{...}{ignored} \item{error_call}{The execution environment of a currently From 6e94bbd9c551a96a847e900661675090c02367dc Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Sat, 9 Mar 2024 11:40:26 +0100 Subject: [PATCH 4/6] document() --- R/models.R | 4 +--- man/models.Rd | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/R/models.R b/R/models.R index 0711fe5..4d085c8 100644 --- a/R/models.R +++ b/R/models.R @@ -18,9 +18,7 @@ check_model <- function(model, error_call = caller_env()) { #' @return A character vector with the models available in the Mistral API #' #' @examples -#' \dontrun{ -#' models() -#' } +#' models(dry_run = TRUE) #' #' @export models <- function(error_call = caller_env(), dry_run = FALSE) { diff --git a/man/models.Rd b/man/models.Rd index ace584a..5427914 100644 --- a/man/models.Rd +++ b/man/models.Rd @@ -21,8 +21,6 @@ A character vector with the models available in the Mistral API Retrieve all models available in the Mistral API } \examples{ -\dontrun{ - models() -} +models(dry_run = TRUE) } From 38081ca9d2393a6b592650e1a9e58144f3f95151 Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Sat, 9 Mar 2024 11:47:11 +0100 Subject: [PATCH 5/6] update chat doc --- man/chat.Rd | 4 ---- 1 file changed, 4 deletions(-) diff --git a/man/chat.Rd b/man/chat.Rd index 6643858..67f8483 100644 --- a/man/chat.Rd +++ b/man/chat.Rd @@ -36,8 +36,4 @@ Chat with the Mistral api \examples{ chat("Top 5 R packages", dry_run = TRUE) -\dontrun{ - chat("Top 5 R packages") -} - } From 796ee088de138c0a258085b2833f4b36122374e9 Mon Sep 17 00:00:00 2001 From: Romain Francois Date: Sat, 9 Mar 2024 11:55:30 +0100 Subject: [PATCH 6/6] authenticate(dry_run) --- R/authenticate.R | 5 ++--- R/chat.R | 2 +- R/models.R | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/R/authenticate.R b/R/authenticate.R index 36e67a4..568cbf6 100644 --- a/R/authenticate.R +++ b/R/authenticate.R @@ -1,6 +1,6 @@ -authenticate <- function(request, error_call = caller_env()){ +authenticate <- function(request, dry_run = FALSE, error_call = caller_env()){ key <- Sys.getenv("MISTRAL_API_KEY") - if (identical(key, "")) { + if (!is_true(dry_run) && identical(key, "")) { cli_abort(call = error_call, c( "Please set the {.code MISTRAL_API_KEY} environment variable", i = "Get an API key from {.url https://console.mistral.ai/api-keys/}", @@ -9,4 +9,3 @@ authenticate <- function(request, error_call = caller_env()){ } req_auth_bearer_token(request, key) } - diff --git a/R/chat.R b/R/chat.R index 7198117..ba60d22 100644 --- a/R/chat.R +++ b/R/chat.R @@ -28,7 +28,7 @@ req_chat <- function(text = "What are the top 5 R packages ?", model = "mistral- } request(mistral_base_url) |> req_url_path_append("v1", "chat", "completions") |> - authenticate(error_call = error_call) |> + authenticate(error_call = error_call, dry_run = dry_run) |> req_body_json( list( model = model, diff --git a/R/models.R b/R/models.R index 3105c99..1282b9a 100644 --- a/R/models.R +++ b/R/models.R @@ -11,7 +11,7 @@ models <- function(error_call = caller_env(), dry_run = FALSE) { req <- request(mistral_base_url) |> req_url_path_append("v1", "models") |> - authenticate(error_call = call) |> + authenticate(error_call = call, dry_run = dry_run) |> req_cache(tempdir(), use_on_error = TRUE, max_age = 2 * 60 * 60) # 2 hours