Skip to contents

Retrieves results from a completed OpenAI batch job, processes them according to the original API endpoint called (e.g., chat completions, embeddings), and returns them as an R object.

Usage

workspace_batch(batch_id, timeout = 120)

Arguments

batch_id

Character string. The ID of the OpenAI batch job (e.g., "batch_abc123"). Required.

timeout

Numeric. Request timeout in seconds, applied to both the status check and the file download requests. Default is 120 (allowing more time for potential file download).

Value

The type of object returned depends on the batch job's endpoint:

  • /v1/chat/completions: A character vector of responses (NA_character_ for failures).

  • /v1/embeddings: A list where each element is a numeric embedding vector (NULL for failures).

  • Other endpoints: A list where each element is the raw parsed body from the response JSONL line (NULL for failures). Returns NULL if the batch job is not yet completed, if critical API calls fail, or if essential information (like output file ID or endpoint) is missing.

Details

This function checks the batch job status. If not "completed", it stops. If completed, it downloads the corresponding output file from OpenAI.

The function then parses the output JSONL file. Crucially, it inspects the endpoint field recorded in the batch job status (e.g., /v1/chat/completions or /v1/embeddings) to determine how to process the body within each successful response line.

  • For /v1/chat/completions: Extracts the text content.

  • For /v1/embeddings: Extracts the numeric embedding vector.

  • For other endpoints: Returns the raw parsed body (as a list) and issues a warning.

Failed requests within the batch are represented by NA_character_ (for chat) or NULL (for embeddings or other types). The results are ordered according to the custom_id (e.g., "request-1", "request-2", ...) used during batch creation.

Examples

if (FALSE) { # \dontrun{
# Ensure API key is set
# Sys.setenv(OPENAI_API_KEY = "YOUR_OPENAI_KEY")

# --- Example for a completed Chat Completion batch ---
completed_chat_batch_id <- "batch_chat_xxxxxxxx" # Replace with your actual ID
chat_results <- workspace_batch(completed_chat_batch_id)
if (!is.null(chat_results)) {
  print("Fetched Chat Batch Results:")
  print(chat_results)
}

# --- Example for a completed Embeddings batch ---
completed_embed_batch_id <- "batch_embed_yyyyyyyy" # Replace with your actual ID
embedding_results <- workspace_batch(completed_embed_batch_id)
if (!is.null(embedding_results)) {
  print("Fetched Embedding Batch Results:")
  # Print dimensions of the first embedding
  if(length(embedding_results) > 0 && is.numeric(embedding_results[[1]])) {
     print(paste("Dim of first embedding:", length(embedding_results[[1]])))
  }
}
} # }