View source: R/provider_openai.R
| create_openai | R Documentation |
Factory function to create an OpenAI provider.
create_openai(
api_key = NULL,
base_url = NULL,
organization = NULL,
project = NULL,
headers = NULL,
name = NULL,
timeout_seconds = NULL,
total_timeout_seconds = NULL,
first_byte_timeout_seconds = NULL,
connect_timeout_seconds = NULL,
idle_timeout_seconds = NULL,
disable_stream_options = FALSE,
api_format = c("auto", "chat", "responses")
)
api_key |
OpenAI API key. Defaults to OPENAI_API_KEY env var. |
base_url |
Base URL for API calls. Defaults to https://api.openai.com/v1. |
organization |
Optional OpenAI organization ID. |
project |
Optional OpenAI project ID. |
headers |
Optional additional headers. |
name |
Optional provider name override (for compatible APIs). |
timeout_seconds |
Legacy alias for |
total_timeout_seconds |
Optional total request timeout in seconds for API calls. |
first_byte_timeout_seconds |
Optional time-to-first-byte timeout in seconds for API calls. |
connect_timeout_seconds |
Optional connection-establishment timeout in seconds for API calls. |
idle_timeout_seconds |
Optional stall timeout in seconds for API calls. |
disable_stream_options |
Disable stream_options parameter (for providers like Volcengine that don't support it). |
api_format |
Default API surface for |
An OpenAIProvider object.
gpt-5.5: Latest OpenAI frontier model for complex reasoning, coding, and professional workflows. (Reasoning, Vision, Tools, Structured) | ctx: 1050k
gpt-5.5-pro: Higher-compute GPT-5.5 variant for the hardest professional and reasoning tasks. (Reasoning, Vision, Tools, Structured) | ctx: 1050k
gpt-5.4: Frontier model for coding and professional work with 1.05M context. (Reasoning, Vision, Tools, Structured) | ctx: 1050k
gpt-5.4-pro: Higher-compute GPT-5.4 variant for more precise responses on difficult tasks. (Reasoning, Vision, Tools, Structured) | ctx: 1050k
gpt-5.4-mini: Strong mini model for coding, computer use, and subagents. (Reasoning, Vision, Tools, Structured) | ctx: 400k
gpt-5.4-nano: Lowest-cost GPT-5.4-class model for high-volume classification, extraction, ranking, and subagents. (Reasoning, Vision, Tools, Structured) | ctx: 400k
gpt-5.3-codex: Agentic coding model optimized for Codex-like environments. (Reasoning, Vision, Tools, Structured) | ctx: 400k
gpt-5: Reasoning model for coding and agentic tasks across domains. (Reasoning, Vision, Tools, Structured) | ctx: 400k
gpt-5-mini: Cost-sensitive, low-latency GPT-5 model for well-defined tasks and precise prompts. (Reasoning, Vision, Tools, Structured) | ctx: 400k
gpt-5-nano: Fastest and lowest-cost GPT-5 model for summarization, classification, and routing. (Reasoning, Vision, Tools, Structured) | ctx: 400k
gpt-4.1: Smart non-reasoning model with strong instruction following and tool calling. (Vision, Tools, Structured) | ctx: 1048k
gpt-4.1-mini: Smaller, faster GPT-4.1 model with long-context support. (Vision, Tools, Structured) | ctx: 1048k
gpt-4.1-nano: Fastest and lowest-cost GPT-4.1 model for simple tasks. (Vision, Tools, Structured) | ctx: 1048k
gpt-4o: Legacy multimodal GPT model with broad ecosystem support. (Vision, Tools, Structured) | ctx: 128k
gpt-4o-mini: Fast, affordable GPT-4o model for focused tasks. (Vision, Tools, Structured) | ctx: 128k
... and 6 more models. Use list_models("openai") to see all.
The SDK provides a unified max_tokens parameter that automatically maps to the
correct API field based on the model and API type:
Chat API (standard models): max_tokens -> max_tokens
Chat API (o1/o3 models): max_tokens -> max_completion_tokens
Responses API: max_tokens -> max_output_tokens (total: reasoning + answer)
For advanced users who need fine-grained control:
max_completion_tokens: Explicitly set completion tokens (Chat API, o1/o3)
max_output_tokens: Explicitly set total output limit (Responses API)
max_answer_tokens: Limit answer only, excluding reasoning (Responses API, Volcengine-specific)
if (interactive()) {
# Basic usage with Chat Completions API
openai <- create_openai(api_key = "sk-...")
model <- openai$language_model("gpt-4o")
result <- generate_text(model, "Hello!")
# Using Responses API for reasoning models
openai <- create_openai()
model <- openai$responses_model("o1")
result <- generate_text(model, "Solve this math problem...")
print(result$reasoning) # Access chain-of-thought
# Smart model selection (auto-detects best API)
model <- openai$smart_model("o3-mini") # Uses Responses API
model <- openai$smart_model("gpt-4o") # Uses Chat Completions API
# Token limits - unified interface
# For standard models: limits generated content
result <- model$generate(messages = msgs, max_tokens = 1000)
# For o1/o3 models: automatically maps to max_completion_tokens
model_o1 <- openai$language_model("o1")
result <- model_o1$generate(messages = msgs, max_tokens = 2000)
# For Responses API: automatically maps to max_output_tokens (total limit)
model_resp <- openai$responses_model("o1")
result <- model_resp$generate(messages = msgs, max_tokens = 2000)
# Advanced: explicitly control answer-only limit (Volcengine Responses API)
result <- model_resp$generate(messages = msgs, max_answer_tokens = 500)
# Multi-turn conversation with Responses API
model <- openai$responses_model("o1")
result1 <- generate_text(model, "What is 2+2?")
result2 <- generate_text(model, "Now multiply that by 3") # Remembers context
model$reset() # Start fresh conversation
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.