Class: OpenAI::Client

Inherits:
Internal::Transport::BaseClient show all
Defined in:
lib/openai/client.rb

Constant Summary collapse

DEFAULT_MAX_RETRIES =

Default max number of retries to attempt after a failed retryable request.

2
DEFAULT_TIMEOUT_IN_SECONDS =

Default per-request timeout.

600.0
DEFAULT_INITIAL_RETRY_DELAY =

Default initial retry delay in seconds. Overall delay is calculated using exponential backoff + jitter.

0.5
DEFAULT_MAX_RETRY_DELAY =

Default max retry delay in seconds.

8.0

Constants inherited from Internal::Transport::BaseClient

Internal::Transport::BaseClient::MAX_REDIRECTS, Internal::Transport::BaseClient::PLATFORM_HEADERS

Instance Attribute Summary collapse

Attributes inherited from Internal::Transport::BaseClient

#base_url, #headers, #idempotency_header, #initial_retry_delay, #max_retries, #max_retry_delay, #requester, #timeout

Instance Method Summary collapse

Methods inherited from Internal::Transport::BaseClient

follow_redirect, #inspect, reap_connection!, #request, should_retry?, validate!

Methods included from Internal::Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, #to_sorbet_type, to_sorbet_type

Constructor Details

#initialize(api_key: ENV["OPENAI_API_KEY"], organization: ENV["OPENAI_ORG_ID"], project: ENV["OPENAI_PROJECT_ID"], base_url: ENV["OPENAI_BASE_URL"], max_retries: self.class::DEFAULT_MAX_RETRIES, timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS, initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY, max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY) ⇒ Client

Creates and returns a new client for interacting with the API.

‘“api.example.com/v2/”`. Defaults to `ENV`

Parameters:

  • api_key (String, nil) (defaults to: ENV["OPENAI_API_KEY"])

    Defaults to ‘ENV`

  • organization (String, nil) (defaults to: ENV["OPENAI_ORG_ID"])

    Defaults to ‘ENV`

  • project (String, nil) (defaults to: ENV["OPENAI_PROJECT_ID"])

    Defaults to ‘ENV`

  • base_url (String, nil) (defaults to: ENV["OPENAI_BASE_URL"])

    Override the default base URL for the API, e.g.,

  • max_retries (Integer) (defaults to: self.class::DEFAULT_MAX_RETRIES)

    Max number of retries to attempt after a failed retryable request.

  • timeout (Float) (defaults to: self.class::DEFAULT_TIMEOUT_IN_SECONDS)
  • initial_retry_delay (Float) (defaults to: self.class::DEFAULT_INITIAL_RETRY_DELAY)
  • max_retry_delay (Float) (defaults to: self.class::DEFAULT_MAX_RETRY_DELAY)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/openai/client.rb', line 105

def initialize(
  api_key: ENV["OPENAI_API_KEY"],
  organization: ENV["OPENAI_ORG_ID"],
  project: ENV["OPENAI_PROJECT_ID"],
  base_url: ENV["OPENAI_BASE_URL"],
  max_retries: self.class::DEFAULT_MAX_RETRIES,
  timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
  initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
  max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
)
  base_url ||= "https://api.openai.com/v1"

  if api_key.nil?
    raise ArgumentError.new("api_key is required, and can be set via environ: \"OPENAI_API_KEY\"")
  end

  headers = {
    "openai-organization" => (@organization = organization&.to_s),
    "openai-project" => (@project = project&.to_s)
  }

  @api_key = api_key.to_s

  super(
    base_url: base_url,
    timeout: timeout,
    max_retries: max_retries,
    initial_retry_delay: initial_retry_delay,
    max_retry_delay: max_retry_delay,
    headers: headers
  )

  @completions = OpenAI::Resources::Completions.new(client: self)
  @chat = OpenAI::Resources::Chat.new(client: self)
  @embeddings = OpenAI::Resources::Embeddings.new(client: self)
  @files = OpenAI::Resources::Files.new(client: self)
  @images = OpenAI::Resources::Images.new(client: self)
  @audio = OpenAI::Resources::Audio.new(client: self)
  @moderations = OpenAI::Resources::Moderations.new(client: self)
  @models = OpenAI::Resources::Models.new(client: self)
  @fine_tuning = OpenAI::Resources::FineTuning.new(client: self)
  @graders = OpenAI::Resources::Graders.new(client: self)
  @vector_stores = OpenAI::Resources::VectorStores.new(client: self)
  @beta = OpenAI::Resources::Beta.new(client: self)
  @batches = OpenAI::Resources::Batches.new(client: self)
  @uploads = OpenAI::Resources::Uploads.new(client: self)
  @responses = OpenAI::Resources::Responses.new(client: self)
  @evals = OpenAI::Resources::Evals.new(client: self)
  @containers = OpenAI::Resources::Containers.new(client: self)
end

Instance Attribute Details

#api_keyString (readonly)

Returns:

  • (String)


19
20
21
# File 'lib/openai/client.rb', line 19

def api_key
  @api_key
end

#audioOpenAI::Resources::Audio (readonly)



43
44
45
# File 'lib/openai/client.rb', line 43

def audio
  @audio
end

#batchesOpenAI::Resources::Batches (readonly)



64
65
66
# File 'lib/openai/client.rb', line 64

def batches
  @batches
end

#betaOpenAI::Resources::Beta (readonly)



61
62
63
# File 'lib/openai/client.rb', line 61

def beta
  @beta
end

#chatOpenAI::Resources::Chat (readonly)



31
32
33
# File 'lib/openai/client.rb', line 31

def chat
  @chat
end

#completionsOpenAI::Resources::Completions (readonly)



28
29
30
# File 'lib/openai/client.rb', line 28

def completions
  @completions
end

#containersOpenAI::Resources::Containers (readonly)



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

def containers
  @containers
end

#embeddingsOpenAI::Resources::Embeddings (readonly)



34
35
36
# File 'lib/openai/client.rb', line 34

def embeddings
  @embeddings
end

#evalsOpenAI::Resources::Evals (readonly)



73
74
75
# File 'lib/openai/client.rb', line 73

def evals
  @evals
end

#filesOpenAI::Resources::Files (readonly)



37
38
39
# File 'lib/openai/client.rb', line 37

def files
  @files
end

#fine_tuningOpenAI::Resources::FineTuning (readonly)



52
53
54
# File 'lib/openai/client.rb', line 52

def fine_tuning
  @fine_tuning
end

#gradersOpenAI::Resources::Graders (readonly)



55
56
57
# File 'lib/openai/client.rb', line 55

def graders
  @graders
end

#imagesOpenAI::Resources::Images (readonly)



40
41
42
# File 'lib/openai/client.rb', line 40

def images
  @images
end

#modelsOpenAI::Resources::Models (readonly)



49
50
51
# File 'lib/openai/client.rb', line 49

def models
  @models
end

#moderationsOpenAI::Resources::Moderations (readonly)



46
47
48
# File 'lib/openai/client.rb', line 46

def moderations
  @moderations
end

#organizationString? (readonly)

Returns:

  • (String, nil)


22
23
24
# File 'lib/openai/client.rb', line 22

def organization
  @organization
end

#projectString? (readonly)

Returns:

  • (String, nil)


25
26
27
# File 'lib/openai/client.rb', line 25

def project
  @project
end

#responsesOpenAI::Resources::Responses (readonly)



70
71
72
# File 'lib/openai/client.rb', line 70

def responses
  @responses
end

#uploadsOpenAI::Resources::Uploads (readonly)



67
68
69
# File 'lib/openai/client.rb', line 67

def uploads
  @uploads
end

#vector_storesOpenAI::Resources::VectorStores (readonly)



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

def vector_stores
  @vector_stores
end