Class EmbeddingUtils

java.lang.Object
io.agentscope.core.embedding.EmbeddingUtils

public final class EmbeddingUtils extends Object
Utility class for common EmbeddingModel operations.

This class provides shared functionality used across different EmbeddingModel implementations, including timeout and retry logic for embedding API calls, and conversion utilities.

  • Method Summary

    Modifier and Type
    Method
    Description
    static reactor.core.publisher.Mono<double[]>
    applyTimeoutAndRetry(reactor.core.publisher.Mono<double[]> embeddingMono, io.agentscope.core.model.ExecutionConfig config, String modelName, String provider, org.slf4j.Logger logger)
    Applies timeout and retry configuration to a single embedding Mono.
    static <T> reactor.core.publisher.Mono<T>
    applyTimeoutAndRetry(reactor.core.publisher.Mono<T> embeddingMono, io.agentscope.core.model.ExecutionConfig config, String modelName, String provider, org.slf4j.Logger logger, String operationType)
    Applies timeout and retry configuration to an embedding Mono.
    static reactor.core.publisher.Mono<List<double[]>>
    applyTimeoutAndRetryBatch(reactor.core.publisher.Mono<List<double[]>> batchEmbeddingMono, io.agentscope.core.model.ExecutionConfig config, String modelName, String provider, org.slf4j.Logger logger)
    Applies timeout and retry configuration to a batch embedding Mono.
    static double[]
    Converts a List of Double values to a double array.
    static double[]
    Converts a float array to a double array.
    static double[]
    Converts a List of Float values to a double array.
    static io.agentscope.core.model.ExecutionConfig
    ensureDefaultExecutionConfig(io.agentscope.core.model.ExecutionConfig config)
    Ensures ExecutionConfig has MODEL_DEFAULTS applied.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • applyTimeoutAndRetry

      public static <T> reactor.core.publisher.Mono<T> applyTimeoutAndRetry(reactor.core.publisher.Mono<T> embeddingMono, io.agentscope.core.model.ExecutionConfig config, String modelName, String provider, org.slf4j.Logger logger, String operationType)
      Applies timeout and retry configuration to an embedding Mono.

      This method wraps the original Mono with timeout and retry operators based on the configuration in ExecutionConfig. Both timeout and retry are optional and only applied if configured.

      Timeout Behavior:

      • If timeout is configured, the entire request will fail if it exceeds the specified duration
      • Timeout triggers an EmbeddingException with details about the timeout duration
      • If no timeout is configured, requests can run indefinitely

      Retry Behavior:

      • If ExecutionConfig is provided with maxAttempts > 1, failed requests will be retried with exponential backoff
      • Retries respect the maxAttempts, initialBackoff, and maxBackoff settings
      • Only errors matching the retryOn predicate will be retried
      • Each retry is logged with attempt number and failure reason
      Type Parameters:
      T - the type of the embedding result
      Parameters:
      embeddingMono - the original embedding Mono to enhance
      config - execution config containing timeout and retry settings (may be null)
      modelName - the name of the model for error messages and logging
      provider - the provider name (e.g., "dashscope", "openai") for error messages
      logger - the logger instance for debug and warning messages
      operationType - the operation type description (e.g., "embedding", "batch embedding")
      Returns:
      wrapped Mono with timeout and retry applied
    • applyTimeoutAndRetry

      public static reactor.core.publisher.Mono<double[]> applyTimeoutAndRetry(reactor.core.publisher.Mono<double[]> embeddingMono, io.agentscope.core.model.ExecutionConfig config, String modelName, String provider, org.slf4j.Logger logger)
      Applies timeout and retry configuration to a single embedding Mono.

      This is a convenience method that delegates to the generic applyTimeoutAndRetry(Mono, ExecutionConfig, String, String, Logger, String) with "embedding" as the operation type.

      Parameters:
      embeddingMono - the original embedding Mono to enhance
      config - execution config containing timeout and retry settings (may be null)
      modelName - the name of the model for error messages and logging
      provider - the provider name (e.g., "dashscope", "openai") for error messages
      logger - the logger instance for debug and warning messages
      Returns:
      wrapped Mono with timeout and retry applied
    • applyTimeoutAndRetryBatch

      public static reactor.core.publisher.Mono<List<double[]>> applyTimeoutAndRetryBatch(reactor.core.publisher.Mono<List<double[]>> batchEmbeddingMono, io.agentscope.core.model.ExecutionConfig config, String modelName, String provider, org.slf4j.Logger logger)
      Applies timeout and retry configuration to a batch embedding Mono.

      This is a convenience method that delegates to the generic applyTimeoutAndRetry(Mono, ExecutionConfig, String, String, Logger, String) with "batch embedding" as the operation type.

      Parameters:
      batchEmbeddingMono - the original batch embedding Mono to enhance
      config - execution config containing timeout and retry settings (may be null)
      modelName - the name of the model for error messages and logging
      provider - the provider name (e.g., "dashscope", "openai") for error messages
      logger - the logger instance for debug and warning messages
      Returns:
      wrapped Mono with timeout and retry applied
    • ensureDefaultExecutionConfig

      public static io.agentscope.core.model.ExecutionConfig ensureDefaultExecutionConfig(io.agentscope.core.model.ExecutionConfig config)
      Ensures ExecutionConfig has MODEL_DEFAULTS applied.

      This method applies the standard default execution configuration:

      • If config is null, returns MODEL_DEFAULTS
      • Otherwise returns config unchanged (caller should merge if needed)

      This is used by embedding model builders to ensure that all models have proper default timeout and retry configuration applied.

      Parameters:
      config - the original config (may be null)
      Returns:
      config with MODEL_DEFAULTS applied as needed, or MODEL_DEFAULTS if config is null
    • convertDoubleListToArray

      public static double[] convertDoubleListToArray(List<Double> values)
      Converts a List of Double values to a double array.

      This method is used to convert embedding vectors returned by DashScope SDK (which uses List<Double>) to the standard double[] format used by EmbeddingModel.

      Parameters:
      values - the list of Double values
      Returns:
      the double array
    • convertFloatListToDoubleArray

      public static double[] convertFloatListToDoubleArray(List<Float> values)
      Converts a List of Float values to a double array.

      This method is used to convert embedding vectors returned by OpenAI SDK (which uses List<Float>) to the standard double[] format used by EmbeddingModel.

      Parameters:
      values - the list of Float values
      Returns:
      the double array
    • convertFloatArrayToDoubleArray

      public static double[] convertFloatArrayToDoubleArray(float[] values)
      Converts a float array to a double array.

      This method is used to convert embedding vectors returned by Ollama SDK (which uses float[]) to the standard double[] format used by EmbeddingModel.

      Parameters:
      values - the float array
      Returns:
      the double array