Package com.tnsai.llm.routing
Class CapabilityRouter
java.lang.Object
com.tnsai.llm.routing.CapabilityRouter
- All Implemented Interfaces:
com.tnsai.llm.LLMClient,LLMRouter
Routes LLM requests based on required capabilities.
CapabilityRouter automatically selects the appropriate model based on the capabilities needed for each request. This is useful for multi-modal applications where some requests need vision and others don't.
Usage Example
// Create router with different capability profiles
CapabilityRouter router = CapabilityRouter.builder()
.addClient(new OpenAIClient("gpt-4o")) // Vision + Tools
.addClient(new GroqClient("llama-3.3-70b")) // Tools only
.addClient(new OllamaClient("llama3.2")) // Basic text
.build();
// Select vision-capable model for image inputs
LLMClient visionClient = router.selectByCapability(cap -> cap.supportsVision())
.orElseThrow();
// Select model with function calling for tool use
LLMClient toolClient = router.selectByCapability(cap -> cap.supportsFunctionCalling())
.orElseThrow();
Default Capability Selection
// Set default capability requirement
CapabilityRouter router = CapabilityRouter.builder()
.addClient(new OpenAIClient("gpt-4o"))
.addClient(new GroqClient("llama-3.3-70b"))
.defaultRequirement(cap -> cap.supportsFunctionCalling())
.build();
// All requests go to function-calling capable models
router.chat("Use the search tool");
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for CapabilityRouter.Nested classes/interfaces inherited from interface com.tnsai.llm.routing.LLMRouter
LLMRouter.RoutingStats -
Method Summary
Modifier and TypeMethodDescriptionstatic CapabilityRouter.Builderbuilder()Returns a builder for configuring the router.com.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Routes a chat request to the appropriate client.com.tnsai.llm.LLMCapabilitiesList<com.tnsai.llm.LLMClient> Returns all registered LLM clients.getModel()com.tnsai.llm.LLMClientReturns the currently selected/primary client.getStats()Returns routing statistics (calls per provider, failures, latencies).floatstatic CapabilityRouterof(com.tnsai.llm.LLMClient... clients) Creates a CapabilityRouter with the given clients.voidResets routing statistics.Optional<com.tnsai.llm.LLMClient> selectByCapability(Predicate<com.tnsai.llm.LLMCapabilities> filter) Selects a client that matches the given capability predicate.Optional<com.tnsai.llm.LLMClient> Selects the best client for structured output (JSON mode).Optional<com.tnsai.llm.LLMClient> Selects the best client for tool/function calling.Optional<com.tnsai.llm.LLMClient> Selects the best client for vision tasks.Optional<com.tnsai.llm.LLMClient> selectWithMinContext(int minTokens) Selects a client with minimum context window size.streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Stream<com.tnsai.llm.ChatChunk> streamChatWithSpec(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, chat, getProviderName, getTopP, listModels, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec
-
Method Details
-
of
Creates a CapabilityRouter with the given clients. -
builder
Returns a builder for configuring the router. -
getClients
Description copied from interface:LLMRouterReturns all registered LLM clients.- Specified by:
getClientsin interfaceLLMRouter
-
getPrimaryClient
public com.tnsai.llm.LLMClient getPrimaryClient()Description copied from interface:LLMRouterReturns the currently selected/primary client.- Specified by:
getPrimaryClientin interfaceLLMRouter
-
selectByCapability
public Optional<com.tnsai.llm.LLMClient> selectByCapability(Predicate<com.tnsai.llm.LLMCapabilities> filter) Description copied from interface:LLMRouterSelects a client that matches the given capability predicate.- Specified by:
selectByCapabilityin interfaceLLMRouter- Parameters:
filter- Predicate to filter by capabilities- Returns:
- The first matching client, or empty if none match
-
selectVisionCapable
Selects the best client for vision tasks. -
selectToolCapable
Selects the best client for tool/function calling. -
selectStructuredOutputCapable
Selects the best client for structured output (JSON mode). -
selectWithMinContext
Selects a client with minimum context window size. -
getModel
- Specified by:
getModelin interfacecom.tnsai.llm.LLMClient
-
getTemperature
public float getTemperature()- Specified by:
getTemperaturein interfacecom.tnsai.llm.LLMClient
-
getMaxTokens
- Specified by:
getMaxTokensin interfacecom.tnsai.llm.LLMClient
-
getCapabilities
public com.tnsai.llm.LLMCapabilities getCapabilities()- Specified by:
getCapabilitiesin interfacecom.tnsai.llm.LLMClient
-
chat
public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Description copied from interface:LLMRouterRoutes a chat request to the appropriate client.The routing decision depends on the implementation:
- FallbackRouter: Uses primary, falls back on error
- CostBasedRouter: Selects cheapest viable option
- LatencyBasedRouter: Selects fastest option
-
streamChat
public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatin interfacecom.tnsai.llm.LLMClient
-
streamChatWithSpec
public Stream<com.tnsai.llm.ChatChunk> streamChatWithSpec(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatWithSpecin interfacecom.tnsai.llm.LLMClient
-
getStats
Description copied from interface:LLMRouterReturns routing statistics (calls per provider, failures, latencies). -
resetStats
public void resetStats()Description copied from interface:LLMRouterResets routing statistics.- Specified by:
resetStatsin interfaceLLMRouter
-