Class: OpenAI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/openai_ruby/client.rb

Constant Summary collapse

BASE_URL =
"https://api.openai.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/openai_ruby/client.rb', line 9

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (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

Instance Method Details

#create_chat_completion(params = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/openai_ruby/client.rb', line 21

def create_chat_completion(params = {})
  if params[:stream]
    connection.post("/v1/chat/completions") do |req|
      req.body = params.to_json
      req.options.on_data = proc do |chunk, overall_received_bytes, env|
        if block_given?
          yield(chunk, overall_received_bytes, env)
        end
      end
    end
  else
    connection.post("/v1/chat/completions", params.to_json, headers)
  end
end

#create_completion(params = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/openai_ruby/client.rb', line 13

def create_completion(params = {})
  Faraday.post(
    "#{BASE_URL}/v1/completions",
    params.to_json,
    headers
  )
end

#create_edit(params = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/openai_ruby/client.rb', line 36

def create_edit(params = {})
  Faraday.post(
    "#{BASE_URL}/v1/edits",
    params.to_json,
    headers
  )
end