Class GoogleAiGeminiBatchEmbeddingModel

java.lang.Object
dev.langchain4j.model.googleai.GoogleAiGeminiBatchEmbeddingModel
All Implemented Interfaces:
dev.langchain4j.model.embedding.BatchEmbeddingModel

@Experimental public final class GoogleAiGeminiBatchEmbeddingModel extends Object implements dev.langchain4j.model.embedding.BatchEmbeddingModel
Batch embedding model for Google AI Gemini.

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:
  • BatchEmbeddingModel
  • BatchResponse
  • 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 to submit(GeminiBatchRequest)).

      Specified by:
      submit in interface dev.langchain4j.model.embedding.BatchEmbeddingModel
      Parameters:
      request - the list of TextSegments to generate embeddings for
      Returns:
      a BatchResponse representing 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 - a GeminiBatchRequest carrying the text segments and optional metadata
      Returns:
      a BatchResponse representing 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 the GeminiFiles API to get a GeminiFiles.GeminiFile.

      Parameters:
      displayName - a user-defined name for the batch, used for identification
      file - the GeminiFiles.GeminiFile representing the uploaded JSONL file containing the requests
      Returns:
      a BatchResponse representing 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 TextSegment objects wrapped in BatchRequestResponse.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 - the JsonLinesWriter to write to
      requests - an iterable of BatchRequestResponse.BatchFileRequests, each containing a key and a TextSegment
      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:
      retrieve in interface dev.langchain4j.model.embedding.BatchEmbeddingModel
      Parameters:
      batchId - the batch name obtained from BatchEmbeddingModel.submit(BatchRequest) or submit(String, GeminiFile)
      Returns:
      a BatchResponse representing the current state of the batch operation
    • cancel

      public void cancel(String batchId)

      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:
      cancel in interface dev.langchain4j.model.embedding.BatchEmbeddingModel
      Parameters:
      batchId - the batch name to cancel
    • deleteBatchJob

      public void deleteBatchJob(String batchId)
      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:
      list in interface dev.langchain4j.model.embedding.BatchEmbeddingModel
    • builder

      Returns a new builder for constructing GoogleAiGeminiBatchEmbeddingModel instances.
      Returns:
      a new builder instance