Package com.tnsai.llm.providers
Class OpenAIClient
java.lang.Object
com.tnsai.llm.providers.AbstractLLMClient
com.tnsai.llm.providers.OpenAIClient
- All Implemented Interfaces:
JsonCapableLLMClient,com.tnsai.llm.LLMClient
OpenAI LLM client implementation.
Provides access to OpenAI's GPT models via the Chat Completions API.
Supported models:
- gpt-4o - GPT-4o (recommended, multimodal)
- gpt-4o-mini - GPT-4o Mini (fast and affordable)
- gpt-4-turbo - GPT-4 Turbo
- gpt-4 - GPT-4
- gpt-3.5-turbo - GPT-3.5 Turbo (fast)
- o1-preview - OpenAI o1 (reasoning)
- o1-mini - OpenAI o1 Mini (reasoning, fast)
Includes built-in retry mechanism for resilience against network issues and transient API errors.
Configuration via environment variables:
- OPENAI_API_KEY - Required API key for authentication
- OPENAI_BASE_URL - Optional, defaults to https://api.openai.com/v1
- OPENAI_ORG_ID - Optional organization ID
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionOpenAIClient(String model) Creates an OpenAIClient with the specified model.OpenAIClient(String model, float temperature) Creates an OpenAIClient with the specified model and temperature.OpenAIClient(String model, float temperature, Float topP, Integer maxTokens) Creates an OpenAIClient with the specified model, temperature, topP, and max tokens.OpenAIClient(String model, float temperature, Integer maxTokens) Creates an OpenAIClient with the specified model, temperature, and max tokens.OpenAIClient(String baseUrl, String model, float temperature, Float topP, Integer maxTokens, String apiKey, String organizationId) Creates an OpenAIClient with full configuration.OpenAIClient(String baseUrl, String model, float temperature, Float topP, Integer maxTokens, String apiKey, String organizationId, LLMClientSettings settings) -
Method Summary
Modifier and TypeMethodDescriptioncom.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) 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.floatfloatList<com.tnsai.llm.ModelInfo> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) booleansupportsFormat(ResponseFormat format) Checks if this client supports the given response format.Methods inherited from class com.tnsai.llm.providers.AbstractLLMClient
getBaseUrl, getCallTimeoutMs, getMaxTokens, getModel, getTemperature, getTopPMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.format.JsonCapableLLMClient
chatJson, chatJson, chatWithSchema, chatWithSchemaMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, chat, getCapabilities, getMaxTokens, getModel, getTemperature, getTopP, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec, streamChatWithSpec
-
Constructor Details
-
OpenAIClient
Creates an OpenAIClient with the specified model. API key is read from OPENAI_API_KEY environment variable.- Parameters:
model- The model to use (e.g., "gpt-4o")
-
OpenAIClient
Creates an OpenAIClient with the specified model and temperature. API key is read from OPENAI_API_KEY environment variable.- Parameters:
model- The model to usetemperature- The temperature for response generation (0.0-2.0)
-
OpenAIClient
Creates an OpenAIClient with the specified model, temperature, and max tokens. API key is read from OPENAI_API_KEY environment variable.- Parameters:
model- The model to usetemperature- The temperature for response generation (0.0-2.0)maxTokens- The maximum number of tokens to generate
-
OpenAIClient
Creates an OpenAIClient with the specified model, temperature, topP, and max tokens. API key is read from OPENAI_API_KEY environment variable.- Parameters:
model- The model to usetemperature- The temperature for response generation (0.0-2.0)topP- The top-p (nucleus sampling) value (0.0-1.0)maxTokens- The maximum number of tokens to generate
-
OpenAIClient
public OpenAIClient(String baseUrl, String model, float temperature, Float topP, Integer maxTokens, String apiKey, String organizationId) Creates an OpenAIClient with full configuration.- Parameters:
baseUrl- The base URL for the API (null for default)model- The model to usetemperature- The temperature for response generation (0.0-2.0)topP- The top-p (nucleus sampling) value (0.0-1.0, null for default)maxTokens- The maximum number of tokens to generateapiKey- The API key (null to read from environment)organizationId- The organization ID (null to read from environment)
-
OpenAIClient
-
-
Method Details
-
getFrequencyPenalty
public float getFrequencyPenalty() -
getPresencePenalty
public float getPresencePenalty() -
chat
public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
chatin interfacecom.tnsai.llm.LLMClient
-
streamChat
public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatin interfacecom.tnsai.llm.LLMClient
-
chatWithFormat
public com.tnsai.llm.ChatResponse chatWithFormat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools, ResponseFormat format) Description copied from interface:JsonCapableLLMClientSends a chat message with the specified response format.- Specified by:
chatWithFormatin interfaceJsonCapableLLMClient- 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
Description copied from interface:JsonCapableLLMClientChecks if this client supports the given response format.- Specified by:
supportsFormatin interfaceJsonCapableLLMClient- Parameters:
format- The format to check- Returns:
- true if the format is supported
-
listModels
- Specified by:
listModelsin interfacecom.tnsai.llm.LLMClient
-
getProviderName
- Specified by:
getProviderNamein interfacecom.tnsai.llm.LLMClient
-