Class: LexLLM::Image
- Inherits:
-
Object
- Object
- LexLLM::Image
- Defined in:
- lib/lex_llm/image.rb
Overview
Represents a generated image from an AI model.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#mime_type ⇒ Object
readonly
Returns the value of attribute mime_type.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#revised_prompt ⇒ Object
readonly
Returns the value of attribute revised_prompt.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#usage ⇒ Object
readonly
Returns the value of attribute usage.
Class Method Summary collapse
-
.paint(prompt, model: nil, provider: nil, assume_model_exists: false, size: '1024x1024', context: nil, with: nil, mask: nil, params: {}) ⇒ Object
rubocop:disable Metrics/ParameterLists.
Instance Method Summary collapse
- #base64? ⇒ Boolean
-
#initialize(url: nil, data: nil, mime_type: nil, revised_prompt: nil, model_id: nil, usage: {}) ⇒ Image
constructor
rubocop:disable Metrics/ParameterLists.
- #input_cost ⇒ Object
- #model_info ⇒ Object
- #output_cost ⇒ Object
- #save(path) ⇒ Object
- #to_blob ⇒ Object
- #total_cost ⇒ Object
Constructor Details
#initialize(url: nil, data: nil, mime_type: nil, revised_prompt: nil, model_id: nil, usage: {}) ⇒ Image
rubocop:disable Metrics/ParameterLists
8 9 10 11 12 13 14 15 |
# File 'lib/lex_llm/image.rb', line 8 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
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/lex_llm/image.rb', line 6 def data @data end |
#mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type.
6 7 8 |
# File 'lib/lex_llm/image.rb', line 6 def mime_type @mime_type end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
6 7 8 |
# File 'lib/lex_llm/image.rb', line 6 def model_id @model_id end |
#revised_prompt ⇒ Object (readonly)
Returns the value of attribute revised_prompt.
6 7 8 |
# File 'lib/lex_llm/image.rb', line 6 def revised_prompt @revised_prompt end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
6 7 8 |
# File 'lib/lex_llm/image.rb', line 6 def url @url end |
#usage ⇒ Object (readonly)
Returns the value of attribute usage.
6 7 8 |
# File 'lib/lex_llm/image.rb', line 6 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lex_llm/image.rb', line 35 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 || LexLLM.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
17 18 19 |
# File 'lib/lex_llm/image.rb', line 17 def base64? !@data.nil? end |
#input_cost ⇒ Object
57 58 59 60 61 |
# File 'lib/lex_llm/image.rb', line 57 def input_cost return flat_input_cost unless detailed_input_usage? text_input_cost + image_input_cost end |
#model_info ⇒ Object
67 68 69 |
# File 'lib/lex_llm/image.rb', line 67 def model_info @model_info ||= LexLLM.models.find(model_id) end |
#output_cost ⇒ Object
63 64 65 |
# File 'lib/lex_llm/image.rb', line 63 def output_cost tokens_for('output_tokens') * output_token_price.to_f / 1_000_000 end |
#save(path) ⇒ Object
30 31 32 33 |
# File 'lib/lex_llm/image.rb', line 30 def save(path) File.binwrite(File.(path), to_blob) path end |
#to_blob ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/lex_llm/image.rb', line 21 def to_blob if base64? Base64.decode64 @data else response = Connection.basic.get @url response.body end end |
#total_cost ⇒ Object
53 54 55 |
# File 'lib/lex_llm/image.rb', line 53 def total_cost input_cost + output_cost end |