Class: RubyLLM::Agents::ImageGenerationResult
- Inherits:
-
Object
- Object
- RubyLLM::Agents::ImageGenerationResult
- Includes:
- Trackable
- Defined in:
- lib/ruby_llm/agents/results/image_generation_result.rb
Overview
Result wrapper for image generation operations
Provides a consistent interface for accessing generated images, metadata, timing, and cost information.
Instance Attribute Summary collapse
-
#completed_at ⇒ Object
readonly
Returns the value of attribute completed_at.
-
#error_class ⇒ Object
readonly
Returns the value of attribute error_class.
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#execution_id ⇒ Object
readonly
Returns the value of attribute execution_id.
-
#generator_class ⇒ Object
readonly
Returns the value of attribute generator_class.
-
#images ⇒ Object
readonly
Returns the value of attribute images.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#quality ⇒ Object
readonly
Returns the value of attribute quality.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#style ⇒ Object
readonly
Returns the value of attribute style.
-
#tenant_id ⇒ Object
readonly
Returns the value of attribute tenant_id.
Class Method Summary collapse
-
.from_cache(data) ⇒ CachedImageGenerationResult
Create a result from cached data.
Instance Method Summary collapse
-
#base64? ⇒ Boolean
Check if the image is base64 encoded.
-
#batch? ⇒ Boolean
Check if this was a batch request.
-
#blobs ⇒ Array<String>
Get all images as binary data.
-
#count ⇒ Integer
Get the number of generated images.
-
#data ⇒ String?
Get the base64 data of the first image.
-
#datas ⇒ Array<String>
Get all base64 data.
-
#duration_ms ⇒ Integer
Get the generation duration in milliseconds.
-
#error? ⇒ Boolean
Check if generation failed.
-
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database.
-
#image ⇒ Object?
Get the first/only image.
-
#initialize(images:, prompt:, model_id:, size:, quality:, style:, started_at:, completed_at:, tenant_id:, generator_class:, error_class: nil, error_message: nil, execution_id: nil, agent_class_name: nil) ⇒ ImageGenerationResult
constructor
Initialize a new result.
-
#input_tokens ⇒ Integer
Estimate input tokens from prompt.
-
#mime_type ⇒ String?
Get the MIME type.
-
#revised_prompt ⇒ String?
Get the revised prompt (if model modified it).
-
#revised_prompts ⇒ Array<String>
Get all revised prompts.
-
#save(path) ⇒ Object
Save the first image to a file.
-
#save_all(directory, prefix: "image") ⇒ Object
Save all images to a directory.
-
#single? ⇒ Boolean
Check if this was a single image request.
-
#success? ⇒ Boolean
Check if generation was successful.
-
#to_blob ⇒ String?
Get the first image as binary data.
-
#to_cache ⇒ Hash
Convert to cacheable format.
-
#to_h ⇒ Hash
Convert to hash.
-
#total_cost ⇒ Float
Get the total cost for this generation.
-
#url ⇒ String?
Get the URL of the first image.
-
#urls ⇒ Array<String>
Get all image URLs.
Methods included from Trackable
Constructor Details
#initialize(images:, prompt:, model_id:, size:, quality:, style:, started_at:, completed_at:, tenant_id:, generator_class:, error_class: nil, error_message: nil, execution_id: nil, agent_class_name: nil) ⇒ ImageGenerationResult
Initialize a new result
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 43 def initialize(images:, prompt:, model_id:, size:, quality:, style:, started_at:, completed_at:, tenant_id:, generator_class:, error_class: nil, error_message: nil, execution_id: nil, agent_class_name: nil) @images = images @prompt = prompt @model_id = model_id @size = size @quality = quality @style = style @started_at = started_at @completed_at = completed_at @tenant_id = tenant_id @generator_class = generator_class @error_class = error_class @error_message = @execution_id = execution_id # Tracking @agent_class_name = agent_class_name register_with_tracker end |
Instance Attribute Details
#completed_at ⇒ Object (readonly)
Returns the value of attribute completed_at.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def completed_at @completed_at end |
#error_class ⇒ Object (readonly)
Returns the value of attribute error_class.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def error_class @error_class end |
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def @error_message end |
#execution_id ⇒ Object (readonly)
Returns the value of attribute execution_id.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def execution_id @execution_id end |
#generator_class ⇒ Object (readonly)
Returns the value of attribute generator_class.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def generator_class @generator_class end |
#images ⇒ Object (readonly)
Returns the value of attribute images.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def images @images end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def model_id @model_id end |
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def prompt @prompt end |
#quality ⇒ Object (readonly)
Returns the value of attribute quality.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def quality @quality end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def size @size end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def started_at @started_at end |
#style ⇒ Object (readonly)
Returns the value of attribute style.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def style @style end |
#tenant_id ⇒ Object (readonly)
Returns the value of attribute tenant_id.
25 26 27 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 25 def tenant_id @tenant_id end |
Class Method Details
.from_cache(data) ⇒ CachedImageGenerationResult
Create a result from cached data
300 301 302 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 300 def self.from_cache(data) CachedImageGenerationResult.new(data) end |
Instance Method Details
#base64? ⇒ Boolean
Check if the image is base64 encoded
142 143 144 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 142 def base64? image&.base64? || false end |
#batch? ⇒ Boolean
Check if this was a batch request
98 99 100 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 98 def batch? count > 1 end |
#blobs ⇒ Array<String>
Get all images as binary data
244 245 246 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 244 def blobs images.map(&:to_blob) end |
#count ⇒ Integer
Get the number of generated images
172 173 174 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 172 def count images.size end |
#data ⇒ String?
Get the base64 data of the first image
128 129 130 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 128 def data image&.data end |
#datas ⇒ Array<String>
Get all base64 data
135 136 137 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 135 def datas images.map(&:data).compact end |
#duration_ms ⇒ Integer
Get the generation duration in milliseconds
181 182 183 184 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 181 def duration_ms return 0 unless started_at && completed_at ((completed_at - started_at) * 1000).round end |
#error? ⇒ Boolean
Check if generation failed
84 85 86 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 84 def error? !success? end |
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database
68 69 70 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 68 def execution @execution ||= RubyLLM::Agents::Execution.find_by(id: execution_id) if execution_id end |
#image ⇒ Object?
Get the first/only image
107 108 109 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 107 def image images.first end |
#input_tokens ⇒ Integer
Estimate input tokens from prompt
207 208 209 210 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 207 def input_tokens # Approximate token count for prompt (prompt.length / 4.0).ceil end |
#mime_type ⇒ String?
Get the MIME type
149 150 151 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 149 def mime_type image&.mime_type end |
#revised_prompt ⇒ String?
Get the revised prompt (if model modified it)
156 157 158 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 156 def revised_prompt image&.revised_prompt end |
#revised_prompts ⇒ Array<String>
Get all revised prompts
163 164 165 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 163 def revised_prompts images.map(&:revised_prompt).compact end |
#save(path) ⇒ Object
Save the first image to a file
218 219 220 221 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 218 def save(path) raise "No image to save" unless image image.save(path) end |
#save_all(directory, prefix: "image") ⇒ Object
Save all images to a directory
227 228 229 230 231 232 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 227 def save_all(directory, prefix: "image") images.each_with_index do |img, idx| filename = "#{prefix}_#{idx + 1}.png" img.save(File.join(directory, filename)) end end |
#single? ⇒ Boolean
Check if this was a single image request
91 92 93 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 91 def single? count == 1 end |
#success? ⇒ Boolean
Check if generation was successful
77 78 79 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 77 def success? error_class.nil? && images.any? end |
#to_blob ⇒ String?
Get the first image as binary data
237 238 239 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 237 def to_blob image&.to_blob end |
#to_cache ⇒ Hash
Convert to cacheable format
284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 284 def to_cache { urls: urls, datas: datas, mime_type: mime_type, revised_prompts: revised_prompts, model_id: model_id, total_cost: total_cost, cached_at: Time.current.iso8601 } end |
#to_h ⇒ Hash
Convert to hash
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 253 def to_h { success: success?, count: count, urls: urls, base64: base64?, mime_type: mime_type, prompt: prompt, revised_prompts: revised_prompts, model_id: model_id, size: size, quality: quality, style: style, total_cost: total_cost, input_tokens: input_tokens, duration_ms: duration_ms, started_at: started_at&.iso8601, completed_at: completed_at&.iso8601, tenant_id: tenant_id, generator_class: generator_class, error_class: error_class, error_message: , execution_id: execution_id } end |
#total_cost ⇒ Float
Get the total cost for this generation
Uses dynamic pricing from the Pricing module
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 193 def total_cost return 0 if error? ImageGenerator::Pricing.calculate_cost( model_id: model_id, size: size, quality: quality, count: count ) end |
#url ⇒ String?
Get the URL of the first image
114 115 116 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 114 def url image&.url end |
#urls ⇒ Array<String>
Get all image URLs
121 122 123 |
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 121 def urls images.map(&:url).compact end |