Class: RubyLLM::Providers::OpenAI

Inherits:
RubyLLM::Provider show all
Defined in:
lib/ruby_llm/providers/openai.rb,
lib/ruby_llm/providers/openai/models.rb,
lib/ruby_llm/providers/openai/responses.rb,
lib/ruby_llm/providers/openai/capabilities.rb

Overview

OpenAI API integration.

Defined Under Namespace

Modules: Capabilities, Models Classes: Responses

Constant Summary collapse

RATE_LIMIT_RESET_HEADERS =
%w[x-ratelimit-reset-requests x-ratelimit-reset-tokens].freeze
RESET_DURATION_UNITS =
{ 'h' => 3600, 'm' => 60, 's' => 1, 'ms' => 0.001 }.freeze

Constants included from Support::Inspectable

Support::Inspectable::TRUNCATE_AT

Instance Attribute Summary

Attributes inherited from RubyLLM::Provider

#config, #connection

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RubyLLM::Provider

#animate_later, assume_models_exist?, #assume_models_exist?, #batch_cost, #batch_cost_amounts, #batch_protocol_name, #batch_status, #batches?, #cache_content, #cancel_batch, #capabilities, #compact, #complete, #configuration_requirements, #configured?, configured?, configured_providers, configured_remote_providers, #count_tokens, #create_batch, #delete_cache, display_name, #download_file, #embed, #extend_cache, #files?, #find_cache, #find_file, #find_research_job, #initialize, #list_file_uris, #list_models, #local?, local?, local_providers, #long_context_pricing?, model_registry_files, model_required?, models_dev_model_id, #moderate, #name, #ocr, #paint, #parse_error, #preprocess_message, protocol, #protocols, protocols, providers, register, remote?, remote_providers, #render, #render_embedding, #rerank, #research_later, resolve, resolve!, resolve_registry_id, #slug, #speak, #tokenize, #tool_approval_response, #transcribe, #upload_file

Methods included from Support::Inspectable

#full_inspect, #inspect, #pretty_print

Constructor Details

This class inherits a constructor from RubyLLM::Provider

Class Method Details

.capabilitiesObject



58
59
60
# File 'lib/ruby_llm/providers/openai.rb', line 58

def capabilities
  OpenAI::Capabilities
end

.configuration_optionsObject



66
67
68
69
70
71
72
73
74
# File 'lib/ruby_llm/providers/openai.rb', line 66

def configuration_options
  %i[
    openai_api_key
    openai_api_base
    openai_organization_id
    openai_project_id
    openai_use_system_role
  ]
end

.configuration_requirementsObject



76
77
78
# File 'lib/ruby_llm/providers/openai.rb', line 76

def configuration_requirements
  %i[openai_api_key]
end

.models_dev_aliasObject



62
63
64
# File 'lib/ruby_llm/providers/openai.rb', line 62

def models_dev_alias(...)
  OpenAI::Models.models_dev_alias(...)
end

Instance Method Details

#api_baseObject



16
17
18
# File 'lib/ruby_llm/providers/openai.rb', line 16

def api_base
  @config.openai_api_base || 'https://api.openai.com/v1'
end

#batch_cost_multiplierObject



55
# File 'lib/ruby_llm/providers/openai.rb', line 55

def batch_cost_multiplier(**) = 0.5

#batch_results(id, batch_protocol: nil) ⇒ Object



51
52
53
# File 'lib/ruby_llm/providers/openai.rb', line 51

def batch_results(id, batch_protocol: nil)
  super(id, batch_protocol: resolve_batch_protocol(batch_protocol) || batch_protocol_for_stored_batch(id))
end

#find_batch(id) ⇒ Object



44
45
46
47
48
49
# File 'lib/ruby_llm/providers/openai.rb', line 44

def find_batch(id)
  batch = super
  protocol = batch_protocol_for_endpoint(batch[:endpoint])

  protocol ? batch.merge(batch_protocol: protocol) : batch
end

#headersObject



27
28
29
30
31
32
33
# File 'lib/ruby_llm/providers/openai.rb', line 27

def headers
  {
    'Authorization' => "Bearer #{@config.openai_api_key}",
    'OpenAI-Organization' => @config.openai_organization_id,
    'OpenAI-Project' => @config.openai_project_id
  }.compact
end

#protocol_for(model) ⇒ Object

Audio, realtime, and dedicated search models only exist on Chat Completions.



21
22
23
24
25
# File 'lib/ruby_llm/providers/openai.rb', line 21

def protocol_for(model, **)
  return protocols[:chat_completions] if Capabilities::SEARCH_MODELS.include?(model.id)

  model.id.match?(/audio|realtime/) ? protocols[:chat_completions] : super
end

#retry_delay(response) ⇒ Object

OpenAI reports when each rate limit resets in its own headers, as durations like "6m0s", "7.66s" or "76ms".



37
38
39
40
41
42
# File 'lib/ruby_llm/providers/openai.rb', line 37

def retry_delay(response)
  headers = response.response_headers
  return unless headers

  RATE_LIMIT_RESET_HEADERS.filter_map { |header| parse_reset_duration(headers[header]) }.max
end