Skip to content

Commit

Permalink
chat(...) once again
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Apr 20, 2024
1 parent bd9ba11 commit 71e8369
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 63 deletions.
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Generated by roxygen2: do not edit by hand

S3method(as_messages,character)
S3method(as_messages,default)
S3method(as_messages,list)
S3method(as_msg,character)
S3method(as_msg,chat_tibble)
S3method(print,chat_tibble)
Expand Down
21 changes: 11 additions & 10 deletions R/chat.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
#' Chat with the Mistral api
#'
#' @param messages Messages
#' @param model which model to use. See [models()] for more information about which models are available
#' @inheritParams rlang::args_dots_empty
#' @param ... messages, see [as_messages()].
#' @param model which model to use. See [models()] for more information about which models are available.
#' @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`
#' @return A tibble with columns `role` and `content` with class `chat_tibble`
#'
#' @examples
#'
#' \dontrun{
#' chat("Top 5 R packages")
#' res <- chat("What are the top 5 R packages ?")
#' res
#'
#' # use the result from a previous chat() to continue the
#' # conversation
#' chat(res, "Why do people love them so much ?")
#' }
#'
#' @export
chat <- function(messages, model = "mistral-tiny", ..., error_call = current_env()) {
check_dots_empty(call = error_call)

messages <- as_messages(messages) %!% "Can't convert {.arg messages} to a list of messages."
chat <- function(..., model = "mistral-tiny", error_call = current_env()) {
messages <- as_messages(..., error_call = error_call)

req <- req_chat(messages, model = model, error_call = error_call)
resp <- authenticate(req, error_call = error_call) |>
Expand Down
45 changes: 14 additions & 31 deletions R/messages.R
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
#' Convert object into a messages list
#'
#' @param messages object to convert to messages
#' @inheritParams rlang::args_dots_empty
#' @param ... objects to convert to messages. Each element can be:
#' a string or a result from [chat()].
#' @inheritParams rlang::args_error_context
#'
#' @examples
#' # unnamed string means user
#' as_messages("hello")
#' as_messages(list("hello"))
#' as_messages(list(assistant = "hello", user = "hello"))
#'
#' # explicit names
#' as_messages(assistant = "hello", user = "hello")
#'
#' \dontrun{
#' res <- chat("hello")
#'
#' # add result from previous chat()
#' as_messages(res, "hello")
#' }
#' @export
as_messages <- function(messages, ...) {
UseMethod("as_messages")
}

#' @export
as_messages.default <- function(messages, ..., error_call = current_env()) {
cli_abort(c(
"No known method for objects of class {.cls {class(messages)}}.",
i = "Use as_messages(<character>) or as_messages(<list>)."
), call = error_call)
}

#' @export
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 = messages)
)
}

#' @export
as_messages.list <- function(messages, ..., error_call = caller_env()) {
check_dots_empty(call = error_call)

as_messages <- function(..., error_call = current_env()) {
messages <- list2(...)
out <- list_flatten(
map2(messages, names2(messages), as_msg, error_call = error_call)
)
Expand Down
7 changes: 2 additions & 5 deletions R/stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
#'
#' @rdname chat
#' @export
stream <- function(messages, model = "mistral-tiny", ..., error_call = current_env()) {
check_dots_empty(call = error_call)

messages <- as_messages(messages)
stream <- function(..., model = "mistral-tiny", error_call = current_env()) {
messages <- as_messages(..., error_call = error_call)
req <- req_chat(messages, model, stream = TRUE, error_call = error_call)

resp <- req_perform_stream(req, callback = stream_callback, round = "line", buffer_kb = 0.01)

invisible(resp)
Expand Down
22 changes: 17 additions & 5 deletions man/as_messages.Rd

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

20 changes: 11 additions & 9 deletions man/chat.Rd

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

0 comments on commit 71e8369

Please sign in to comment.