Class GoogleAiGeminiBatchEmbeddingModel

java.lang.Object
dev.langchain4j.model.googleai.GoogleAiGeminiBatchEmbeddingModel

@Experimental public final class GoogleAiGeminiBatchEmbeddingModel extends Object
Batch embedding model for Google AI Gemini.
  • Method Details

    • createBatchInline

      public BatchRequestResponse.BatchResponse<dev.langchain4j.data.embedding.Embedding> createBatchInline(String displayName, @Nullable Long priority, List<dev.langchain4j.data.segment.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 listing
      priority - 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 null
      segments - the list of TextSegments to generate embeddings for
      Returns:
      a BatchRequestResponse.BatchResponse representing the initial state of the batch operation, typically BatchRequestResponse.BatchIncomplete
    • createBatchFromFile

      public BatchRequestResponse.BatchResponse<dev.langchain4j.data.embedding.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 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 BatchRequestResponse.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
    • retrieveBatchResults

      public BatchRequestResponse.BatchResponse<dev.langchain4j.data.embedding.Embedding> retrieveBatchResults(BatchRequestResponse.BatchName name)
      Retrieves the current state and results of a batch operation.
    • cancelBatchJob

      public void cancelBatchJob(BatchRequestResponse.BatchName name)
      Cancels a batch operation that is currently pending or running.
    • deleteBatchJob

      public void deleteBatchJob(BatchRequestResponse.BatchName name)
      Deletes a batch job.
    • listBatchJobs

      public BatchRequestResponse.BatchList<dev.langchain4j.data.embedding.Embedding> listBatchJobs(@Nullable Integer pageSize, @Nullable String pageToken)
      Lists batch jobs.
    • builder