Learn R Programming

openaiRtools (version 0.2.2)

create_response: Create a Response (Convenience Function)

Description

Shortcut that creates an OpenAI client from the OPENAI_API_KEY environment variable and calls client$responses$create().

Usage

create_response(model, input, ...)

Value

A response object. Access the text via $output[[1]]$content[[1]]$text.

Arguments

model

Character. Required. Model ID (e.g. "gpt-4o", "gpt-4o-mini", "o1").

input

Required. A text string or list of message objects as the prompt.

...

Additional parameters passed to ResponsesClient$create(), such as instructions, previous_response_id (for multi-turn), tools, temperature, max_output_tokens, stream, store.

Details

The Responses API is OpenAI's newer, simpler alternative to Chat Completions. It natively supports multi-turn conversations using previous_response_id (no need to resend message history), and includes built-in web search, file search, and other tools.

Examples

Run this code
if (FALSE) {
Sys.setenv(OPENAI_API_KEY = "sk-xxxxxx")

# Simple text response
resp <- create_response(
  model = "gpt-4o",
  input = "What is the difference between OLS and IV?"
)
cat(resp$output[[1]]$content[[1]]$text)

# Multi-turn: continue conversation using previous response ID
resp1 <- create_response("gpt-4o", "What is GMM?")
resp2 <- create_response(
  model                = "gpt-4o",
  input                = "Give me an R code example.",
  previous_response_id = resp1$id
)
cat(resp2$output[[1]]$content[[1]]$text)
}

Run the code above in your browser using DataLab