From bc70717721414110ec7f22ccdf0c7f49e2366032 Mon Sep 17 00:00:00 2001 From: JBGruber Date: Thu, 6 Feb 2025 19:41:06 +0100 Subject: [PATCH 1/2] add draft support for open-webui --- R/chat.r | 10 +++++++--- R/lib.R | 9 ++++++--- R/utils.r | 10 ++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/R/chat.r b/R/chat.r index 94f3c69..c5ebbfd 100644 --- a/R/chat.r +++ b/R/chat.r @@ -43,6 +43,8 @@ #' value is `"json"`. #' @param template the prompt template to use (overrides what is defined in the #' Modelfile). +#' @param endpoint should be left as default for Ollama, only matters for +#' different servers (like Open WebUI). #' @param verbose Whether to print status messages to the Console #' (`TRUE`/`FALSE`). The default is to have status messages in #' interactive sessions. Can be changed with `options(rollama_verbose = @@ -137,6 +139,7 @@ query <- function(q, output = c("response", "text", "list", "data.frame", "httr2_response", "httr2_request"), format = NULL, template = NULL, + endpoint = "/api/chat", verbose = getOption("rollama_verbose", default = interactive())) { @@ -170,7 +173,8 @@ query <- function(q, images = images, model_params = model_params, format = format, - template = template) + template = template, + endpoint = endpoint) if (output == "httr2_request") return(invisible(reqs)) @@ -182,7 +186,7 @@ query <- function(q, res <- NULL if (screen) { - res <- purrr::map(resps, httr2::resp_body_json) + res <- purrr::map(resps, resp_body) purrr::walk(res, function(r) { screen_answer(purrr::pluck(r, "message", "content"), purrr::pluck(r, "model")) @@ -192,7 +196,7 @@ query <- function(q, if (output == "httr2_response") return(invisible(resps)) if (is.null(res)) { - res <- purrr::map(resps, httr2::resp_body_json) + res <- purrr::map(res, resp_body) } out <- switch(output, diff --git a/R/lib.R b/R/lib.R index aac8597..32ccffd 100644 --- a/R/lib.R +++ b/R/lib.R @@ -40,7 +40,8 @@ build_req <- function(model, images, model_params, format, - template) { + template, + endpoint) { if (is.null(model)) model <- getOption("rollama_model", default = "llama3.1") if (is.null(server)) server <- getOption("rollama_server", @@ -49,7 +50,9 @@ build_req <- function(model, if (!is.null(seed) && !purrr::pluck_exists(model_params, "seed")) { model_params <- append(model_params, list(seed = seed)) } - check_model_installed(model, server = server) + if (endpoint == "/api/chat") { + check_model_installed(model, server = server) + } req_data <- purrr::map(msg, function(ms) { purrr::map(model, function(m) { list(model = m, @@ -60,7 +63,7 @@ build_req <- function(model, template = template) |> purrr::compact() |> # remove NULL values make_req(server = sample(server, 1, prob = as_prob(names(server))), - endpoint = "/api/chat") + endpoint = endpoint) }) }) |> unlist(recursive = FALSE) diff --git a/R/utils.r b/R/utils.r index 189571c..07ad40b 100644 --- a/R/utils.r +++ b/R/utils.r @@ -124,3 +124,13 @@ throw_error <- function(fails) { } } } + + +resp_body <- function(r) { + r <- httr2::resp_body_json(r) + # for Open WebUI + if (purrr::pluck_exists(r, "choices")) { + r$message <- r[["choices"]][[1]][["message"]] + } + return(r) +} From 515d6c4b11c76ac2925f60073539a57f98a507e2 Mon Sep 17 00:00:00 2001 From: JBGruber Date: Thu, 13 Feb 2025 10:10:18 +0100 Subject: [PATCH 2/2] make model functions use headers --- R/models.r | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/R/models.r b/R/models.r index 44a8c70..9e98a40 100644 --- a/R/models.r +++ b/R/models.r @@ -49,7 +49,8 @@ pull_model <- function(model = NULL, the$str_prgs <- NULL req <- httr2::request(server) |> httr2::req_url_path_append("/api/pull") |> - httr2::req_body_json(list(name = model, insecure = insecure)) + httr2::req_body_json(list(name = model, insecure = insecure)) |> + httr2::req_headers(!!!get_headers()) if (verbose) { httr2::req_perform_stream(req, callback = pgrs, buffer_kb = 0.1) cli::cli_process_done(.envir = the) @@ -76,6 +77,7 @@ show_model <- function(model = NULL, server = NULL) { httr2::req_url_path_append("/api/show") |> httr2::req_body_json(list(name = model)) |> httr2::req_error(body = function(resp) httr2::resp_body_json(resp)$error) |> + httr2::req_headers(!!!get_headers()) |> httr2::req_perform() |> httr2::resp_body_json() |> purrr::list_flatten(name_spec = "{inner}") |> @@ -123,6 +125,7 @@ create_model <- function(model, modelfile, server = NULL) { httr2::req_url_path_append("/api/create") |> httr2::req_method("POST") |> httr2::req_body_json(list(name = model, modelfile = modelfile)) |> + httr2::req_headers(!!!get_headers()) |> httr2::req_perform_stream(callback = pgrs, buffer_kb = 0.1) cli::cli_process_done(.envir = the) @@ -146,6 +149,7 @@ delete_model <- function(model, server = NULL) { httr2::req_method("DELETE") |> httr2::req_body_json(list(name = model)) |> httr2::req_error(body = function(resp) httr2::resp_body_json(resp)$error) |> + httr2::req_headers(!!!get_headers()) |> httr2::req_perform() cli::cli_alert_success("model {model} removed") @@ -166,6 +170,7 @@ copy_model <- function(model, httr2::req_body_json(list(source = model, destination = destination)) |> httr2::req_error(body = function(resp) httr2::resp_body_json(resp)$error) |> + httr2::req_headers(!!!get_headers()) |> httr2::req_perform() cli::cli_alert_success("model {model} copied to {destination}") @@ -185,6 +190,7 @@ list_models <- function(server = NULL) { httr2::request(server) |> httr2::req_url_path_append("/api/tags") |> + httr2::req_headers(!!!get_headers()) |> httr2::req_perform() |> httr2::resp_body_json() |> purrr::pluck("models") |>