Package com.tnsai.llm
Class LLMResolver.Builder
java.lang.Object
com.tnsai.llm.LLMResolver.Builder
- Enclosing class:
LLMResolver
Builder for LLM resolution with full fallback chain support.
Supports the hierarchy:
- Role's LLM config (requires configFactory)
- Agent's LLM
Example
LLMClient llm = LLMResolver.builder()
.roleLLMSpec(() -> role.getLLMSpec().orElse(null))
.agentLLM(agent::llm)
.configFactory(cfg -> new OpenAIClient(cfg.model(), cfg.temperature()))
.context("analyze action")
.resolve();
-
Method Summary
Modifier and TypeMethodDescriptionSets the agent's LLM supplier.configFactory(Function<LLMSpecSource, LLMClient> factory) Sets the factory for creating LLMClient from LLM configuration.Sets context for error messages.resolve()Resolves the LLM using the fallback chain.Resolves the LLM, returning empty if none found.Resolves the LLM with source tracking.roleLLMSpec(Supplier<? extends LLMSpecSource> supplier) Sets the role's LLM config supplier.
-
Method Details
-
roleLLMSpec
Sets the role's LLM config supplier.Called lazily. If returns non-null, requires
configFactory(Function)to convert to LLMClient. If returns null, falls back to agent's LLM.Accepts any
LLMSpecSourcesupplier.- Parameters:
supplier- supplies the role's LLM config (may return null)- Returns:
- this builder
-
agentLLM
Sets the agent's LLM supplier.Called lazily only if role has no LLM config.
- Parameters:
supplier- supplies the agent's LLM (may return null)- Returns:
- this builder
-
configFactory
Sets the factory for creating LLMClient from LLM configuration.Required if actionLLMSpec or roleLLMSpec is used. The factory receives the config (model, temperature) and should return an appropriate LLMClient.
The factory receives
LLMSpecSource, allowing it to handle both basicLLMResolver.LLMSpecand extended types like PortableLLMSpec.Example
.configFactory(cfg -> { OpenAIClient.Builder builder = OpenAIClient.builder() .model(cfg.model()); if (cfg.hasTemperature()) { builder.temperature(cfg.temperature()); } return builder.build(); })- Parameters:
factory- function that creates LLMClient from config- Returns:
- this builder
-
context
Sets context for error messages.- Parameters:
context- descriptive context (e.g., action name)- Returns:
- this builder
-
resolve
Resolves the LLM using the fallback chain.- Returns:
- the resolved LLM client
- Throws:
IllegalStateException- if no LLM found in chain
-
resolveOptional
Resolves the LLM, returning empty if none found.- Returns:
- Optional containing the LLM, or empty
-
resolveWithSource
Resolves the LLM with source tracking.Use this method when you need to know where the LLM came from (for logging, debugging, or conditional logic).
- Returns:
- resolution result with LLM and source
-