Package com.tnsai.llm

Class ChatResponse

java.lang.Object
com.tnsai.llm.ChatResponse

@Immutable public final class ChatResponse extends Object
Represents a chat response from an LLM, potentially including tool calls.

ChatResponse is the unified response type returned by all LLMClient implementations. It contains the text content generated by the model and optionally a list of tool calls the model wants to execute.

Response Types

A ChatResponse can represent:

  • Text-only response: Model generates text content
  • Tool call response: Model requests tool execution (may also include text)
  • Empty response: Content is empty string, no tool calls

Usage Examples

Basic Usage


 LLMClient client = new OllamaClient("llama3.2");
 ChatResponse response = client.chat("What is 2+2?");

 // Get the text content
 String answer = response.getContent();
 System.out.println(answer); // "2+2 equals 4"
 

Handling Tool Calls


 ChatResponse response = client.chat("Search for weather", history, tools);

 if (response.hasToolCalls()) {
     for (ChatResponse.ToolCall call : response.getToolCalls()) {
         String toolName = call.getName();           // e.g., "search"
         Map<String, Object> args = call.getArguments(); // e.g., {"query": "weather"}

         // Execute the tool
         Object result = toolExecutor.execute(toolName, args);

         // Continue conversation with tool result
         history.add(Map.of("role", "tool", "content", result.toString()));
     }
 }
 

Factory Methods


 // Text-only response
 ChatResponse textResponse = ChatResponse.textOnly("Hello, world!");

 // Response with tool calls
 ChatResponse toolResponse = ChatResponse.withToolCalls(
     "I'll search for that.",
     List.of(new ToolCall("search", Map.of("query", "weather")))
 );
 

Immutability

ChatResponse and ToolCall are immutable value objects. All collections are defensively copied on construction.

JSON Serialization

ChatResponse can be serialized/deserialized using Jackson:


 ObjectMapper mapper = new ObjectMapper();
 String json = mapper.writeValueAsString(response);
 ChatResponse restored = mapper.readValue(json, ChatResponse.class);
 
See Also:
  • Constructor Details

  • Method Details

    • textOnly

      public static ChatResponse textOnly(String content)
    • withToolCalls

      public static ChatResponse withToolCalls(String content, List<ChatResponse.ToolCall> toolCalls)
    • withUsage

      public static ChatResponse withUsage(String content, List<ChatResponse.ToolCall> toolCalls, int inputTokens, int outputTokens)
      Creates a response with usage metrics.
    • withCacheUsage

      public static ChatResponse withCacheUsage(String content, List<ChatResponse.ToolCall> toolCalls, int inputTokens, int outputTokens, int cacheCreationTokens, int cacheReadTokens)
      Creates a response with cache usage metrics.
    • hasToolCalls

      public boolean hasToolCalls()
    • getInputTokens

      public Optional<Integer> getInputTokens()
      Gets input token count if available.
    • getOutputTokens

      public Optional<Integer> getOutputTokens()
      Gets output token count if available.
    • getCacheCreationInputTokens

      public Optional<Integer> getCacheCreationInputTokens()
      Gets cache creation input tokens if available.
    • getCacheReadInputTokens

      public Optional<Integer> getCacheReadInputTokens()
      Gets cache read input tokens if available.
    • hasCacheUsage

      public boolean hasCacheUsage()
      Checks if this response has cache usage information.