类 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 built-in providers.

Built-in providers read API keys from standard environment variables when auto-creating models: OPENAI_API_KEY, DASHSCOPE_API_KEY, GEMINI_API_KEY, ANTHROPIC_API_KEY (optional for Anthropic SDK), OLLAMA_BASE_URL (optional, defaults to http://localhost:11434).

  • 方法详细资料

    • 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 built-in providers.
      参数:
      modelNameRegex - regex with semantics of Pattern#matches(CharSequence) on the full model id
      factory - creates a Model from the full model id
    • 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 built-in factories.
      抛出:
      IllegalArgumentException - if the id cannot be resolved or creation fails
    • canResolve

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

      public static void reset()
      Clears named models, user-registered factories, and the factory-resolution cache. Built-in provider rules are preserved. Intended for tests.