Class: RubyLLM::Agents::ImageAnalysisResult

Inherits:
Object
  • Object
show all
Includes:
Trackable
Defined in:
lib/ruby_llm/agents/results/image_analysis_result.rb

Overview

Result wrapper for image analysis operations

Provides a consistent interface for accessing analysis data including captions, tags, objects, colors, and extracted text.

Examples:

Accessing analysis data

result = ImageAnalyzer.call(image: "photo.jpg")
result.caption      # => "A sunset over mountains"
result.tags         # => ["sunset", "mountains", "nature"]
result.objects      # => [{name: "mountain", location: "center", confidence: "high"}]
result.colors       # => [{hex: "#FF6B35", name: "orange", percentage: 30}]
result.description  # => "A detailed description..."
result.success?     # => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Trackable

included

Constructor Details

#initialize(image:, model_id:, analysis_type:, caption:, description:, tags:, objects:, colors:, text:, raw_response:, started_at:, completed_at:, tenant_id:, analyzer_class:, error_class: nil, error_message: nil, agent_class_name: nil) ⇒ ImageAnalysisResult

Initialize a new result

Parameters:

  • image (String)

    The analyzed image path or URL

  • model_id (String)

    Model used for analysis

  • analysis_type (Symbol)

    Type of analysis performed

  • caption (String, nil)

    Brief caption of the image

  • description (String, nil)

    Detailed description

  • tags (Array<String>)

    Tags/keywords for the image

  • objects (Array<Hash>)

    Detected objects with metadata

  • colors (Array<Hash>)

    Dominant colors with hex, name, percentage

  • text (String, nil)

    Extracted text (OCR)

  • raw_response (Hash, String, nil)

    Raw response from the model

  • started_at (Time)

    When analysis started

  • completed_at (Time)

    When analysis completed

  • tenant_id (String, nil)

    Tenant identifier

  • analyzer_class (String)

    Name of the analyzer class

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

    Error class name if failed

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

    Error message if failed



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 46

def initialize(image:, model_id:, analysis_type:, caption:, description:, tags:,
  objects:, colors:, text:, raw_response:, started_at:, completed_at:,
  tenant_id:, analyzer_class:, error_class: nil, error_message: nil, agent_class_name: nil)
  @image = image
  @model_id = model_id
  @analysis_type = analysis_type
  @caption = caption
  @description = description
  @tags = tags || []
  @objects = objects || []
  @colors = colors || []
  @text = text
  @raw_response = raw_response
  @started_at = started_at
  @completed_at = completed_at
  @tenant_id = tenant_id
  @analyzer_class = analyzer_class
  @error_class = error_class
  @error_message = error_message
  @execution_id = nil

  # Tracking
  @agent_class_name = agent_class_name
  register_with_tracker
end

Instance Attribute Details

#analysis_typeObject (readonly)

Returns the value of attribute analysis_type.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def analysis_type
  @analysis_type
end

#analyzer_classObject (readonly)

Returns the value of attribute analyzer_class.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def analyzer_class
  @analyzer_class
end

#captionObject (readonly)

Returns the value of attribute caption.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def caption
  @caption
end

#colorsObject (readonly)

Returns the value of attribute colors.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def colors
  @colors
end

#completed_atObject (readonly)

Returns the value of attribute completed_at.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def completed_at
  @completed_at
end

#descriptionObject (readonly)

Returns the value of attribute description.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def description
  @description
end

#error_classObject (readonly)

Returns the value of attribute error_class.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def error_class
  @error_class
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def error_message
  @error_message
end

#execution_idObject

Returns the value of attribute execution_id.



26
27
28
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 26

def execution_id
  @execution_id
end

#imageObject (readonly)

Returns the value of attribute image.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def image
  @image
end

#model_idObject (readonly)

Returns the value of attribute model_id.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def model_id
  @model_id
end

#objectsObject (readonly)

Returns the value of attribute objects.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def objects
  @objects
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def raw_response
  @raw_response
end

#started_atObject (readonly)

Returns the value of attribute started_at.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def started_at
  @started_at
end

#tagsObject (readonly)

Returns the value of attribute tags.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def tags
  @tags
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def tenant_id
  @tenant_id
end

#textObject (readonly)

Returns the value of attribute text.



22
23
24
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22

def text
  @text
end

Class Method Details

.from_cache(data) ⇒ Object



248
249
250
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 248

def self.from_cache(data)
  CachedImageAnalysisResult.new(data)
end

Instance Method Details

#batch?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 94

def batch?
  false
end

#caption?Boolean

Check if the result has a caption

Returns:

  • (Boolean)


106
107
108
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 106

def caption?
  caption.present?
end

#colors?Boolean

Check if the result has color information

Returns:

  • (Boolean)


126
127
128
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 126

def colors?
  colors.any?
end

#countObject

Count (always 1 for analysis)



99
100
101
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 99

def count
  success? ? 1 : 0
end

#description?Boolean

Check if the result has a description

Returns:

  • (Boolean)


111
112
113
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 111

def description?
  description.present?
end

#dominant_colorHash?

Get the dominant color (highest percentage)

Returns:

  • (Hash, nil)

    The dominant color or nil



145
146
147
148
149
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 145

def dominant_color
  return nil unless colors?

  colors.max_by { |c| c[:percentage] || 0 }
end

#duration_msObject

Timing



186
187
188
189
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 186

def duration_ms
  return 0 unless started_at && completed_at
  ((completed_at - started_at) * 1000).round
end

#error?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 85

def error?
  !success?
end

#executionRubyLLM::Agents::Execution?

Loads the associated Execution record from the database

Returns:



75
76
77
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 75

def execution
  @execution ||= RubyLLM::Agents::Execution.find_by(id: execution_id) if execution_id
end

#high_confidence_objectsArray<Hash>

Get high-confidence objects

Returns:

  • (Array<Hash>)

    High-confidence objects



162
163
164
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 162

def high_confidence_objects
  objects_with_confidence("high")
end

#includes_object?(name) ⇒ Boolean Also known as: has_object?

Check if a specific object was detected

Parameters:

  • name (String)

    Object name to search for

Returns:

  • (Boolean)

    Whether the object was detected



170
171
172
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 170

def includes_object?(name)
  objects.any? { |obj| obj[:name]&.downcase&.include?(name.to_s.downcase) }
end

#includes_tag?(tag) ⇒ Boolean Also known as: has_tag?

Check if a specific tag is present

Parameters:

  • tag (String)

    Tag to search for

Returns:

  • (Boolean)

    Whether the tag is present



179
180
181
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 179

def includes_tag?(tag)
  tags.any? { |t| t.downcase == tag.to_s.downcase }
end

#objects?Boolean

Check if the result has detected objects

Returns:

  • (Boolean)


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

def objects?
  objects.any?
end

#objects_with_confidence(confidence) ⇒ Array<Hash>

Get objects by confidence level

Parameters:

  • confidence (String)

    Confidence level (“high”, “medium”, “low”)

Returns:

  • (Array<Hash>)

    Objects with matching confidence



155
156
157
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 155

def objects_with_confidence(confidence)
  objects.select { |obj| obj[:confidence]&.downcase == confidence.to_s.downcase }
end

#single?Boolean

Analysis is always single

Returns:

  • (Boolean)


90
91
92
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 90

def single?
  true
end

#success?Boolean

Status helpers

Returns:

  • (Boolean)


81
82
83
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 81

def success?
  error_class.nil? && (caption.present? || description.present? || tags.any?)
end

#tag_symbolsArray<Symbol>

Get tags as symbols

Returns:

  • (Array<Symbol>)

    Tags as symbols



138
139
140
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 138

def tag_symbols
  tags.map { |t| t.to_s.downcase.gsub(/\s+/, "_").to_sym }
end

#tags?Boolean

Check if the result has tags

Returns:

  • (Boolean)


116
117
118
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 116

def tags?
  tags.any?
end

#text?Boolean

Check if text was extracted

Returns:

  • (Boolean)


131
132
133
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 131

def text?
  text.present?
end

#to_cacheObject

Caching



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 232

def to_cache
  {
    image: image,
    model_id: model_id,
    analysis_type: analysis_type,
    caption: caption,
    description: description,
    tags: tags,
    objects: objects,
    colors: colors,
    text: text,
    total_cost: total_cost,
    cached_at: Time.current.iso8601
  }
end

#to_hObject

Serialization



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 206

def to_h
  {
    success: success?,
    image: image,
    model_id: model_id,
    analysis_type: analysis_type,
    caption: caption,
    description: description,
    tags: tags,
    objects: objects,
    colors: colors,
    text: text,
    total_cost: total_cost,
    duration_ms: duration_ms,
    started_at: started_at&.iso8601,
    completed_at: completed_at&.iso8601,
    tenant_id: tenant_id,
    analyzer_class: analyzer_class,
    error_class: error_class,
    error_message: error_message,
    execution_id: execution_id
  }
end

#total_costObject

Cost estimation



193
194
195
196
197
198
199
200
201
202
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 193

def total_cost
  return 0 if error?

  # Analysis typically uses vision model pricing
  # Estimate based on model and image size
  ImageGenerator::Pricing.calculate_cost(
    model_id: model_id,
    count: 1
  )
end