Skip to content

Commit

Permalink
%!% \(err)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Apr 6, 2024
1 parent c2246ff commit dcce7d0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
11 changes: 0 additions & 11 deletions R/chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@ chat <- function(messages, model = "mistral-tiny", ..., error_call = current_env
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 Down
16 changes: 5 additions & 11 deletions R/httr2.R
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
req_mistral_perform <- function(req, error_call = caller_env()) {

withCallingHandlers(
req_perform(req),
error = function(err) {
resp <- err$resp
handler <- mistral_error_handler(err, resp, req)
handler(err, req, resp, error_call = error_call)
}
)
req_perform(req) %!% \(err) handle_mistral_error(err, req, error_call)
}

mistral_error_handler <- function(err, resp, req) {
handle_mistral_error <- function(err, req, error_call) {
resp <- err$resp
status <- resp_status(resp)

if (status == 401) {
handler <- if (status == 401) {
handle_unauthorized
} else if (status == 400 && resp_body_json(resp)$type == "invalid_model") {
handle_invalid_model
} else {
handle_other
}
handler(err, req, resp, error_call = error_call)
}

handle_invalid_model <- function(err, req, resp, error_call = caller_env()) {
Expand Down

0 comments on commit dcce7d0

Please sign in to comment.