Package com.tnsai.llm
Class ParallelLLM
java.lang.Object
com.tnsai.llm.ParallelLLM
Parallel LLM execution utilities.
Provides fan-out, scatter-gather, and consensus patterns for LLM calls.
Example usage:
// Fan-out to multiple LLMs
List<LLMClient> llms = List.of(deepseek, qwen, gemini);
List<String> responses = ParallelLLM.fanOut(prompt, llms);
// Consensus from multiple LLMs
String bestResponse = ParallelLLM.consensus(prompt, llms, scorer);
// Race - first response wins
String fastest = ParallelLLM.race(prompt, llms, 30, TimeUnit.SECONDS);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordResult of a parallel LLM call.static final recordAggregated results from multiple LLMs. -
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<ParallelLLM.LLMResult> Gets responses from multiple LLMs and selects the best one using a scoring function.Gets responses from multiple LLMs and combines them using an aggregator LLM.static ParallelLLM.ParallelResultsSends the same prompt to multiple LLMs in parallel and collects all responses.static ParallelLLM.ParallelResultsSends the same prompt to multiple LLMs in parallel with timeout.static Optional<ParallelLLM.LLMResult> majorityVote(String prompt, List<LLMClient> llms) Gets responses from multiple LLMs and selects by majority voting.static Optional<ParallelLLM.LLMResult> Sends prompt to multiple LLMs, returns the first successful response.
-
Method Details
-
fanOut
Sends the same prompt to multiple LLMs in parallel and collects all responses.- Parameters:
prompt- The prompt to sendllms- List of LLM clients- Returns:
- Aggregated results from all LLMs
-
fanOut
public static ParallelLLM.ParallelResults fanOut(String prompt, List<LLMClient> llms, long timeout, TimeUnit unit) Sends the same prompt to multiple LLMs in parallel with timeout.- Parameters:
prompt- The prompt to sendllms- List of LLM clientstimeout- Timeout valueunit- Timeout unit- Returns:
- Aggregated results from all LLMs
-
race
public static Optional<ParallelLLM.LLMResult> race(String prompt, List<LLMClient> llms, long timeout, TimeUnit unit) Sends prompt to multiple LLMs, returns the first successful response.- Parameters:
prompt- The prompt to sendllms- List of LLM clientstimeout- Timeout valueunit- Timeout unit- Returns:
- First successful response, or empty if all fail/timeout
-
consensus
public static Optional<ParallelLLM.LLMResult> consensus(String prompt, List<LLMClient> llms, Function<String, Double> scorer) Gets responses from multiple LLMs and selects the best one using a scoring function.- Parameters:
prompt- The prompt to sendllms- List of LLM clientsscorer- Function to score each response (higher is better)- Returns:
- Best response based on scoring
-
majorityVote
Gets responses from multiple LLMs and selects by majority voting. Uses similarity to find the most common response pattern.- Parameters:
prompt- The prompt to sendllms- List of LLM clients- Returns:
- Most common response pattern
-
ensemble
Gets responses from multiple LLMs and combines them using an aggregator LLM.- Parameters:
prompt- The original promptllms- List of LLM clients for initial responsesaggregator- LLM client to synthesize final response- Returns:
- Synthesized response
-