A specialized AgentDriver that interacts with the OpenAI Chat
Completions API. Requires an active OpenAI account and API key.
Details
Setup: To use this driver, you must set the OPENAI_API_KEY
environment variable. It is recommended to add this to your .Renviron file:
OPENAI_API_KEY="sk-..."
Super class
HydraR::AgentDriver -> OpenAIAPIDriver
Methods
Method new()
Initialize OpenAIAPIDriver
Usage
OpenAIAPIDriver$new(
id = "openai_api",
model = "gpt-5.4-mini",
validation_mode = "warning",
working_dir = NULL
)Arguments
idString. Unique identifier for this driver instance.
modelString. The OpenAI model ID (e.g.,
"gpt-4","gpt-3.5-turbo").validation_modeString. Either
"warning"or"strict". Controls how schema mismatches are handled.working_dirString. Optional. The directory where output files should be generated.
Method call()
Usage
OpenAIAPIDriver$call(
prompt,
model = NULL,
system_prompt = NULL,
cli_opts = list(),
...
)Arguments
promptString. The primary user message or instruction sent to the LLM.
modelString. Optional model override for this specific call.
system_promptString. Optional system-level instruction (role-playing or constraint).
cli_optsList. Additional parameters passed to the JSON body (e.g.,
temperature = 0.7)....Additional arguments. Passed through to the internal request handler.
Examples
if (FALSE) { # \dontrun{
# 1. Standard API-based completion
driver <- OpenAIAPIDriver$new(model = "gpt-4-turbo")
# 2. Advanced call with JSON mode and deterministic sampling
# This ensures the LLM returns a strictly formatted JSON string.
response <- driver$call(
prompt = "Return a JSON list of 3 common R packages for plotting.",
cli_opts = list(
temperature = 0,
response_format = list(type = "json_object"),
max_tokens = 500
)
)
message(response)
} # }