Class: RubyLLM::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/image.rb

Overview

Represents a generated image from an AI model.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, data: nil, mime_type: nil, revised_prompt: nil, model_id: nil) ⇒ Image

Returns a new instance of Image.



8
9
10
11
12
13
14
# File 'lib/ruby_llm/image.rb', line 8

def initialize(url: nil, data: nil, mime_type: nil, revised_prompt: nil, model_id: nil)
  @url = url
  @data = data
  @mime_type = mime_type
  @revised_prompt = revised_prompt
  @model_id = model_id
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/ruby_llm/image.rb', line 6

def data
  @data
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



6
7
8
# File 'lib/ruby_llm/image.rb', line 6

def mime_type
  @mime_type
end

#model_idObject (readonly)

Returns the value of attribute model_id.



6
7
8
# File 'lib/ruby_llm/image.rb', line 6

def model_id
  @model_id
end

#revised_promptObject (readonly)

Returns the value of attribute revised_prompt.



6
7
8
# File 'lib/ruby_llm/image.rb', line 6

def revised_prompt
  @revised_prompt
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/ruby_llm/image.rb', line 6

def url
  @url
end

Class Method Details

.paint(prompt, model: nil, provider: nil, assume_model_exists: false, size: '1024x1024', context: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_llm/image.rb', line 34

def self.paint(prompt, # rubocop:disable Metrics/ParameterLists
               model: nil,
               provider: nil,
               assume_model_exists: false,
               size: '1024x1024',
               context: nil)
  config = context&.config || RubyLLM.config
  model ||= config.default_image_model
  model, provider_instance = Models.resolve(model, provider: provider, assume_exists: assume_model_exists,
                                                   config: config)
  model_id = model.id

  provider_instance.paint(prompt, model: model_id, size:)
end

Instance Method Details

#base64?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ruby_llm/image.rb', line 16

def base64?
  !@data.nil?
end

#save(path) ⇒ Object



29
30
31
32
# File 'lib/ruby_llm/image.rb', line 29

def save(path)
  File.binwrite(File.expand_path(path), to_blob)
  path
end

#to_blobObject



20
21
22
23
24
25
26
27
# File 'lib/ruby_llm/image.rb', line 20

def to_blob
  if base64?
    Base64.decode64 @data
  else
    response = Connection.basic.get @url
    response.body
  end
end