Class OpenAIClient

java.lang.Object
com.tnsai.llm.providers.AbstractLLMClient
com.tnsai.llm.providers.OpenAIClient
All Implemented Interfaces:
JsonCapableLLMClient, com.tnsai.llm.LLMClient

public class OpenAIClient extends AbstractLLMClient implements JsonCapableLLMClient
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 Details

    • OpenAIClient

      public OpenAIClient(String model)
      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

      public OpenAIClient(String model, float temperature)
      Creates an OpenAIClient with the specified model and temperature. API key is read from OPENAI_API_KEY environment variable.
      Parameters:
      model - The model to use
      temperature - The temperature for response generation (0.0-2.0)
    • OpenAIClient

      public OpenAIClient(String model, float temperature, Integer maxTokens)
      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 use
      temperature - The temperature for response generation (0.0-2.0)
      maxTokens - The maximum number of tokens to generate
    • OpenAIClient

      public OpenAIClient(String model, float temperature, Float topP, Integer maxTokens)
      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 use
      temperature - 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 use
      temperature - 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 generate
      apiKey - The API key (null to read from environment)
      organizationId - The organization ID (null to read from environment)
    • OpenAIClient

      public OpenAIClient(String baseUrl, String model, float temperature, Float topP, Integer maxTokens, String apiKey, String organizationId, LLMClientSettings settings)
  • Method Details