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 Type
    Method
    Description
    default com.tnsai.llm.ChatResponse
    chatJson(String message)
    Simplified chat with JSON object format.
    default com.tnsai.llm.ChatResponse
    chatJson(String message, String systemPrompt)
    Simplified chat with JSON object format and system prompt.
    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.
    default com.tnsai.llm.ChatResponse
    chatWithSchema(String message, String schemaName, Class<?> clazz)
    Chat with JSON Schema generated from a class.
    default com.tnsai.llm.ChatResponse
    chatWithSchema(String message, String schemaName, Map<String,Object> schema)
    Chat with JSON Schema format.
    default boolean
    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 message
      systemPrompt - Optional system prompt
      history - Optional conversation history
      tools - Optional list of available tools
      format - The desired response format
      Returns:
      The LLM response
    • supportsFormat

      default boolean supportsFormat(ResponseFormat format)
      Checks if this client supports the given response format.
      Parameters:
      format - The format to check
      Returns:
      true if the format is supported
    • chatJson

      default com.tnsai.llm.ChatResponse chatJson(String message)
      Simplified chat with JSON object format.
      Parameters:
      message - The user message
      Returns:
      ChatResponse with JSON content
    • chatJson

      default com.tnsai.llm.ChatResponse chatJson(String message, String systemPrompt)
      Simplified chat with JSON object format and system prompt.
      Parameters:
      message - The user message
      systemPrompt - 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 message
      schemaName - Name for the schema
      schema - 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 message
      schemaName - Name for the schema
      clazz - Class to generate schema from
      Returns:
      ChatResponse with JSON content matching the class structure