类 ModelRegistry

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

public final class ModelRegistry extends Object
Registry for resolving Model instances from string identifiers (named instances or provider:model patterns). User-registered factories take precedence over providers loaded from the ModelProvider SPI.

Provider extension modules read API keys from their own standard environment variables when auto-creating models.

  • 方法详细资料

    • register

      public static void register(String name, Model model)
      Registers a named Model instance. resolve(String) returns this instance for an exact name match (no caching of factory-created instances applies).
    • registerFactory

      public static void registerFactory(String modelNameRegex, ModelRegistry.ModelFactory factory)
      Registers a factory matched against the full modelId string using Pattern.matches(java.lang.String, java.lang.CharSequence). Newly registered factories are consulted before older user registrations and before SPI providers.
      参数:
      modelNameRegex - regex with semantics of Pattern#matches(CharSequence) on the full model id
      factory - creates a Model from the full model id
    • registerFactory

      public static void registerFactory(String modelNameRegex, ModelRegistry.ContextModelFactory factory)
      Registers a context-aware factory matched against the full modelId string using Pattern.matches(java.lang.String, java.lang.CharSequence). Newly registered factories are consulted before older user registrations and before SPI providers.
      参数:
      modelNameRegex - regex with semantics of Pattern#matches(CharSequence) on the full model id
      factory - creates a Model from the full model id and creation context
    • resolve

      public static Model resolve(String modelId)
      Resolves a Model for the given id: named registration first, then cached factory-created instance, then user factories (newest first), then SPI providers.
      抛出:
      IllegalArgumentException - if the id cannot be resolved or creation fails
    • resolve

      public static Model resolve(String modelId, ModelCreationContext context)
      Resolves a Model for the given id and creation context: named registration first, then cache when enabled, then user factories (newest first), then SPI providers.

      Non-empty context resolution is not cached unless the context uses CachePolicy.ENABLED.

      抛出:
      IllegalArgumentException - if the id cannot be resolved, the context cannot be safely cached, or creation fails
    • canResolve

      public static boolean canResolve(String modelId)
      Returns true if resolve(String) can find a named model or a matching factory/ provider pattern (without creating an instance).
    • canResolve

      public static boolean canResolve(String modelId, ModelCreationContext context)
      Returns true if resolve(String, ModelCreationContext) can find a named model or a matching factory/provider pattern (without creating an instance).
    • reset

      public static void reset()
      Clears named models, user-registered factories, the factory-resolution cache, and cached SPI providers. Intended for tests.
    • reloadProviders

      public static void reloadProviders()
      Clears the cached SPI providers so the next resolution can rediscover providers from the current classloader context.