Package com.tnsai.llm
Class ChatResponse
java.lang.Object
com.tnsai.llm.ChatResponse
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a tool/function call from the LLM. -
Constructor Summary
ConstructorsConstructorDescriptionChatResponse(String content, List<ChatResponse.ToolCall> toolCalls) ChatResponse(String content, List<ChatResponse.ToolCall> toolCalls, Integer inputTokens, Integer outputTokens, Integer cacheCreationInputTokens, Integer cacheReadInputTokens) -
Method Summary
Modifier and TypeMethodDescriptionGets cache creation input tokens if available.Gets cache read input tokens if available.Gets input token count if available.Gets output token count if available.booleanChecks if this response has cache usage information.booleanstatic ChatResponsestatic ChatResponsewithCacheUsage(String content, List<ChatResponse.ToolCall> toolCalls, int inputTokens, int outputTokens, int cacheCreationTokens, int cacheReadTokens) Creates a response with cache usage metrics.static ChatResponsewithToolCalls(String content, List<ChatResponse.ToolCall> toolCalls) static ChatResponsewithUsage(String content, List<ChatResponse.ToolCall> toolCalls, int inputTokens, int outputTokens) Creates a response with usage metrics.
-
Constructor Details
-
ChatResponse
-
ChatResponse
-
-
Method Details
-
textOnly
-
withToolCalls
-
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
Gets input token count if available. -
getOutputTokens
Gets output token count if available. -
getCacheCreationInputTokens
Gets cache creation input tokens if available. -
getCacheReadInputTokens
Gets cache read input tokens if available. -
hasCacheUsage
public boolean hasCacheUsage()Checks if this response has cache usage information.
-