Class GoogleAiGeminiBatchChatModel
- All Implemented Interfaces:
BatchChatModel
Implements BatchChatModel for unified batch processing of chat requests.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for constructingGoogleAiGeminiBatchChatModelinstances. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Returns a new builder for constructingGoogleAiGeminiBatchChatModelinstances.voidCancels a chat batch operation that is currently pending or running.voiddeleteBatchJob(String batchId) Deletes a batch job from the system.list(@Nullable BatchPagination batchPagination) Lists chat batch jobs with optional pagination.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.submit(GeminiBatchRequest<ChatRequest> request) Creates and enqueues a batch of content generation requests for asynchronous processing, with Gemini-specific options such as display name and priority.submit(String displayName, GeminiFiles.GeminiFile file) Creates a batch of chat requests from an uploaded file.voidwriteBatchToFile(JsonLinesWriter writer, Iterable<BatchRequestResponse.BatchFileRequest<ChatRequest>> requests) Writes a batch of chat requests to a JSONL file for later upload and processing.
-
Method Details
-
submit
Creates a batch of chat requests and submits them for asynchronous processing.The returned
BatchResponserepresents the status of the batch operation.Creates and enqueues a batch of content generation requests for asynchronous processing using default display name and priority. To set a custom display name or priority, pass a
GeminiBatchRequest(it will resolve tosubmit(GeminiBatchRequest)).All requests are processed by the model configured on this batch model (via
BaseGeminiChatModel.GoogleAiGeminiChatModelBaseBuilder.modelName(String)).- Specified by:
submitin interfaceBatchChatModel- Parameters:
request- a list of chat requests to be processed in the batch- Returns:
- a
BatchResponserepresenting the initial state of the batch operation
-
submit
Creates and enqueues a batch of content generation requests for asynchronous processing, with Gemini-specific options such as display name and priority.- Parameters:
request- aGeminiBatchRequestcarrying the chat requests and optional metadata- Returns:
- a
BatchResponserepresenting the initial state of the batch operation
-
submit
Creates a batch of chat requests from an uploaded file.This method allows you to create a batch job using a JSONL file that has been previously uploaded to the Gemini Files API. This is useful for larger batches that exceed the 20 MB inline request limit.
The file must contain batch requests in JSONL format, where each line is a JSON object with a "key" and "request" field. You can use
writeBatchToFile(JsonLinesWriter, Iterable)to create properly formatted JSONL files.- Parameters:
displayName- a user-defined name for the batch, used for identificationfile- the GeminiFile object representing the uploaded file containing batch requests- Returns:
- a
BatchResponserepresenting the initial state of the batch operation - See Also:
-
writeBatchToFile
public void writeBatchToFile(JsonLinesWriter writer, Iterable<BatchRequestResponse.BatchFileRequest<ChatRequest>> requests) throws IOException Writes a batch of chat requests to a JSONL file for later upload and processing.This method serializes chat requests into JSONL (JSON Lines) format, where each line contains a single request wrapped in a
BatchRequestResponse.BatchFileRequestwith a unique key. The resulting file can be uploaded using the Gemini Files API and then used to create a batch job viasubmit(String, GeminiFile).Each request is converted to the internal Gemini format before being written to the file.
Example usage:
Path batchFile = Files.createTempFile("batch", ".jsonl"); try (JsonLinesWriter writer = new StreamingJsonLinesWriter(batchFile)) { List<BatchFileRequest<ChatRequest>> requests = List.of( new BatchFileRequest<>("request-1", ChatRequest.builder() .messages(UserMessage.from("Question 1")) .build()), new BatchFileRequest<>("request-2", ChatRequest.builder() .messages(UserMessage.from("Question 2")) .build()) ); batchModel.writeBatchToFile(writer, requests); }- Parameters:
writer- the JsonLinesWriter to which the batch requests will be writtenrequests- an iterable collection of BatchFileRequest objects containing ChatRequest instances, each with a unique key identifier- Throws:
IOException- if an I/O error occurs while writing to the writer- See Also:
-
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.
Polls the Gemini API to get the latest state of a previously created batch. Clients should poll this method at intervals to check the operation status until completion.
- Specified by:
retrievein interfaceBatchChatModel- Parameters:
batchId- the batch name obtained fromBatchChatModel.submit(BatchRequest)orsubmit(String, GeminiFile)- Returns:
- a
BatchResponserepresenting the current state of the batch operation
-
cancel
Cancels a chat batch operation that is currently pending or running.Cancellation is only possible for batches that are in
BatchState.PENDINGorBatchState.RUNNINGstate. Batches that have already completed, failed, or been cancelled cannot be cancelled.- Specified by:
cancelin interfaceBatchChatModel- Parameters:
batchId- the batch name to cancel- Throws:
HttpException- if the batch cannot be cancelled (e.g., already completed, already cancelled, or does not exist)
-
deleteBatchJob
Deletes a batch job from the system.This removes the batch job record but does not cancel it if still running. Use
cancel(String)to cancel a running batch before deletion.- Parameters:
batchId- the batch name to delete- Throws:
RuntimeException- if the batch job cannot be deleted or does not exist
-
list
Lists chat batch jobs with optional pagination.- Specified by:
listin interfaceBatchChatModel- Parameters:
batchPagination- 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 builder for constructingGoogleAiGeminiBatchChatModelinstances.- Returns:
- a new builder instance
-