Module: RubyLLM::Agents::ImageAnalyzer::DSL

Includes:
Concerns::ImageOperationDSL
Defined in:
lib/ruby_llm/agents/image/analyzer/dsl.rb

Overview

DSL for configuring image analyzers

Provides class-level methods to configure model, analysis type, and feature extraction options.

Examples:

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

Constant Summary collapse

VALID_ANALYSIS_TYPES =
%i[caption detailed tags objects colors all].freeze

Instance Method Summary collapse

Methods included from Concerns::ImageOperationDSL

#cache_enabled?, #cache_for, #cache_ttl, #description, #model

Instance Method Details

#analysis_type(value = nil) ⇒ Symbol

Set or get the analysis type

Parameters:

  • value (Symbol, nil) (defaults to: nil)

    Analysis type (:caption, :detailed, :tags, :objects, :colors, :all)

Returns:

  • (Symbol)

    The analysis type



31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_llm/agents/image/analyzer/dsl.rb', line 31

def analysis_type(value = nil)
  if value
    unless VALID_ANALYSIS_TYPES.include?(value)
      raise ArgumentError, "Analysis type must be one of: #{VALID_ANALYSIS_TYPES.join(", ")}"
    end
    @analysis_type = value
  else
    @analysis_type || inherited_or_default(:analysis_type, :detailed)
  end
end

#custom_prompt(value = nil) ⇒ String?

Set or get a custom analysis prompt

Allows specifying a custom prompt for the vision model to customize what information is extracted.

Parameters:

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

    Custom prompt

Returns:

  • (String, nil)

    The custom prompt



99
100
101
102
103
104
105
# File 'lib/ruby_llm/agents/image/analyzer/dsl.rb', line 99

def custom_prompt(value = nil)
  if value
    @custom_prompt = value
  else
    @custom_prompt || inherited_or_default(:custom_prompt, nil)
  end
end

#detect_objects(value = nil) ⇒ Boolean

Set or get whether to detect objects

When enabled, detects and identifies objects in the image with bounding boxes and confidence scores.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

    Enable object detection

Returns:

  • (Boolean)

    Whether object detection is enabled



66
67
68
69
70
71
72
73
74
# File 'lib/ruby_llm/agents/image/analyzer/dsl.rb', line 66

def detect_objects(value = nil)
  if value.nil?
    result = @detect_objects
    result = inherited_or_default(:detect_objects, false) if result.nil?
    result
  else
    @detect_objects = value
  end
end

#extract_colors(value = nil) ⇒ Boolean

Set or get whether to extract colors

When enabled, extracts dominant colors from the image with hex values, names, and percentages.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

    Enable color extraction

Returns:

  • (Boolean)

    Whether color extraction is enabled



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

def extract_colors(value = nil)
  if value.nil?
    result = @extract_colors
    result = inherited_or_default(:extract_colors, false) if result.nil?
    result
  else
    @extract_colors = value
  end
end

#extract_text(value = nil) ⇒ Boolean

Set or get whether to extract text (OCR)

When enabled, extracts text visible in the image using OCR.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

    Enable text extraction

Returns:

  • (Boolean)

    Whether text extraction is enabled



82
83
84
85
86
87
88
89
90
# File 'lib/ruby_llm/agents/image/analyzer/dsl.rb', line 82

def extract_text(value = nil)
  if value.nil?
    result = @extract_text
    result = inherited_or_default(:extract_text, false) if result.nil?
    result
  else
    @extract_text = value
  end
end

#max_tags(value = nil) ⇒ Integer

Set or get maximum number of tags

Parameters:

  • value (Integer, nil) (defaults to: nil)

    Maximum tags to return

Returns:

  • (Integer)

    The maximum number of tags



111
112
113
114
115
116
117
118
119
120
# File 'lib/ruby_llm/agents/image/analyzer/dsl.rb', line 111

def max_tags(value = nil)
  if value
    unless value.is_a?(Integer) && value > 0
      raise ArgumentError, "Max tags must be a positive integer"
    end
    @max_tags = value
  else
    @max_tags || inherited_or_default(:max_tags, 10)
  end
end