Class OpenAiOfficialBatchChatModel
- All Implemented Interfaces:
BatchChatModel
Requests are serialized to a JSONL file, uploaded through the Files API with the batch purpose,
and submitted against the chat completions endpoint. Results are correlated back to the submitted requests
by custom_id, so BatchResponse.results() is in submission order even though OpenAI does
not guarantee the order of the output file. See retrieve(String) for the one case in which that
correlation is not possible.
Every request in a batch must use the same model, so submitting requests that resolve to different
models fails fast with an IllegalArgumentException.
Azure OpenAI is supported. Its batch and file operations are scoped to the resource rather than to a
deployment, it expects /chat/completions as the batch endpoint, and it identifies the model by
deployment name, all of which are handled here. GitHub Models has no Batch API and is rejected with an
UnsupportedFeatureException.
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected com.openai.client.OpenAIClientAsyncprotected com.openai.client.OpenAIClientprotected OpenAiOfficialChatRequestParametersprotected List<ChatModelListener> protected Stringprotected ModelProviderprotected Stringprotected Booleanprotected Booleanprotected Set<Capability> protected TokenCountEstimator -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuilder()voidCancels a chat batch operation that is currently pending or running.voidinit(String baseUrl, String apiKey, com.openai.credential.Credential credential, String microsoftFoundryDeploymentName, com.openai.azure.AzureOpenAIServiceVersion azureOpenAIServiceVersion, String organizationId, boolean isAzure, boolean isGitHubModels, ChatRequestParameters defaultRequestParameters, String modelName, Double temperature, Double topP, List<String> stop, Integer maxCompletionTokens, Double presencePenalty, Double frequencyPenalty, Map<String, Integer> logitBias, String responseFormat, Boolean strictJsonSchema, Integer seed, String user, Boolean strictTools, Boolean parallelToolCalls, Boolean store, Map<String, String> metadata, String serviceTier, Duration timeout, Integer maxRetries, Proxy proxy, TokenCountEstimator tokenCountEstimator, Map<String, String> customHeaders, List<ChatModelListener> listeners, Set<Capability> capabilities, boolean isAsync) list(@Nullable BatchPagination pagination) Lists chat batch jobs with optional pagination.provider()Retrieves the current state and results of a chat batch operation.submit(BatchRequest<ChatRequest> request) Creates a batch of chat requests and submits them for asynchronous processing.
-
Field Details
-
client
protected com.openai.client.OpenAIClient client -
asyncClient
protected com.openai.client.OpenAIClientAsync asyncClient -
modelName
-
defaultRequestParameters
-
responseFormat
-
strictJsonSchema
-
strictTools
-
tokenCountEstimator
-
listeners
-
supportedCapabilities
-
modelProvider
-
-
Constructor Details
-
OpenAiOfficialBatchChatModel
-
-
Method Details
-
submit
Creates a batch of chat requests and submits them for asynchronous processing.The returned
BatchResponserepresents the status of the batch operation.Submitting a batch uploads a JSONL input file through the provider's Files API before creating the batch, so that API's storage, retention and quota limits apply in addition to the batch limits, and may change independently of them. Uploaded input files are retained until they expire or are deleted, which
OpenAiOfficialBatchChatModel.Builder.inputFileExpiresAfter(Duration)controls.OpenAI currently limits a batch to 50,000 requests and its input file to 200 MB; exceeding either is rejected by the API.
- Specified by:
submitin interfaceBatchChatModel- Parameters:
request- the list of chat requests to process in the batch- Returns:
- a
BatchResponserepresenting the initial state of the batch operation - Throws:
IllegalArgumentException- if the batch is empty, or if the requests do not all resolve to the same model.
-
retrieve
Retrieves the current state and results of a chat batch operation.The response indicates whether the batch is still processing, completed successfully, or failed. Clients should poll this method at intervals until the batch completes.
Both the output file and the error file are streamed, but every parsed result is held in memory, so a batch close to OpenAI's 200 MB limit requires a correspondingly large heap. Results are returned as soon as OpenAI has produced them, which includes the partial results of an expired or cancelled batch.
Results are correlated by
custom_id, using the identifiers thatsubmit(BatchRequest)generates. If any of them is missing, malformed, duplicated or outside the submitted range - which happens for a batch that was not submitted through this model, for example one created from the OpenAI dashboard - correlation is abandoned for the whole batch and the results are returned in the order the result files list them, with a warning logged. Results are never discarded, so a batch that was paid for is always readable; only their correspondence to the submitted requests is lost.BatchError.code()holds the HTTP status code for a request that OpenAI attempted and rejected, and0for a request that never ran (for example an expired one) or for a batch-level failure, because OpenAI reports those with a string code instead. That string code is available under the"code"key ofBatchError.details().- Specified by:
retrievein interfaceBatchChatModel- Parameters:
batchId- the batch identifier obtained fromBatchChatModel.submit(BatchRequest)- Returns:
- a
BatchResponserepresenting the current state of the chat batch operation
-
cancel
Description copied from interface:BatchChatModelCancels a chat batch operation that is currently pending or running.- Specified by:
cancelin interfaceBatchChatModel- Parameters:
batchId- the batch identifier to cancel
-
list
Description copied from interface:BatchChatModelLists chat batch jobs with optional pagination.- Specified by:
listin interfaceBatchChatModel- Parameters:
pagination- the maximum number of batch jobs to return and token for retrieving a specific page; if null, uses server default- Returns:
- a
BatchPagecontaining chat batch responses and pagination information
-
builder
- Returns:
- a new
OpenAiOfficialBatchChatModel.BuilderforOpenAiOfficialBatchChatModel.
-
init
public void init(String baseUrl, String apiKey, com.openai.credential.Credential credential, String microsoftFoundryDeploymentName, com.openai.azure.AzureOpenAIServiceVersion azureOpenAIServiceVersion, String organizationId, boolean isAzure, boolean isGitHubModels, ChatRequestParameters defaultRequestParameters, String modelName, Double temperature, Double topP, List<String> stop, Integer maxCompletionTokens, Double presencePenalty, Double frequencyPenalty, Map<String, Integer> logitBias, String responseFormat, Boolean strictJsonSchema, Integer seed, String user, Boolean strictTools, Boolean parallelToolCalls, Boolean store, Map<String, String> metadata, String serviceTier, Duration timeout, Integer maxRetries, Proxy proxy, TokenCountEstimator tokenCountEstimator, Map<String, String> customHeaders, List<ChatModelListener> listeners, Set<Capability> capabilities, boolean isAsync) -
defaultRequestParameters
-
supportedCapabilities
-
listeners
-
provider
-