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
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
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"], 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, #send_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"], 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`
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/openai/client.rb', line 139 def initialize( api_key: ENV["OPENAI_API_KEY"], 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 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_key ⇒ String (readonly)
19 20 21 |
# File 'lib/openai/client.rb', line 19 def api_key @api_key end |
#audio ⇒ OpenAI::Resources::Audio (readonly)
53 54 55 |
# File 'lib/openai/client.rb', line 53 def audio @audio end |
#batches ⇒ OpenAI::Resources::Batches (readonly)
Create large batches of API requests to run asynchronously.
81 82 83 |
# File 'lib/openai/client.rb', line 81 def batches @batches end |
#beta ⇒ OpenAI::Resources::Beta (readonly)
77 78 79 |
# File 'lib/openai/client.rb', line 77 def beta @beta end |
#chat ⇒ OpenAI::Resources::Chat (readonly)
36 37 38 |
# File 'lib/openai/client.rb', line 36 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.
33 34 35 |
# File 'lib/openai/client.rb', line 33 def completions @completions end |
#containers ⇒ OpenAI::Resources::Containers (readonly)
102 103 104 |
# File 'lib/openai/client.rb', line 102 def containers @containers end |
#conversations ⇒ OpenAI::Resources::Conversations (readonly)
Manage conversations and conversation items.
95 96 97 |
# File 'lib/openai/client.rb', line 95 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.
41 42 43 |
# File 'lib/openai/client.rb', line 41 def @embeddings end |
#evals ⇒ OpenAI::Resources::Evals (readonly)
Manage and run evals in the OpenAI platform.
99 100 101 |
# File 'lib/openai/client.rb', line 99 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.
46 47 48 |
# File 'lib/openai/client.rb', line 46 def files @files end |
#fine_tuning ⇒ OpenAI::Resources::FineTuning (readonly)
65 66 67 |
# File 'lib/openai/client.rb', line 65 def fine_tuning @fine_tuning end |
#graders ⇒ OpenAI::Resources::Graders (readonly)
68 69 70 |
# File 'lib/openai/client.rb', line 68 def graders @graders end |
#images ⇒ OpenAI::Resources::Images (readonly)
Given a prompt and/or an input image, the model will generate a new image.
50 51 52 |
# File 'lib/openai/client.rb', line 50 def images @images end |
#models ⇒ OpenAI::Resources::Models (readonly)
List and describe the various models available in the API.
62 63 64 |
# File 'lib/openai/client.rb', line 62 def models @models end |
#moderations ⇒ OpenAI::Resources::Moderations (readonly)
Given text and/or image inputs, classifies if those inputs are potentially harmful.
58 59 60 |
# File 'lib/openai/client.rb', line 58 def moderations @moderations end |
#organization ⇒ String? (readonly)
22 23 24 |
# File 'lib/openai/client.rb', line 22 def organization @organization end |
#project ⇒ String? (readonly)
25 26 27 |
# File 'lib/openai/client.rb', line 25 def project @project end |
#realtime ⇒ OpenAI::Resources::Realtime (readonly)
91 92 93 |
# File 'lib/openai/client.rb', line 91 def realtime @realtime end |
#responses ⇒ OpenAI::Resources::Responses (readonly)
88 89 90 |
# File 'lib/openai/client.rb', line 88 def responses @responses end |
#skills ⇒ OpenAI::Resources::Skills (readonly)
105 106 107 |
# File 'lib/openai/client.rb', line 105 def skills @skills end |
#uploads ⇒ OpenAI::Resources::Uploads (readonly)
Use Uploads to upload large files in multiple parts.
85 86 87 |
# File 'lib/openai/client.rb', line 85 def uploads @uploads end |
#vector_stores ⇒ OpenAI::Resources::VectorStores (readonly)
71 72 73 |
# File 'lib/openai/client.rb', line 71 def vector_stores @vector_stores end |
#videos ⇒ OpenAI::Resources::Videos (readonly)
108 109 110 |
# File 'lib/openai/client.rb', line 108 def videos @videos end |
#webhook_secret ⇒ String? (readonly)
28 29 30 |
# File 'lib/openai/client.rb', line 28 def webhook_secret @webhook_secret end |
#webhooks ⇒ OpenAI::Resources::Webhooks (readonly)
74 75 76 |
# File 'lib/openai/client.rb', line 74 def webhooks @webhooks end |