Package com.tnsai.llm.format
Interface JsonCapableLLMClient
- All Superinterfaces:
com.tnsai.llm.LLMClient
- All Known Implementing Classes:
MiniMaxClient,OpenAIClient,ZhipuAIClient
public interface JsonCapableLLMClient
extends com.tnsai.llm.LLMClient
Extended LLM client interface that supports native JSON mode.
Providers that support native JSON output format (OpenAI, Gemini) should implement this interface to enable structured output.
Example Implementation
public class OpenAIClient implements JsonCapableLLMClient {
@Override
public ChatResponse chatWithFormat(String message, Optional<String> systemPrompt,
Optional<List<Map<String, Object>>> history,
Optional<List<Map<String, Object>>> tools,
ResponseFormat format) {
// Add response_format to API request
requestBody.put("response_format", format.toOpenAIFormat());
// ... rest of implementation
}
}
Usage
if (client instanceof JsonCapableLLMClient jsonClient) {
ChatResponse response = jsonClient.chatWithFormat(
"List 3 colors",
Optional.empty(),
Optional.empty(),
Optional.empty(),
ResponseFormat.jsonObject()
);
}
- Author:
- TnsAI Team
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault com.tnsai.llm.ChatResponseSimplified chat with JSON object format.default com.tnsai.llm.ChatResponseSimplified chat with JSON object format and system prompt.com.tnsai.llm.ChatResponsechatWithFormat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools, ResponseFormat format) Sends a chat message with the specified response format.default com.tnsai.llm.ChatResponsechatWithSchema(String message, String schemaName, Class<?> clazz) Chat with JSON Schema generated from a class.default com.tnsai.llm.ChatResponseChat with JSON Schema format.default booleansupportsFormat(ResponseFormat format) Checks if this client supports the given response format.Methods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, chat, chat, getCapabilities, getMaxTokens, getModel, getProviderName, getTemperature, getTopP, listModels, streamChat, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec, streamChatWithSpec
-
Method Details
-
chatWithFormat
com.tnsai.llm.ChatResponse chatWithFormat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools, ResponseFormat format) Sends a chat message with the specified response format.- Parameters:
message- The user messagesystemPrompt- Optional system prompthistory- Optional conversation historytools- Optional list of available toolsformat- The desired response format- Returns:
- The LLM response
-
supportsFormat
Checks if this client supports the given response format.- Parameters:
format- The format to check- Returns:
- true if the format is supported
-
chatJson
Simplified chat with JSON object format.- Parameters:
message- The user message- Returns:
- ChatResponse with JSON content
-
chatJson
Simplified chat with JSON object format and system prompt.- Parameters:
message- The user messagesystemPrompt- The system prompt- Returns:
- ChatResponse with JSON content
-
chatWithSchema
default com.tnsai.llm.ChatResponse chatWithSchema(String message, String schemaName, Map<String, Object> schema) Chat with JSON Schema format.- Parameters:
message- The user messageschemaName- Name for the schemaschema- JSON Schema as a Map- Returns:
- ChatResponse with JSON content matching the schema
-
chatWithSchema
default com.tnsai.llm.ChatResponse chatWithSchema(String message, String schemaName, Class<?> clazz) Chat with JSON Schema generated from a class.- Parameters:
message- The user messageschemaName- Name for the schemaclazz- Class to generate schema from- Returns:
- ChatResponse with JSON content matching the class structure
-