Class: OpenAI::Client
- Inherits:
-
Object
- Object
- OpenAI::Client
- Defined in:
- lib/openai_ruby/client.rb
Constant Summary collapse
- DEFAULT_BASE_URI =
"https://api.openai.com"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #create_chat_completion(params = {}, &block) ⇒ Object
- #create_completion(params = {}) ⇒ Object
- #create_edit(params = {}) ⇒ Object
- #create_realtime_call(sdp_offer:, session: nil) ⇒ Object
- #create_speech(params = {}) ⇒ Object
- #images ⇒ Object
-
#initialize(api_key, options = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(api_key, options = {}) ⇒ Client
Returns a new instance of Client.
9 10 11 12 13 |
# File 'lib/openai_ruby/client.rb', line 9 def initialize(api_key, = {}) @api_key = api_key @options = @base_uri = [:base_uri] || DEFAULT_BASE_URI end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/openai_ruby/client.rb', line 7 def api_key @api_key end |
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
7 8 9 |
# File 'lib/openai_ruby/client.rb', line 7 def base_uri @base_uri end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/openai_ruby/client.rb', line 7 def @options end |
Instance Method Details
#create_chat_completion(params = {}, &block) ⇒ Object
23 24 25 26 27 |
# File 'lib/openai_ruby/client.rb', line 23 def create_chat_completion(params = {}, &block) return connection.post("/v1/chat/completions", params.to_json) unless streaming?(params) create_streaming_chat_completion(params, &block) end |
#create_completion(params = {}) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/openai_ruby/client.rb', line 15 def create_completion(params = {}) Faraday.post( "#{base_uri}/v1/completions", params.to_json, headers ) end |
#create_edit(params = {}) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/openai_ruby/client.rb', line 29 def create_edit(params = {}) Faraday.post( "#{base_uri}/v1/edits", params.to_json, headers ) end |
#create_realtime_call(sdp_offer:, session: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/openai_ruby/client.rb', line 45 def create_realtime_call(sdp_offer:, session: nil) if session boundary, body = build_multipart_body(sdp_offer, session) connection.post("/v1/realtime/calls") do |req| req.headers["Authorization"] = "Bearer #{api_key}" req.headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}" req.body = body end else connection.post("/v1/realtime/calls") do |req| req.headers["Content-Type"] = "application/sdp" req.headers["Authorization"] = "Bearer #{api_key}" req.body = sdp_offer end end end |
#create_speech(params = {}) ⇒ Object
37 38 39 |
# File 'lib/openai_ruby/client.rb', line 37 def create_speech(params = {}) connection.post("/v1/audio/speech", params.to_json) end |