Learn R Programming

openaiRtools (version 0.2.2)

image_from_file: Create Image Content from a Local File

Description

Reads a local image file, encodes it as Base64, and wraps it in a data URI so it can be sent directly to the model without hosting the file anywhere. This is the recommended approach for local figures, screenshots, or any image not accessible via a public URL.

Usage

image_from_file(file_path, mime_type = NULL, detail = "auto")

Value

A named list (content part) ready to include in a message's content list.

Arguments

file_path

Character. Absolute or relative path to a local image file. Supported formats: .jpg/.jpeg, .png, .gif (static), .webp. Maximum recommended size: 20 MB (smaller is faster).

mime_type

Character or NULL. MIME type of the image. When NULL (default), auto-detected from the file extension:

  • .jpg / .jpeg"image/jpeg"

  • .png"image/png"

  • .gif"image/gif"

  • .webp"image/webp"

Override only if the extension is missing or wrong.

detail

Character. Image detail level: "auto" (default), "low", or "high". See image_from_url for full explanation.

See Also

image_from_url, image_from_plot, create_multimodal_message

Examples

Run this code
if (FALSE) {
library(openaiRtools)
client <- OpenAI$new(api_key = "sk-xxxxxx")

# Send a local chart image
img <- image_from_file("results/regression_plot.png", detail = "high")

messages <- list(
  list(
    role = "user",
    content = list(
      list(
        type = "text",
        text = "This is a residuals-vs-fitted plot. Does it show heteroskedasticity?"
      ),
      img
    )
  )
)

response <- client$chat$completions$create(
  messages = messages,
  model    = "gpt-4o"
)
cat(response$choices[[1]]$message$content)
}

Run the code above in your browser using DataLab