Learn R Programming

openaiRtools (version 0.2.2)

create_multimodal_message: Build a Multimodal User Message (Text + Images)

Description

Convenience function that assembles a complete "user" message object containing both text and one or more images. Automatically handles URL vs. local file detection.

Usage

create_multimodal_message(text = NULL, images = NULL, detail = "auto")

Value

A named list representing a "user" message:


list(
  role    = "user",
  content = list(
    list(type = "text",      text = <text>),
    list(type = "image_url", image_url = list(url = ..., detail = ...)),
    ...
  )
)

Arguments

text

Character or NULL. The text prompt accompanying the image(s). If NULL, only images are sent (less common).

images

List of image sources. Each element can be:

  • A URL string starting with "http://" or "https://" — passed to image_from_url

  • A local file path string — passed to image_from_file

  • A pre-built content part from image_from_url, image_from_file, or image_from_plot (these are passed through as-is)

Default: NULL (text-only message).

detail

Character. Detail level applied to all images supplied as strings. Ignored for pre-built content parts. "auto" (default), "low", or "high".

Details

Pass the returned object (or a list of such objects) directly to client$chat$completions$create(messages = ...).

See Also

image_from_url(), image_from_file(), image_from_plot()

Examples

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

# --- URL image ---
msg <- create_multimodal_message(
  text   = "What is shown in this chart?",
  images = list("https://example.com/gdp_chart.png")
)
response <- client$chat$completions$create(messages = list(msg), model = "gpt-4o")

# --- Local file ---
msg <- create_multimodal_message(
  text   = "Identify any statistical issues in this residual plot.",
  images = list("output/resid_plot.png"),
  detail = "high"
)

# --- Multiple images (compare two charts) ---
msg <- create_multimodal_message(
  text   = "Compare these two regression diagnostics plots.",
  images = list("plot_model1.png", "plot_model2.png"),
  detail = "high"
)

# --- Mix of pre-built parts ---
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point()
msg <- create_multimodal_message(
  text   = "Describe the scatter pattern.",
  images = list(image_from_plot(p, dpi = 180))
)
}

Run the code above in your browser using DataLab