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 = {}) ⇒ 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 = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/openai_ruby/client.rb', line 23 def create_chat_completion(params = {}) parser = EventStreamParser::Parser.new params.deep_stringify_keys! if params["stream"] connection.post("/v1/chat/completions") do |req| req.body = params.to_json req..on_data = proc do |chunk, _overall_received_bytes, env| if env && env.status != 200 raise_error = Faraday::Response::RaiseError.new raise_error.on_complete(env.merge(body: try_parse_json(chunk))) end parser.feed(chunk) do |_type, data| yield(JSON.parse(data)) if block_given? && data != "[DONE]" end end end else connection.post("/v1/chat/completions", params.to_json) end 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
46 47 48 49 50 51 52 |
# File 'lib/openai_ruby/client.rb', line 46 def create_edit(params = {}) Faraday.post( "#{base_uri}/v1/edits", params.to_json, headers ) end |
#create_realtime_call(sdp_offer:, session: nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/openai_ruby/client.rb', line 63 def create_realtime_call(sdp_offer:, session: nil) uri = "#{base_uri}/v1/realtime/calls" if session boundary, body = build_multipart_body(sdp_offer, session) Faraday.post(uri) do |req| req.headers["Authorization"] = "Bearer #{api_key}" req.headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}" req.body = body end else Faraday.post(uri) 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
54 55 56 57 |
# File 'lib/openai_ruby/client.rb', line 54 def create_speech(params = {}) params.deep_stringify_keys! connection.post("/v1/audio/speech", params.to_json) end |