Class GoogleAiGeminiBatchEmbeddingModel
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionbuilder()voidCancels a batch operation that is currently pending or running.createBatchFromFile(String displayName, GeminiFiles.GeminiFile file) Creates and enqueues a batch of embedding requests from an uploaded file.createBatchInline(String displayName, @Nullable Long priority, List<TextSegment> segments) Creates and enqueues a batch of embedding requests for asynchronous processing using the inline API.voidDeletes a batch job.listBatchJobs(@Nullable Integer pageSize, @Nullable String pageToken) Lists batch jobs.Retrieves the current state and results of a batch operation.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
-
createBatchInline
public BatchRequestResponse.BatchResponse<Embedding> createBatchInline(String displayName, @Nullable Long priority, List<TextSegment> segments) Creates and enqueues a batch of embedding requests for asynchronous processing using the inline API.This method submits a list of text segments to be embedded as a single batch operation. It is designed for efficient, asynchronous processing of multiple texts. This method uses the inline batch creation endpoint, which supports requests up to 20 MB in size.
The response contains the initial state of the batch job (usually PENDING). You can monitor the job's progress using
retrieveBatchResults(BatchName).- Parameters:
displayName- a user-defined name for the batch, used for identification and listingpriority- optional priority for the batch; batches with higher priority values are processed before those with lower values; negative values are allowed; defaults to 0 if nullsegments- the list ofTextSegments to generate embeddings for- Returns:
- a
BatchRequestResponse.BatchResponserepresenting the initial state of the batch operation, typicallyBatchRequestResponse.BatchIncomplete
-
createBatchFromFile
public BatchRequestResponse.BatchResponse<Embedding> createBatchFromFile(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. 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
BatchRequestResponse.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
-
retrieveBatchResults
public BatchRequestResponse.BatchResponse<Embedding> retrieveBatchResults(BatchRequestResponse.BatchName name) Retrieves the current state and results of a batch operation. -
cancelBatchJob
Cancels a batch operation that is currently pending or running. -
deleteBatchJob
Deletes a batch job. -
listBatchJobs
public BatchRequestResponse.BatchList<Embedding> listBatchJobs(@Nullable Integer pageSize, @Nullable String pageToken) Lists batch jobs. -
builder
-