diff --git a/R/ollama.R b/R/ollama.R index 9dc3773..f8f0d81 100644 --- a/R/ollama.R +++ b/R/ollama.R @@ -82,10 +82,14 @@ create_request <- function(endpoint, host = NULL) { #' generate("llama3", "The sky is...", stream = TRUE, output = "text") #' generate("llama3", "The sky is...", stream = TRUE, output = "text", temperature = 2.0) #' generate("llama3", "The sky is...", stream = FALSE, output = "jsonlist") -generate <- function(model, prompt, suffix = "", images = list(), system = "", template = "", context = list(), stream = FALSE, raw = FALSE, keep_alive = "5m", output = c("resp", "jsonlist", "raw", "df", "text"), endpoint = "/api/generate", host = NULL, ...) { +generate <- function(model, prompt, suffix = "", images = "", system = "", template = "", context = list(), stream = FALSE, raw = FALSE, keep_alive = "5m", output = c("resp", "jsonlist", "raw", "df", "text"), endpoint = "/api/generate", host = NULL, ...) { req <- create_request(endpoint, host) req <- httr2::req_method(req, "POST") + + images_list <- list() + if (images != "") images_list <- list(image_encode_base64(images)) + body_json <- list( model = model, prompt = prompt, @@ -95,7 +99,7 @@ generate <- function(model, prompt, suffix = "", images = list(), system = "", t context = context, stream = stream, raw = raw, - images = images, + images = images_list, stream = stream, keep_alive = keep_alive ) diff --git a/R/utils.R b/R/utils.R index fb89d5b..63a7366 100644 --- a/R/utils.R +++ b/R/utils.R @@ -191,6 +191,9 @@ resp_process_stream <- function(resp, output) { #' image_path <- file.path(system.file('extdata', package = "ollamar"), "image1.png") #' image_encode_base64(image_path) image_encode_base64 <- function(image_path) { + if (!file.exists(image_path)) { + stop("Image file does not exist.") + } img_raw <- readBin(image_path, "raw", file.info(image_path)$size) return(base64enc::base64encode(img_raw)) }