Package com.tnsai.agents.execution
Class LLMOrchestrator
java.lang.Object
com.tnsai.agents.execution.LLMOrchestrator
Orchestrates LLM communication for agents.
This class is extracted from Agent to provide a focused component responsible for all LLM-related operations:
- Invoking LLM with various configurations
- Processing LLM responses (text and tool calls)
- Managing conversation history in memory store
- Supporting streaming responses
- Handling spinner display during LLM calls
Design Principles
- Single responsibility: LLM communication only
- Stateless operations: receives context via method parameters
- Callback-based: delegates tool execution back to agent
Usage
LLMOrchestrator orchestrator = new LLMOrchestrator(
agentName,
llmClient,
memoryStore
);
// Specure callbacks
orchestrator.setToolCallHandler(chatResponse ->
toolExecutor.executeToolCalls(chatResponse, true));
orchestrator.setSystemPromptSupplier(() -> generateSystemPrompt());
orchestrator.setToolsSupplier(() -> getAvailableTools());
// Invoke LLM
String response = orchestrator.chat("Hello", true);
// Streaming
orchestrator.streamChat("Tell me a story")
.forEach(System.out::print);
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for handling tool calls. -
Constructor Summary
ConstructorsConstructorDescriptionLLMOrchestrator(String agentName, LLMClient llm, MemoryStore memoryStore) Creates a new LLMOrchestrator. -
Method Summary
Modifier and TypeMethodDescriptionSends a message to the LLM and returns the response.Sends a message with full configuration options.chatInternal(String message, boolean useHistory) Internal chat without tool execution.voidClears the conversation history.Returns the agent name.Returns the current conversation history.getLLM()Returns the configured LLM client.Returns the memory store.setMaxContextTokens(Integer maxTokens) Sets the maximum context token limit.setSpinnerEnabled(boolean enabled) Enables or disables spinner during LLM calls.setSystemPromptSupplier(Supplier<String> supplier) Sets the system prompt supplier.Sets the tool call handler.Sets the tools supplier.streamChat(String message) Sends a message and returns a streaming response.voidstreamChatWithConsumer(String message, Consumer<String> tokenConsumer) Sends a message with streaming and custom event handling.
-
Constructor Details
-
LLMOrchestrator
Creates a new LLMOrchestrator.- Parameters:
agentName- the agent name for logging and tracingllm- the LLM client to usememoryStore- the memory store for conversation history
-
-
Method Details
-
setMaxContextTokens
Sets the maximum context token limit. When set, conversation history is automatically pruned to stay within this limit.- Parameters:
maxTokens- maximum tokens (null to disable pruning)- Returns:
- this for chaining
-
setSystemPromptSupplier
Sets the system prompt supplier.- Parameters:
supplier- function that returns the current system prompt- Returns:
- this for chaining
-
setToolsSupplier
Sets the tools supplier.- Parameters:
supplier- function that returns available tools for the LLM- Returns:
- this for chaining
-
setToolCallHandler
Sets the tool call handler.- Parameters:
handler- function to execute tool calls- Returns:
- this for chaining
-
setSpinnerEnabled
Enables or disables spinner during LLM calls.- Parameters:
enabled- true to show spinner- Returns:
- this for chaining
-
chat
Sends a message to the LLM and returns the response.This method:
- Adds the user message to history (if useHistory is true)
- Prunes history if token limit is set
- Invokes the LLM with system prompt, history, and tools
- Processes the response (handles tool calls if configured)
- Adds the assistant response to history
- Parameters:
message- the user messageuseHistory- whether to include conversation history- Returns:
- the LLM response
-
chat
Sends a message with full configuration options.- Parameters:
message- the user messageuseHistory- whether to include conversation historyuseTools- whether to provide tools to the LLMtrace- whether to trace the request/response- Returns:
- the LLM response
-
chatInternal
Internal chat without tool execution. Prevents recursion when called from tool execution handlers.- Parameters:
message- the messageuseHistory- whether to use history- Returns:
- the response
-
streamChat
Sends a message and returns a streaming response.Unlike
chat(String, boolean), this method returns a Stream that yields response tokens as they are generated.Note: Tool calling is not supported in streaming mode.
- Parameters:
message- the user message- Returns:
- stream of response tokens
-
streamChatWithConsumer
Sends a message with streaming and custom event handling.- Parameters:
message- the user messagetokenConsumer- consumer for each token
-
getHistory
Returns the current conversation history.- Returns:
- list of message maps
-
clearHistory
public void clearHistory()Clears the conversation history. -
getLLM
Returns the configured LLM client.- Returns:
- the LLM client
-
getMemoryStore
Returns the memory store.- Returns:
- the memory store
-
getAgentName
Returns the agent name.- Returns:
- the agent name
-