类 ModelUtils

java.lang.Object
io.agentscope.core.model.ModelUtils

public final class ModelUtils extends Object
Utility class for common Model operations.

This class provides shared functionality used across different Model implementations, including timeout and retry logic for model API calls.

  • 方法详细资料

    • applyTimeoutAndRetry

      public static reactor.core.publisher.Flux<ChatResponse> applyTimeoutAndRetry(reactor.core.publisher.Flux<ChatResponse> responseFlux, GenerateOptions options, GenerateOptions defaultOptions, String modelName, String provider)
      Applies timeout and retry configuration to a model response Flux.

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

      Timeout Behavior:

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

      Retry Behavior:

      • If RetryConfig is provided, 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
      参数:
      responseFlux - the original response Flux to enhance
      options - generation options containing timeout and retry config (may be null)
      defaultOptions - default options to use if options is null
      modelName - the name of the model for error messages and logging
      provider - the provider name (e.g., "dashscope", "openai") for error messages
      返回:
      wrapped Flux with timeout and retry applied
    • ensureDefaultExecutionConfig

      public static GenerateOptions ensureDefaultExecutionConfig(GenerateOptions options)
      Ensures GenerateOptions has MODEL_DEFAULTS for executionConfig applied.

      This method applies the standard default execution configuration to GenerateOptions:

      • If options is null, creates new options with MODEL_DEFAULTS
      • If options has null executionConfig, merges with MODEL_DEFAULTS
      • Otherwise returns options unchanged

      This is used by model builders to ensure that all models have proper default timeout and retry configuration applied, even when users provide custom GenerateOptions without executionConfig.

      参数:
      options - the original options (may be null)
      返回:
      options with MODEL_DEFAULTS applied as needed