Package com.tnsai.llm
Class FallbackLLMClient
java.lang.Object
com.tnsai.llm.FallbackLLMClient
- All Implemented Interfaces:
LLMClient
LLM client with automatic fallback support.
Wraps a primary LLM client with fallback options. If the primary fails, automatically retries with fallback clients.
Inspired by Mastra
Based on Mastra's auto model fallback pattern.
Usage
LLMClient client = FallbackLLMClient.builder()
.primary(new OpenAIClient("gpt-4o"))
.fallback(new AnthropicClient("claude-3-sonnet"))
.fallback(new OllamaClient("llama3.2"))
.retryOnPrimary(2)
.build();
// If gpt-4o fails after 2 retries, automatically tries claude, then llama
ChatResponse response = client.chat("Hello");
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for FallbackLLMClient. -
Method Summary
Modifier and TypeMethodDescriptionstatic FallbackLLMClient.Builderbuilder()Creates a new builder for FallbackLLMClient.chat(ChatRequest request) Sends a chat request using the unifiedChatRequestmodel.chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Sends a chat message and returns the response.Gets the fallback clients.Gets the maximum tokens setting.getModel()Gets the model name being used.Gets the primary client.floatGets the temperature setting.streamChat(ChatRequest request) Streams a chat response using the unifiedChatRequestmodel.streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Sends a chat message and streams the response token by token.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, getCapabilities, getProviderName, getTopP, listModels, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec, streamChatWithSpec
-
Method Details
-
builder
Creates a new builder for FallbackLLMClient. -
getModel
Description copied from interface:LLMClientGets the model name being used. -
getTemperature
public float getTemperature()Description copied from interface:LLMClientGets the temperature setting.- Specified by:
getTemperaturein interfaceLLMClient
-
getMaxTokens
Description copied from interface:LLMClientGets the maximum tokens setting.- Specified by:
getMaxTokensin interfaceLLMClient
-
chat
public ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Description copied from interface:LLMClientSends a chat message and returns the response.- Specified by:
chatin interfaceLLMClient- Parameters:
message- The user messagesystemPrompt- Optional system prompthistory- Optional conversation historytools- Optional list of available tools for function calling- Returns:
- The LLM response as ChatResponse (contains content and optional tool calls)
-
chat
Description copied from interface:LLMClientSends a chat request using the unifiedChatRequestmodel.Dispatches to the appropriate chat method based on request properties:
- Multimodal →
LLMClient.chat(List, Optional, Optional, Optional) - Temperature override →
LLMClient.chat(String, Optional, Optional, Optional, float) - Otherwise →
LLMClient.chat(String, Optional, Optional, Optional)
- Multimodal →
-
streamChat
Description copied from interface:LLMClientStreams a chat response using the unifiedChatRequestmodel.- Specified by:
streamChatin interfaceLLMClient- Parameters:
request- the chat request- Returns:
- stream of response tokens
-
streamChat
public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Description copied from interface:LLMClientSends a chat message and streams the response token by token.Important: The returned Stream holds an HTTP connection open. Callers must close the stream when done, ideally via try-with-resources:
Failing to close the stream will leak HTTP connections.try (Stream<String> tokens = client.streamChat(msg, sys, hist, tools)) { tokens.forEach(System.out::print); }- Specified by:
streamChatin interfaceLLMClient- Parameters:
message- The user messagesystemPrompt- Optional system prompthistory- Optional conversation historytools- Optional list of available tools for function calling- Returns:
- Stream of response tokens — caller must close
-
getPrimary
Gets the primary client. -
getFallbacks
Gets the fallback clients.
-