diff --git a/R/chat.R b/R/chat.R index 028cfed..360e85e 100644 --- a/R/chat.R +++ b/R/chat.R @@ -1,10 +1,10 @@ #' Chat with the Mistral api #' #' @param messages Messages -#' @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 -#' @inheritParams httr2::req_perform +#' @inheritParams rlang::args_dots_empty +#' @inheritParams rlang::args_error_context #' #' @return A tibble with columns `role` and `content` with class `chat_tibble` or a request #' if this is a `dry_run` @@ -13,7 +13,9 @@ #' chat("Top 5 R packages", dry_run = TRUE) #' #' @export -chat <- function(messages, model = "mistral-tiny", dry_run = FALSE, error_call = current_env()) { +chat <- function(messages, model = "mistral-tiny", dry_run = FALSE, ..., error_call = current_env()) { + check_dots_empty() + req <- req_chat(messages, model = model, error_call = error_call, dry_run = dry_run) if (is_true(dry_run)) { return(req) @@ -29,7 +31,9 @@ print.chat <- function(x, ...) { invisible(x) } -req_chat <- function(messages, model = "mistral-tiny", stream = FALSE, dry_run = FALSE, error_call = caller_env()) { +req_chat <- function(messages, model = "mistral-tiny", stream = FALSE, dry_run = FALSE, ..., error_call = caller_env()) { + check_dots_empty() + if (!is_true(dry_run)) { check_model(model, error_call = error_call) } @@ -50,8 +54,12 @@ req_chat <- function(messages, model = "mistral-tiny", stream = FALSE, dry_run = #' @export as.data.frame.chat_response <- function(x, ...) { - df_req <- map_dfr(resp$request$body$data$messages, as.data.frame) - df_resp <- as.data.frame(resp_body_json(resp)$choices[[1]]$message[c("role", "content")]) + 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) } diff --git a/R/messages.R b/R/messages.R index 92660f0..d5501c1 100644 --- a/R/messages.R +++ b/R/messages.R @@ -1,24 +1,37 @@ +#' Convert object into a messages list +#' +#' @param messages object to convert to messages +#' @param ... ignored +#' @inheritParams rlang::args_error_context +#' +#' @examples +#' as_messages("hello") +#' as_messages(list("hello")) +#' as_messages(list(assistant = "hello", user = "hello")) +#' #' @export -as_messages <- function(messages, ...) { +as_messages <- function(messages, ..., error_call = current_env()) { UseMethod("as_messages") } #' @export -as_messages.character <- function(x, ..., error_call = current_env()) { - check_scalar_string(x, error_call = error_call) - check_unnamed_string(x, error_call = error_call) +as_messages.character <- function(messages, ..., error_call = current_env()) { + check_dots_empty(call = error_call) + check_scalar_string(messages, error_call = error_call) + check_unnamed_string(messages, error_call = error_call) list( - list(role = "user", content = x) + list(role = "user", content = messages) ) } #' @export -as_messages.list <- function(x, ..., error_call = caller_env()) { +as_messages.list <- function(messages, ..., error_call = caller_env()) { check_dots_empty() - bits <- map2(x, names2(x), as_msg, error_call = error_call) - out <- list_flatten(bits) + out <- list_flatten( + map2(messages, names2(messages), as_msg, error_call = error_call) + ) names(out) <- NULL out } diff --git a/man/as_messages.Rd b/man/as_messages.Rd new file mode 100644 index 0000000..7d01a9e --- /dev/null +++ b/man/as_messages.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/messages.R +\name{as_messages} +\alias{as_messages} +\title{Convert object into a messages list} +\usage{ +as_messages(messages, ..., error_call = current_env()) +} +\arguments{ +\item{messages}{object to convert to messages} + +\item{...}{ignored} + +\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.} +} +\description{ +Convert object into a messages list +} +\examples{ +as_messages("hello") +as_messages(list("hello")) +as_messages(list(assistant = "hello", user = "hello")) + +} diff --git a/man/chat.Rd b/man/chat.Rd index 891f8fa..f34a1d8 100644 --- a/man/chat.Rd +++ b/man/chat.Rd @@ -8,6 +8,7 @@ chat( messages, model = "mistral-tiny", dry_run = FALSE, + ..., error_call = current_env() ) } @@ -18,12 +19,12 @@ chat( \item{dry_run}{if TRUE the request is not performed} +\item{...}{These dots are for future extensions and must be empty.} + \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{text}{some text} } \value{ A tibble with columns \code{role} and \code{content} with class \code{chat_tibble} or a request diff --git a/man/stream.Rd b/man/stream.Rd index eeb3058..fee756c 100644 --- a/man/stream.Rd +++ b/man/stream.Rd @@ -19,6 +19,8 @@ stream( \item{dry_run}{if TRUE the request is not performed} +\item{...}{These dots are for future extensions and must be empty.} + \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