Skip to content

Commit

Permalink
Merge pull request #27 from tadascience/slap
Browse files Browse the repository at this point in the history
use {slap}
  • Loading branch information
romainfrancois authored Apr 6, 2024
2 parents 9893e89 + 72162f8 commit c2246ff
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ Imports:
tibble,
stringr,
jsonlite,
rlang
Remotes: r-lib/httr2
rlang,
slap
Remotes:
r-lib/httr2,
tadascience/slap
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(as.data.frame,chat_response)
S3method(as_messages,character)
S3method(as_messages,default)
S3method(as_messages,list)
S3method(as_msg,character)
S3method(as_tibble,chat_response)
Expand All @@ -14,6 +15,7 @@ export(stream)
import(cli)
import(httr2)
import(rlang)
import(slap)
import(stringr)
import(tibble)
importFrom(jsonlite,fromJSON)
Expand Down
15 changes: 13 additions & 2 deletions R/chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@
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."

req <- req_chat(messages, model = model, error_call = error_call)
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
}
Expand All @@ -36,8 +49,6 @@ req_chat <- function(messages, model = "mistral-tiny", stream = FALSE, ..., erro
check_dots_empty(call = error_call)
check_scalar_string(model, error_call = error_call)

messages <- as_messages(messages)

request(mistral_base_url) |>
req_url_path_append("v1", "chat", "completions") |>
authenticate(error_call = error_call) |>
Expand Down
18 changes: 13 additions & 5 deletions R/messages.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Convert object into a messages list
#'
#' @param messages object to convert to messages
#' @param ... ignored
#' @inheritParams rlang::args_dots_empty
#' @inheritParams rlang::args_error_context
#'
#' @examples
Expand All @@ -10,10 +10,18 @@
#' as_messages(list(assistant = "hello", user = "hello"))
#'
#' @export
as_messages <- function(messages, ..., error_call = current_env()) {
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)
Expand Down Expand Up @@ -57,12 +65,12 @@ check_role <- function(name = "", error_call = caller_env()) {
name
}

check_scalar_string <- function(x, error_call = caller_env(), arg_name = deparse(substitute(x))) {
check_scalar_string <- function(x, error_call = caller_env(), error_arg = caller_arg(x)) {
if (!is.character(x)) {
cli_abort("{.arg {arg_name}} must be a single string, not {.obj_type_friendly {x}}. ", call = error_call)
cli_abort("{.arg {error_arg}} must be a single string, not {.obj_type_friendly {x}}. ", call = error_call)
}
if (length(x) != 1L) {
cli_abort("{.arg {arg_name}} must be a single string, not length {.code {length(x)}}. ", call = error_call)
cli_abort("{.arg {error_arg}} must be a single string, not length {.code {length(x)}}. ", call = error_call)
}
}

Expand Down
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' @import httr2
#' @import tibble
#' @import stringr
#' @import slap
#' @importFrom purrr map_dfr map_chr pluck map2 list_flatten
#' @importFrom jsonlite fromJSON
NULL
Expand Down
9 changes: 2 additions & 7 deletions man/as_messages.Rd

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

0 comments on commit c2246ff

Please sign in to comment.