
Construct an OpenAI-compatible LLM translator backend
Source:R/translator-backends.R
translator_openai.RdConfigures an httr2-based translator targeting any OpenAI-compatible
/chat/completions endpoint. Enforces strict URL validation (HTTPS by
default, blocking cloud metadata and invalid ports), API key storage in a
session-scoped non-serializing vault (never persisted in plaintext by
saveRDS()), masking in prints and error traces, strict control-character
and temperature validation, bounded HTTP timeouts and maximum response
sizes, JSON-only parsing via jsonlite::fromJSON() (never eval(parse())),
and strict validation via validate_spec_ir().
Usage
translator_openai(
model = "gpt-4o-mini",
api_key = Sys.getenv("OPENAI_API_KEY"),
base_url = "https://api.openai.com/v1",
temperature = 0,
timeout = 60,
max_response_bytes = 524288L,
allow_http_local = FALSE
)Arguments
- model
Character model identifier (e.g.,
"gpt-4o-mini").- api_key
API key scalar; defaults to
Sys.getenv("OPENAI_API_KEY"). Stored in a session-scoped non-serializing vault and masked in printed output and errors.- base_url
Base API URL (defaults to
"https://api.openai.com/v1").- temperature
Numeric sampling temperature in
[0, 2](defaults to0for reproducibility).- timeout
Numeric HTTP timeout in seconds (
1to600).- max_response_bytes
Maximum permitted HTTP response size in bytes (defaults to
524288L, i.e., 512 KB).- allow_http_local
Logical; if
TRUE, allowshttp://localhostorhttp://127.0.0.1endpoints for local OpenAI-compatible servers.
Examples
tr <- translator_openai(model = "gpt-4o-mini", api_key = "sk-demo-1234567890")
print(tr)
#> <spec_translator:openai>
#> model: gpt-4o-mini | endpoint: https://api.openai.com/v1
#> api_key: sk-...7890 | timeout: 60s | max_bytes: 524288