OpenAI Provider
The OpenAI provider (create_openai()) supports the full suite of OpenAI models, including standard GPT-4o, GPT-3.5 Turbo, and the newer reasoning models like o1 and o3-mini.
Configuration
Set your API key in your .Renviron:
OPENAI_API_KEY="sk-..."Usage
library(aisdk)
provider <- create_openai()
model <- provider$language_model("gpt-4o")
response <- generate_text(model, "Explain quantum entanglement in one sentence.")
cat(response$text)Advanced Features
Reasoning Models (o1, o3-mini)
The SDK supports the o1 and o3-mini models. Note that some parameters like temperature or max_tokens (replaced by max_completion_tokens) behave differently for these models.
model <- provider$language_model("o3-mini")
response <- generate_text(model, "Solve this complex puzzle...")JSON Mode
You can force JSON output using response_format:
response <- generate_text(
model,
"Return a list of 3 fruits in JSON.",
response_format = list(type = "json_object")
)For a cleaner experience, see the Structured Outputs vignette.
Image Models
OpenAI image generation is exposed through a dedicated image_model() path rather than the chat APIs.
library(aisdk)
provider <- create_openai()
image_model <- provider$image_model("gpt-image-1")
result <- generate_image(
model = image_model,
prompt = "A clean product photo of a cobalt blue ceramic mug on linen",
output_dir = tempdir()
)
result$images[[1]]$pathImage Editing
OpenAI image models also support image-to-image editing:
library(aisdk)
result <- edit_image(
model = create_openai()$image_model("gpt-image-1"),
image = "inst/extdata/product.png",
prompt = "Change the mug color to cobalt blue.",
output_dir = tempdir()
)
result$images[[1]]$pathIn the current aisdk implementation, OpenAI image editing expects a local file path or data URI for the source image. Optional mask uploads are supported for localized edits.