Class LlmBackendResolver

java.lang.Object
org.mockserver.llm.client.LlmBackendResolver

public class LlmBackendResolver extends Object
Resolves which LlmBackend a runtime-LLM feature should use, across the three config layers (simplest first):
  1. Provider env conventions — auto-detect from OPENAI_API_KEY / ANTHROPIC_API_KEY / GEMINI_API_KEY / OLLAMA_HOST (the same vars each provider SDK reads).
  2. MockServer properties — a single default backend via mockserver.llmProvider / llmApiKey / llmModel / llmBaseUrl.
  3. Named backends JSONmockserver.llmBackendsConfig points at a JSON array of backends, selectable by name.
The default backend prefers layer 2 (explicit properties) over layer 1 (env); named backends come from layer 3. If nothing resolves, runtime-LLM features are simply unavailable — they fall back to deterministic behaviour.

The environment lookup is injected so the resolver is unit-testable without mutating the real process environment.

  • Constructor Details

    • LlmBackendResolver

      public LlmBackendResolver()
    • LlmBackendResolver

      public LlmBackendResolver(Function<String,String> env)
  • Method Details

    • resolveDefault

      public Optional<LlmBackend> resolveDefault()
      Resolve the default backend: explicit MockServer properties (layer 2) if a provider is configured, otherwise an env-detected backend (layer 1), otherwise empty.
    • resolveByName

      public Optional<LlmBackend> resolveByName(String name)
      Resolve a named backend from the layer-3 JSON file. Empty if no file is configured, the file cannot be read, or no entry matches the name.
    • namedBackends

      public Map<String,LlmBackend> namedBackends()
      All named backends from the layer-3 JSON file, keyed by name. Empty if not configured or unreadable (a parse error is logged, not thrown).