Class: Legion::Extensions::Llm::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/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, usage: {}) ⇒ Image

rubocop:disable Metrics/ParameterLists



10
11
12
13
14
15
16
17
# File 'lib/legion/extensions/llm/image.rb', line 10

def initialize(url: nil, data: nil, mime_type: nil, revised_prompt: nil, model_id: nil, usage: {}) # rubocop:disable Metrics/ParameterLists
  @url = url
  @data = data
  @mime_type = mime_type
  @revised_prompt = revised_prompt
  @model_id = model_id
  @usage = usage
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/legion/extensions/llm/image.rb', line 8

def data
  @data
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



8
9
10
# File 'lib/legion/extensions/llm/image.rb', line 8

def mime_type
  @mime_type
end

#model_idObject (readonly)

Returns the value of attribute model_id.



8
9
10
# File 'lib/legion/extensions/llm/image.rb', line 8

def model_id
  @model_id
end

#revised_promptObject (readonly)

Returns the value of attribute revised_prompt.



8
9
10
# File 'lib/legion/extensions/llm/image.rb', line 8

def revised_prompt
  @revised_prompt
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/legion/extensions/llm/image.rb', line 8

def url
  @url
end

#usageObject (readonly)

Returns the value of attribute usage.



8
9
10
# File 'lib/legion/extensions/llm/image.rb', line 8

def usage
  @usage
end

Class Method Details

.paint(prompt, model: nil, provider: nil, assume_model_exists: false, size: '1024x1024', context: nil, with: nil, mask: nil, params: {}) ⇒ Object

rubocop:disable Metrics/ParameterLists



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/legion/extensions/llm/image.rb', line 37

def self.paint(prompt, # rubocop:disable Metrics/ParameterLists
               model: nil,
               provider: nil,
               assume_model_exists: false,
               size: '1024x1024',
               context: nil,
               with: nil,
               mask: nil,
               params: {})
  config = context&.config || Legion::Extensions::Llm.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:, with:, mask:, params:)
end

Instance Method Details

#base64?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/legion/extensions/llm/image.rb', line 19

def base64?
  !@data.nil?
end

#input_costObject



59
60
61
62
63
# File 'lib/legion/extensions/llm/image.rb', line 59

def input_cost
  return flat_input_cost unless detailed_input_usage?

  text_input_cost + image_input_cost
end

#model_infoObject



69
70
71
# File 'lib/legion/extensions/llm/image.rb', line 69

def model_info
  @model_info ||= Legion::Extensions::Llm.models.find(model_id)
end

#output_costObject



65
66
67
# File 'lib/legion/extensions/llm/image.rb', line 65

def output_cost
  tokens_for('output_tokens') * output_token_price.to_f / 1_000_000
end

#save(path) ⇒ Object



32
33
34
35
# File 'lib/legion/extensions/llm/image.rb', line 32

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

#to_blobObject



23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/llm/image.rb', line 23

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

#total_costObject



55
56
57
# File 'lib/legion/extensions/llm/image.rb', line 55

def total_cost
  input_cost + output_cost
end