Class GoogleAiGeminiBatchEmbeddingModel
- All Implemented Interfaces:
BatchEmbeddingModel
Provides asynchronous batch processing for generating embeddings from multiple text segments at reduced cost (50% of standard pricing) with a 24-hour turnaround SLO.
Implements BatchEmbeddingModel for unified batch processing of embedding requests.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for constructingGoogleAiGeminiBatchEmbeddingModelinstances. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Returns a new builder for constructingGoogleAiGeminiBatchEmbeddingModelinstances.voidCancels an embedding batch operation that is currently pending or running.voiddeleteBatchJob(String batchId) Deletes a batch job from the system.list(@Nullable BatchPagination batchPagination) Lists embedding batch jobs with optional pagination.Retrieves the current state and results of an embedding batch operation.submit(BatchRequest<TextSegment> request) Creates a batch of text segments and submits them for asynchronous embedding processing.submit(GeminiBatchRequest<TextSegment> request) Creates and enqueues a batch of embedding requests, with Gemini-specific options such as display name and priority.submit(String displayName, GeminiFiles.GeminiFile file) Creates and enqueues a batch of embedding requests from an uploaded file.voidwriteBatchToFile(JsonLinesWriter writer, Iterable<BatchRequestResponse.BatchFileRequest<TextSegment>> requests) Writes a sequence of text segments to a JSONL file writer in the format required for file-based batch processing.
-
Method Details
-
submit
Creates a batch of text segments and submits them for asynchronous embedding processing.The returned
BatchResponserepresents the status of the batch operation.Creates and enqueues a batch of embedding requests using default display name and priority. To set a custom display name or priority, pass a
GeminiBatchRequest(it will resolve tosubmit(GeminiBatchRequest)).- Specified by:
submitin interfaceBatchEmbeddingModel- Parameters:
request- the list ofTextSegments to generate embeddings for- Returns:
- a
BatchResponserepresenting the initial state of the batch operation
-
submit
Creates and enqueues a batch of embedding requests, with Gemini-specific options such as display name and priority.- Parameters:
request- aGeminiBatchRequestcarrying the text segments and optional metadata- Returns:
- a
BatchResponserepresenting the initial state of the batch operation
-
submit
Creates and enqueues a batch of embedding requests from an uploaded file.This method is used for processing large volumes of embedding requests that exceed the limits of the inline API (20 MB). Before calling this method, you must write your requests to a JSONL file (using
writeBatchToFile(JsonLinesWriter, Iterable)) and upload it using theGeminiFilesAPI to get aGeminiFiles.GeminiFile.- Parameters:
displayName- a user-defined name for the batch, used for identificationfile- theGeminiFiles.GeminiFilerepresenting the uploaded JSONL file containing the requests- Returns:
- a
BatchResponserepresenting the initial state of the batch operation - See Also:
-
writeBatchToFile
public void writeBatchToFile(JsonLinesWriter writer, Iterable<BatchRequestResponse.BatchFileRequest<TextSegment>> requests) throws IOException Writes a sequence of text segments to a JSONL file writer in the format required for file-based batch processing.This helper method takes high-level
TextSegmentobjects wrapped inBatchRequestResponse.BatchFileRequests (which enable assigning unique keys to each request) and serializes them into the specific JSON structure expected by the Gemini Batch API. It handles the conversion to internal request objects, including metadata handling for document titles.Example Usage:
Path batchFile = Files.createTempFile("embeddings", ".jsonl"); try (JsonLinesWriter writer = new StreamingJsonLinesWriter(batchFile)) { List<BatchFileRequest<TextSegment>> requests = List.of( new BatchFileRequest<>("doc-1", TextSegment.from("Content for document 1")), new BatchFileRequest<>("doc-2", TextSegment.from("Content for document 2")) ); batchModel.writeBatchToFile(writer, requests); }- Parameters:
writer- theJsonLinesWriterto write torequests- an iterable ofBatchRequestResponse.BatchFileRequests, each containing a key and aTextSegment- Throws:
IOException- if an error occurs while writing to the underlying stream
-
retrieve
Retrieves the current state and results of an embedding batch operation.The response indicates whether the batch is still processing, completed successfully, or failed. Use this to retrieve the computed embeddings once the state is success.
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 interfaceBatchEmbeddingModel- Parameters:
batchId- the batch name obtained fromBatchEmbeddingModel.submit(BatchRequest)orsubmit(String, GeminiFile)- Returns:
- a
BatchResponserepresenting the current state of the batch operation
-
cancel
Cancels an embedding batch operation that is currently pending or running.Cancellation is only possible for batches that are in PENDING or RUNNING state. Batches that have already completed, failed, or been cancelled cannot be cancelled.
- Specified by:
cancelin interfaceBatchEmbeddingModel- Parameters:
batchId- the batch name to cancel
-
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 embedding batch jobs with optional pagination.- Specified by:
listin interfaceBatchEmbeddingModel- 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 embedding batch responses and pagination information
-
builder
Returns a new builder for constructingGoogleAiGeminiBatchEmbeddingModelinstances.- Returns:
- a new builder instance
-