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
WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER =
"workload-identity-auth"

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"], workload_identity: nil, organization: ENV["OPENAI_ORG_ID"], project: ENV["OPENAI_PROJECT_ID"], webhook_secret: ENV["OPENAI_WEBHOOK_SECRET"], 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`. Mutually exclusive with workload_identity.

  • workload_identity (OpenAI::Auth::WorkloadIdentity, nil) (defaults to: nil)

    OAuth2 workload identity configuration for token exchange authentication. Mutually exclusive with api_key.

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

    Defaults to ‘ENV`.

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

    Defaults to ‘ENV`.

  • webhook_secret (String, nil) (defaults to: ENV["OPENAI_WEBHOOK_SECRET"])

    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)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/openai/client.rb', line 180

def initialize(
  api_key: ENV["OPENAI_API_KEY"],
  workload_identity: nil,
  organization: ENV["OPENAI_ORG_ID"],
  project: ENV["OPENAI_PROJECT_ID"],
  webhook_secret: ENV["OPENAI_WEBHOOK_SECRET"],
  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 workload_identity && api_key && api_key != WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER
    raise ArgumentError.new(
      "The `api_key` and `workload_identity` arguments are mutually exclusive; " \
      "only one can be passed at a time."
    )
  end

  if workload_identity
    @workload_identity_auth = OpenAI::Auth::WorkloadIdentityAuth.new(
      workload_identity,
      organization
    )
    api_key = WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER
  elsif 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
  @webhook_secret = webhook_secret&.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)
  @webhooks = OpenAI::Resources::Webhooks.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)
  @realtime = OpenAI::Resources::Realtime.new(client: self)
  @conversations = OpenAI::Resources::Conversations.new(client: self)
  @evals = OpenAI::Resources::Evals.new(client: self)
  @containers = OpenAI::Resources::Containers.new(client: self)
  @skills = OpenAI::Resources::Skills.new(client: self)
  @videos = OpenAI::Resources::Videos.new(client: self)
end

Instance Attribute Details

#api_keyString (readonly)

Returns:

  • (String)


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

def api_key
  @api_key
end

#audioOpenAI::Resources::Audio (readonly)



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

def audio
  @audio
end

#batchesOpenAI::Resources::Batches (readonly)

Create large batches of API requests to run asynchronously.



87
88
89
# File 'lib/openai/client.rb', line 87

def batches
  @batches
end

#betaOpenAI::Resources::Beta (readonly)



83
84
85
# File 'lib/openai/client.rb', line 83

def beta
  @beta
end

#chatOpenAI::Resources::Chat (readonly)



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

def chat
  @chat
end

#completionsOpenAI::Resources::Completions (readonly)

Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.



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

def completions
  @completions
end

#containersOpenAI::Resources::Containers (readonly)



108
109
110
# File 'lib/openai/client.rb', line 108

def containers
  @containers
end

#conversationsOpenAI::Resources::Conversations (readonly)

Manage conversations and conversation items.



101
102
103
# File 'lib/openai/client.rb', line 101

def conversations
  @conversations
end

#embeddingsOpenAI::Resources::Embeddings (readonly)

Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.



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

def embeddings
  @embeddings
end

#evalsOpenAI::Resources::Evals (readonly)

Manage and run evals in the OpenAI platform.



105
106
107
# File 'lib/openai/client.rb', line 105

def evals
  @evals
end

#filesOpenAI::Resources::Files (readonly)

Files are used to upload documents that can be used with features like Assistants and Fine-tuning.



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

def files
  @files
end

#fine_tuningOpenAI::Resources::FineTuning (readonly)



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

def fine_tuning
  @fine_tuning
end

#gradersOpenAI::Resources::Graders (readonly)



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

def graders
  @graders
end

#imagesOpenAI::Resources::Images (readonly)

Given a prompt and/or an input image, the model will generate a new image.



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

def images
  @images
end

#modelsOpenAI::Resources::Models (readonly)

List and describe the various models available in the API.



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

def models
  @models
end

#moderationsOpenAI::Resources::Moderations (readonly)

Given text and/or image inputs, classifies if those inputs are potentially harmful.



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

def moderations
  @moderations
end

#organizationString? (readonly)

Returns:

  • (String, nil)


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

def organization
  @organization
end

#projectString? (readonly)

Returns:

  • (String, nil)


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

def project
  @project
end

#realtimeOpenAI::Resources::Realtime (readonly)



97
98
99
# File 'lib/openai/client.rb', line 97

def realtime
  @realtime
end

#responsesOpenAI::Resources::Responses (readonly)



94
95
96
# File 'lib/openai/client.rb', line 94

def responses
  @responses
end

#skillsOpenAI::Resources::Skills (readonly)



111
112
113
# File 'lib/openai/client.rb', line 111

def skills
  @skills
end

#uploadsOpenAI::Resources::Uploads (readonly)

Use Uploads to upload large files in multiple parts.



91
92
93
# File 'lib/openai/client.rb', line 91

def uploads
  @uploads
end

#vector_storesOpenAI::Resources::VectorStores (readonly)



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

def vector_stores
  @vector_stores
end

#videosOpenAI::Resources::Videos (readonly)



114
115
116
# File 'lib/openai/client.rb', line 114

def videos
  @videos
end

#webhook_secretString? (readonly)

Returns:

  • (String, nil)


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

def webhook_secret
  @webhook_secret
end

#webhooksOpenAI::Resources::Webhooks (readonly)



80
81
82
# File 'lib/openai/client.rb', line 80

def webhooks
  @webhooks
end

#workload_identity_authOpenAI::Auth::WorkloadIdentityAuth? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def workload_identity_auth
  @workload_identity_auth
end