Class GoogleAiGeminiBatchEmbeddingModel
- All Implemented Interfaces:
dev.langchain4j.model.embedding.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.voidvoiddeleteBatchJob(String batchId) Deletes a batch job from the system.dev.langchain4j.model.batch.BatchPage<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> list(@Nullable dev.langchain4j.model.batch.BatchPagination batchPagination) dev.langchain4j.model.batch.BatchResponse<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> dev.langchain4j.model.batch.BatchResponse<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> submit(dev.langchain4j.model.batch.BatchRequest<dev.langchain4j.data.segment.TextSegment> request) dev.langchain4j.model.batch.BatchResponse<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> submit(GeminiBatchRequest<dev.langchain4j.data.segment.TextSegment> request) Creates and enqueues a batch of embedding requests, with Gemini-specific options such as display name and priority.dev.langchain4j.model.batch.BatchResponse<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> submit(String displayName, GeminiFiles.GeminiFile file) Creates and enqueues a batch of embedding requests from an uploaded file.voidwriteBatchToFile(JsonLinesWriter writer, Iterable<BatchRequestResponse.BatchFileRequest<dev.langchain4j.data.segment.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
public dev.langchain4j.model.batch.BatchResponse<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> submit(dev.langchain4j.model.batch.BatchRequest<dev.langchain4j.data.segment.TextSegment> request) 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 interfacedev.langchain4j.model.embedding.BatchEmbeddingModel- Parameters:
request- the list ofTextSegments to generate embeddings for- Returns:
- a
BatchResponserepresenting the initial state of the batch operation
-
submit
public dev.langchain4j.model.batch.BatchResponse<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> submit(GeminiBatchRequest<dev.langchain4j.data.segment.TextSegment> request) 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
public dev.langchain4j.model.batch.BatchResponse<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> submit(String displayName, GeminiFiles.GeminiFile file) 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<dev.langchain4j.data.segment.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
public dev.langchain4j.model.batch.BatchResponse<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> retrieve(String batchId) 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 interfacedev.langchain4j.model.embedding.BatchEmbeddingModel- Parameters:
batchId- the batch name obtained fromBatchEmbeddingModel.submit(BatchRequest)orsubmit(String, GeminiFile)- Returns:
- a
BatchResponserepresenting the current state of the batch operation
-
cancel
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 interfacedev.langchain4j.model.embedding.BatchEmbeddingModel- 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
public dev.langchain4j.model.batch.BatchPage<dev.langchain4j.model.output.Response<dev.langchain4j.data.embedding.Embedding>> list(@Nullable dev.langchain4j.model.batch.BatchPagination batchPagination) - Specified by:
listin interfacedev.langchain4j.model.embedding.BatchEmbeddingModel
-
builder
Returns a new builder for constructingGoogleAiGeminiBatchEmbeddingModelinstances.- Returns:
- a new builder instance
-