Class: OpenAI::Client
- Inherits:
-
Internal::Transport::BaseClient
- Object
- Internal::Transport::BaseClient
- OpenAI::Client
- 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
- #api_key ⇒ String readonly
- #audio ⇒ OpenAI::Resources::Audio readonly
-
#batches ⇒ OpenAI::Resources::Batches
readonly
Create large batches of API requests to run asynchronously.
- #beta ⇒ OpenAI::Resources::Beta readonly
- #chat ⇒ OpenAI::Resources::Chat readonly
-
#completions ⇒ OpenAI::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.
- #containers ⇒ OpenAI::Resources::Containers readonly
-
#conversations ⇒ OpenAI::Resources::Conversations
readonly
Manage conversations and conversation items.
-
#embeddings ⇒ OpenAI::Resources::Embeddings
readonly
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
-
#evals ⇒ OpenAI::Resources::Evals
readonly
Manage and run evals in the OpenAI platform.
-
#files ⇒ OpenAI::Resources::Files
readonly
Files are used to upload documents that can be used with features like Assistants and Fine-tuning.
- #fine_tuning ⇒ OpenAI::Resources::FineTuning readonly
- #graders ⇒ OpenAI::Resources::Graders readonly
-
#images ⇒ OpenAI::Resources::Images
readonly
Given a prompt and/or an input image, the model will generate a new image.
-
#models ⇒ OpenAI::Resources::Models
readonly
List and describe the various models available in the API.
-
#moderations ⇒ OpenAI::Resources::Moderations
readonly
Given text and/or image inputs, classifies if those inputs are potentially harmful.
- #organization ⇒ String? readonly
- #project ⇒ String? readonly
- #realtime ⇒ OpenAI::Resources::Realtime readonly
- #responses ⇒ OpenAI::Resources::Responses readonly
- #skills ⇒ OpenAI::Resources::Skills readonly
-
#uploads ⇒ OpenAI::Resources::Uploads
readonly
Use Uploads to upload large files in multiple parts.
- #vector_stores ⇒ OpenAI::Resources::VectorStores readonly
- #videos ⇒ OpenAI::Resources::Videos readonly
- #webhook_secret ⇒ String? readonly
- #webhooks ⇒ OpenAI::Resources::Webhooks readonly
- #workload_identity_auth ⇒ OpenAI::Auth::WorkloadIdentityAuth? readonly private
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
-
#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
constructor
Creates and returns a new client for interacting with the API.
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`
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) = 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_key ⇒ String (readonly)
21 22 23 |
# File 'lib/openai/client.rb', line 21 def api_key @api_key end |
#audio ⇒ OpenAI::Resources::Audio (readonly)
59 60 61 |
# File 'lib/openai/client.rb', line 59 def audio @audio end |
#batches ⇒ OpenAI::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 |
#beta ⇒ OpenAI::Resources::Beta (readonly)
83 84 85 |
# File 'lib/openai/client.rb', line 83 def beta @beta end |
#chat ⇒ OpenAI::Resources::Chat (readonly)
42 43 44 |
# File 'lib/openai/client.rb', line 42 def chat @chat end |
#completions ⇒ OpenAI::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 |
#containers ⇒ OpenAI::Resources::Containers (readonly)
108 109 110 |
# File 'lib/openai/client.rb', line 108 def containers @containers end |
#conversations ⇒ OpenAI::Resources::Conversations (readonly)
Manage conversations and conversation items.
101 102 103 |
# File 'lib/openai/client.rb', line 101 def conversations @conversations end |
#embeddings ⇒ OpenAI::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 end |
#evals ⇒ OpenAI::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 |
#files ⇒ OpenAI::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_tuning ⇒ OpenAI::Resources::FineTuning (readonly)
71 72 73 |
# File 'lib/openai/client.rb', line 71 def fine_tuning @fine_tuning end |
#graders ⇒ OpenAI::Resources::Graders (readonly)
74 75 76 |
# File 'lib/openai/client.rb', line 74 def graders @graders end |
#images ⇒ OpenAI::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 |
#models ⇒ OpenAI::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 |
#moderations ⇒ OpenAI::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 |
#organization ⇒ String? (readonly)
24 25 26 |
# File 'lib/openai/client.rb', line 24 def organization @organization end |
#project ⇒ String? (readonly)
27 28 29 |
# File 'lib/openai/client.rb', line 27 def project @project end |
#realtime ⇒ OpenAI::Resources::Realtime (readonly)
97 98 99 |
# File 'lib/openai/client.rb', line 97 def realtime @realtime end |
#responses ⇒ OpenAI::Resources::Responses (readonly)
94 95 96 |
# File 'lib/openai/client.rb', line 94 def responses @responses end |
#skills ⇒ OpenAI::Resources::Skills (readonly)
111 112 113 |
# File 'lib/openai/client.rb', line 111 def skills @skills end |
#uploads ⇒ OpenAI::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_stores ⇒ OpenAI::Resources::VectorStores (readonly)
77 78 79 |
# File 'lib/openai/client.rb', line 77 def vector_stores @vector_stores end |
#videos ⇒ OpenAI::Resources::Videos (readonly)
114 115 116 |
# File 'lib/openai/client.rb', line 114 def videos @videos end |
#webhook_secret ⇒ String? (readonly)
30 31 32 |
# File 'lib/openai/client.rb', line 30 def webhook_secret @webhook_secret end |
#webhooks ⇒ OpenAI::Resources::Webhooks (readonly)
80 81 82 |
# File 'lib/openai/client.rb', line 80 def webhooks @webhooks end |
#workload_identity_auth ⇒ OpenAI::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 |