Class: Light::OpenAI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/light/openai/client.rb

Overview

Minimal chat client that wraps the OpenAI API with retry/backoff logic.

Constant Summary collapse

DEFAULT_API_URL =
ENV.fetch("OPENAI_CHAT_COMPLETIONS_URL", "https://api.openai.com/v1/chat/completions").freeze
DEFAULT_TIMEOUT =
Integer(ENV.fetch("OPENAI_HTTP_TIMEOUT", 900))
DEFAULT_RETRIES =
Integer(ENV.fetch("OPENAI_HTTP_RETRIES", 2))
DEFAULT_RETRY_INTERVAL =
Float(ENV.fetch("OPENAI_HTTP_RETRY_INTERVAL", 1.5))

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, api_url: DEFAULT_API_URL, timeout: DEFAULT_TIMEOUT, retries: DEFAULT_RETRIES, retry_interval: DEFAULT_RETRY_INTERVAL, logger: nil) ⇒ Client

Returns a new instance of Client.

Raises:



18
19
20
21
22
23
24
25
26
27
# File 'lib/light/openai/client.rb', line 18

def initialize(api_key:, api_url: DEFAULT_API_URL, timeout: DEFAULT_TIMEOUT, retries: DEFAULT_RETRIES, retry_interval: DEFAULT_RETRY_INTERVAL, logger: nil)
  raise ArgumentError, "api_key is required for OpenAIClient" if api_key.nil? || api_key.empty?

  @api_key = api_key
  @api_url = api_url
  @timeout = timeout
  @retries = [retries.to_i, 0].max
  @retry_interval = [retry_interval.to_f, 0].max
  @logger = logger
end

Instance Method Details

#chat(model:, messages:, temperature: 1, response_format: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/light/openai/client.rb', line 29

def chat(model:, messages:, temperature: 1, response_format: nil)
  payload = {
    model: model,
    messages: messages
  }
  
  # GPT-5 models don't support temperature parameter
  unless model.to_s.start_with?('gpt-5')
    payload[:temperature] = temperature
  end
  
  payload[:response_format] = response_format if response_format

  request(payload: payload, allow_fallback: !response_format.nil?)
end