Class: GenAI::Image::OpenAI

Inherits:
Base show all
Defined in:
lib/gen_ai/image/open_ai.rb

Constant Summary collapse

DEFAULT_SIZE =
'256x256'
DEFAULT_MODEL =
'dall-e-3'
RESPONSE_FORMAT =
'b64_json'

Instance Method Summary collapse

Methods inherited from Base

#upscale

Methods included from Dependency

#depends_on

Constructor Details

#initialize(token:, options: {}) ⇒ OpenAI

Returns a new instance of OpenAI.



12
13
14
15
16
# File 'lib/gen_ai/image/open_ai.rb', line 12

def initialize(token:, options: {})
  depends_on 'ruby-openai'

  @client = ::OpenAI::Client.new(access_token: token)
end

Instance Method Details

#edit(image, prompt, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gen_ai/image/open_ai.rb', line 42

def edit(image, prompt, options = {})
  parameters = build_edit_options(image, prompt, options)

  response = handle_errors { @client.images.edit(parameters: parameters) }

  build_result(
    raw: response,
    model: parameters[:model],
    parsed: parse_response_data(response['data'])
  )
end

#generate(prompt, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gen_ai/image/open_ai.rb', line 18

def generate(prompt, options = {})
  parameters = build_generation_options(prompt, options)

  response = handle_errors { @client.images.generate(parameters: parameters) }

  build_result(
    raw: response,
    model: parameters[:model],
    parsed: parse_response_data(response['data'])
  )
end

#variations(image, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gen_ai/image/open_ai.rb', line 30

def variations(image, options = {})
  parameters = build_variations_options(image, options)

  response = handle_errors { @client.images.variations(parameters: parameters) }

  build_result(
    raw: response,
    model: parameters[:model],
    parsed: parse_response_data(response['data'])
  )
end