Class: RubyLLM::Agents::ImageGenerationResult

Inherits:
Object
  • Object
show all
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.

Examples:

Accessing a single image

result = ImageGenerator.call(prompt: "A sunset")
result.url       # => "https://..."
result.success?  # => true
result.save("sunset.png")

Accessing multiple images

result = ImageGenerator.call(prompt: "Logos", count: 4)
result.urls      # => ["https://...", ...]
result.count     # => 4
result.save_all("./logos")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trackable

included

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

Parameters:

  • images (Array<Object>)

    Array of image objects from RubyLLM

  • prompt (String)

    The original prompt

  • model_id (String)

    Model used for generation

  • size (String)

    Image size

  • quality (String)

    Quality setting

  • style (String)

    Style setting

  • started_at (Time)

    When generation started

  • completed_at (Time)

    When generation completed

  • tenant_id (String, nil)

    Tenant identifier

  • generator_class (String)

    Name of the generator class

  • error_class (String, nil) (defaults to: nil)

    Error class name if failed

  • error_message (String, nil) (defaults to: nil)

    Error message if failed



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 = error_message
  @execution_id = execution_id

  # Tracking
  @agent_class_name = agent_class_name
  register_with_tracker
end

Instance Attribute Details

#completed_atObject (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_classObject (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_messageObject (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
  @error_message
end

#execution_idObject (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_classObject (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

#imagesObject (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_idObject (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

#promptObject (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

#qualityObject (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

#sizeObject (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_atObject (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

#styleObject (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_idObject (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

Parameters:

  • data (Hash)

    Cached data

Returns:



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

Returns:

  • (Boolean)

    true if base64



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

Returns:

  • (Boolean)

    true if multiple images



98
99
100
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 98

def batch?
  count > 1
end

#blobsArray<String>

Get all images as binary data

Returns:

  • (Array<String>)

    Array of binary data



244
245
246
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 244

def blobs
  images.map(&:to_blob)
end

#countInteger

Get the number of generated images

Returns:

  • (Integer)

    Image count



172
173
174
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 172

def count
  images.size
end

#dataString?

Get the base64 data of the first image

Returns:

  • (String, nil)

    Base64 encoded image data



128
129
130
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 128

def data
  image&.data
end

#datasArray<String>

Get all base64 data

Returns:

  • (Array<String>)

    Array of 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_msInteger

Get the generation duration in milliseconds

Returns:

  • (Integer)

    Duration in ms



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

Returns:

  • (Boolean)

    true if failed



84
85
86
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 84

def error?
  !success?
end

#executionRubyLLM::Agents::Execution?

Loads the associated Execution record from the database

Returns:



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

#imageObject?

Get the first/only image

Returns:

  • (Object, nil)

    The first image object



107
108
109
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 107

def image
  images.first
end

#input_tokensInteger

Estimate input tokens from prompt

Returns:

  • (Integer)

    Approximate token count



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_typeString?

Get the MIME type

Returns:

  • (String, nil)

    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_promptString?

Get the revised prompt (if model modified it)

Returns:

  • (String, nil)

    The revised prompt



156
157
158
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 156

def revised_prompt
  image&.revised_prompt
end

#revised_promptsArray<String>

Get all revised prompts

Returns:

  • (Array<String>)

    Array of 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

Parameters:

  • path (String)

    File path to save to

Raises:

  • (RuntimeError)

    If no image to save



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

Parameters:

  • directory (String)

    Directory path

  • prefix (String) (defaults to: "image")

    Filename prefix



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

Returns:

  • (Boolean)

    true if single image



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

Returns:

  • (Boolean)

    true if 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_blobString?

Get the first image as binary data

Returns:

  • (String, nil)

    Binary image data



237
238
239
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 237

def to_blob
  image&.to_blob
end

#to_cacheHash

Convert to cacheable format

Returns:

  • (Hash)

    Cache-friendly hash



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_hHash

Convert to hash

Returns:

  • (Hash)

    Hash representation



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: error_message,
    execution_id: execution_id
  }
end

#total_costFloat

Get the total cost for this generation

Uses dynamic pricing from the Pricing module

Returns:

  • (Float)

    Total cost in USD



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

#urlString?

Get the URL of the first image

Returns:

  • (String, nil)

    The image URL



114
115
116
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 114

def url
  image&.url
end

#urlsArray<String>

Get all image URLs

Returns:

  • (Array<String>)

    Array of URLs



121
122
123
# File 'lib/ruby_llm/agents/results/image_generation_result.rb', line 121

def urls
  images.map(&:url).compact
end