Skip to content

Commit

Permalink
Parse image in generate()
Browse files Browse the repository at this point in the history
  • Loading branch information
hauselin committed Jul 28, 2024
1 parent 8e905e5 commit fcda9d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions R/ollama.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
)
Expand Down
3 changes: 3 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit fcda9d0

Please sign in to comment.