Class: RubyLLM::Agents::ImageAnalysisResult
- Inherits:
-
Object
- Object
- RubyLLM::Agents::ImageAnalysisResult
- 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.
Instance Attribute Summary collapse
-
#analysis_type ⇒ Object
readonly
Returns the value of attribute analysis_type.
-
#analyzer_class ⇒ Object
readonly
Returns the value of attribute analyzer_class.
-
#caption ⇒ Object
readonly
Returns the value of attribute caption.
-
#colors ⇒ Object
readonly
Returns the value of attribute colors.
-
#completed_at ⇒ Object
readonly
Returns the value of attribute completed_at.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#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
Returns the value of attribute execution_id.
-
#image ⇒ Object
readonly
Returns the value of attribute image.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
-
#raw_response ⇒ Object
readonly
Returns the value of attribute raw_response.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#tenant_id ⇒ Object
readonly
Returns the value of attribute tenant_id.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Class Method Summary collapse
Instance Method Summary collapse
- #batch? ⇒ Boolean
-
#caption? ⇒ Boolean
Check if the result has a caption.
-
#colors? ⇒ Boolean
Check if the result has color information.
-
#count ⇒ Object
Count (always 1 for analysis).
-
#description? ⇒ Boolean
Check if the result has a description.
-
#dominant_color ⇒ Hash?
Get the dominant color (highest percentage).
-
#duration_ms ⇒ Object
Timing.
- #error? ⇒ Boolean
-
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database.
-
#high_confidence_objects ⇒ Array<Hash>
Get high-confidence objects.
-
#includes_object?(name) ⇒ Boolean
(also: #has_object?)
Check if a specific object was detected.
-
#includes_tag?(tag) ⇒ Boolean
(also: #has_tag?)
Check if a specific tag is present.
-
#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
constructor
Initialize a new result.
-
#objects? ⇒ Boolean
Check if the result has detected objects.
-
#objects_with_confidence(confidence) ⇒ Array<Hash>
Get objects by confidence level.
-
#single? ⇒ Boolean
Analysis is always single.
-
#success? ⇒ Boolean
Status helpers.
-
#tag_symbols ⇒ Array<Symbol>
Get tags as symbols.
-
#tags? ⇒ Boolean
Check if the result has tags.
-
#text? ⇒ Boolean
Check if text was extracted.
-
#to_cache ⇒ Object
Caching.
-
#to_h ⇒ Object
Serialization.
-
#total_cost ⇒ Object
Cost estimation.
Methods included from Trackable
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
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 = || [] @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 = @execution_id = nil # Tracking @agent_class_name = agent_class_name register_with_tracker end |
Instance Attribute Details
#analysis_type ⇒ Object (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_class ⇒ Object (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 |
#caption ⇒ Object (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 |
#colors ⇒ Object (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_at ⇒ Object (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 |
#description ⇒ Object (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_class ⇒ Object (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_message ⇒ Object (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 end |
#execution_id ⇒ Object
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 |
#image ⇒ Object (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_id ⇒ Object (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 |
#objects ⇒ Object (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_response ⇒ Object (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_at ⇒ Object (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 |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
22 23 24 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 22 def @tags end |
#tenant_id ⇒ Object (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 |
#text ⇒ Object (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
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
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
126 127 128 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 126 def colors? colors.any? end |
#count ⇒ Object
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
111 112 113 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 111 def description? description.present? end |
#dominant_color ⇒ Hash?
Get the dominant color (highest percentage)
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_ms ⇒ Object
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
85 86 87 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 85 def error? !success? end |
#execution ⇒ RubyLLM::Agents::Execution?
Loads the associated Execution record from the database
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_objects ⇒ Array<Hash>
Get 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
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
179 180 181 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 179 def includes_tag?(tag) .any? { |t| t.downcase == tag.to_s.downcase } end |
#objects? ⇒ Boolean
Check if the result has detected objects
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
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
90 91 92 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 90 def single? true end |
#success? ⇒ Boolean
Status helpers
81 82 83 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 81 def success? error_class.nil? && (caption.present? || description.present? || .any?) end |
#tag_symbols ⇒ Array<Symbol>
Get tags as symbols
138 139 140 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 138 def tag_symbols .map { |t| t.to_s.downcase.gsub(/\s+/, "_").to_sym } end |
#tags? ⇒ Boolean
Check if the result has tags
116 117 118 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 116 def .any? end |
#text? ⇒ Boolean
Check if text was extracted
131 132 133 |
# File 'lib/ruby_llm/agents/results/image_analysis_result.rb', line 131 def text? text.present? end |
#to_cache ⇒ Object
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: , objects: objects, colors: colors, text: text, total_cost: total_cost, cached_at: Time.current.iso8601 } end |
#to_h ⇒ Object
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: , 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: , execution_id: execution_id } end |
#total_cost ⇒ Object
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 |