Class: RubyLLM::Agents::ImageAnalyzer

Inherits:
Object
  • Object
show all
Extended by:
DSL
Includes:
Execution
Defined in:
lib/ruby_llm/agents/image/analyzer.rb,
lib/ruby_llm/agents/image/analyzer/dsl.rb,
lib/ruby_llm/agents/image/analyzer/execution.rb

Overview

Image analyzer for understanding and captioning images

Analyzes images using vision models to extract captions, tags, descriptions, detected objects, and color information.

Examples:

Basic usage

result = RubyLLM::Agents::ImageAnalyzer.call(image: "path/to/photo.jpg")
result.caption     # => "A sunset over mountains"
result.tags        # => [:nature, :sunset, :mountains]
result.description # => "A detailed description..."

Custom analyzer class

class ProductAnalyzer < RubyLLM::Agents::ImageAnalyzer
  model "gpt-4o"
  analysis_type :detailed
  extract_colors true
  detect_objects true

  description "Analyzes product photos"
end

result = ProductAnalyzer.call(image: product_photo)
result.objects  # => [{name: "laptop", confidence: 0.98, bbox: [...]}]
result.colors   # => [{hex: "#C0C0C0", name: "silver", percentage: 45}]

Defined Under Namespace

Modules: DSL, Execution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Execution

#execute

Constructor Details

#initialize(image:, **options) ⇒ ImageAnalyzer

Initialize a new image analyzer instance

Parameters:

  • image (String, IO)

    Image to analyze (path, URL, or IO object)

  • options (Hash)

    Additional options

Options Hash (**options):

  • :model (String)

    Model to use

  • :analysis_type (Symbol)

    Type of analysis (:caption, :detailed, :tags, :objects)

  • :extract_colors (Boolean)

    Whether to extract color information

  • :detect_objects (Boolean)

    Whether to detect objects

  • :extract_text (Boolean)

    Whether to extract text (OCR)

  • :custom_prompt (String)

    Custom analysis prompt

  • :tenant (Object)

    Tenant for multi-tenancy



76
77
78
79
80
# File 'lib/ruby_llm/agents/image/analyzer.rb', line 76

def initialize(image:, **options)
  @image = image
  @options = options
  @tenant_id = nil
end

Instance Attribute Details

#imageObject (readonly)

Returns the value of attribute image.



63
64
65
# File 'lib/ruby_llm/agents/image/analyzer.rb', line 63

def image
  @image
end

#optionsObject (readonly)

Returns the value of attribute options.



63
64
65
# File 'lib/ruby_llm/agents/image/analyzer.rb', line 63

def options
  @options
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



63
64
65
# File 'lib/ruby_llm/agents/image/analyzer.rb', line 63

def tenant_id
  @tenant_id
end

Class Method Details

.call(image:, **options) ⇒ ImageAnalysisResult

Execute image analysis

Parameters:

  • image (String, IO)

    Path, URL, or IO object of the image to analyze

  • options (Hash)

    Additional options (model, analysis_type, etc.)

Returns:



43
44
45
# File 'lib/ruby_llm/agents/image/analyzer.rb', line 43

def call(image:, **options)
  new(image: image, **options).call
end

.inherited(subclass) ⇒ Object

Ensure subclasses inherit DSL settings



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_llm/agents/image/analyzer.rb', line 48

def inherited(subclass)
  super
  subclass.instance_variable_set(:@model, @model)
  subclass.instance_variable_set(:@analysis_type, @analysis_type)
  subclass.instance_variable_set(:@extract_colors, @extract_colors)
  subclass.instance_variable_set(:@detect_objects, @detect_objects)
  subclass.instance_variable_set(:@extract_text, @extract_text)
  subclass.instance_variable_set(:@custom_prompt, @custom_prompt)
  subclass.instance_variable_set(:@max_tags, @max_tags)
  subclass.instance_variable_set(:@version, @version)
  subclass.instance_variable_set(:@description, @description)
  subclass.instance_variable_set(:@cache_ttl, @cache_ttl)
end

Instance Method Details

#callImageAnalysisResult

Execute the image analysis

Returns:



85
86
87
# File 'lib/ruby_llm/agents/image/analyzer.rb', line 85

def call
  execute
end