Class: DSPy::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/dspy/image.rb

Constant Summary collapse

SUPPORTED_FORMATS =
%w[image/jpeg image/png image/gif image/webp].freeze
MAX_SIZE_BYTES =

5MB limit

5 * 1024 * 1024
PROVIDER_CAPABILITIES =

Provider capability registry

{
  'openai' => {
    sources: %w[url base64 data],
    parameters: %w[detail]
  },
  'anthropic' => {
    sources: %w[base64 data], 
    parameters: []
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, base64: nil, data: nil, content_type: nil, detail: nil) ⇒ Image

Returns a new instance of Image.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dspy/image.rb', line 25

def initialize(url: nil, base64: nil, data: nil, content_type: nil, detail: nil)
  @detail = detail # OpenAI detail level: 'low', 'high', or 'auto'
  
  # Validate input
  validate_input!(url, base64, data)
  
  if url
    @url = url
    @content_type = content_type || infer_content_type_from_url(url)
  elsif base64
    raise ArgumentError, "content_type is required when using base64" unless content_type
    @base64 = base64
    @content_type = content_type
    validate_size!(Base64.decode64(base64).bytesize)
  elsif data
    raise ArgumentError, "content_type is required when using data" unless content_type
    @data = data
    @content_type = content_type
    validate_size!(data.size)
  end
  
  validate_content_type!
end

Instance Attribute Details

#base64Object (readonly)

Returns the value of attribute base64.



8
9
10
# File 'lib/dspy/image.rb', line 8

def base64
  @base64
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



8
9
10
# File 'lib/dspy/image.rb', line 8

def content_type
  @content_type
end

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/dspy/image.rb', line 8

def data
  @data
end

#detailObject (readonly)

Returns the value of attribute detail.



8
9
10
# File 'lib/dspy/image.rb', line 8

def detail
  @detail
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/dspy/image.rb', line 8

def url
  @url
end

Instance Method Details

#to_anthropic_formatObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dspy/image.rb', line 76

def to_anthropic_format
  if url
    # Anthropic requires base64, so we'd need to fetch the URL
    # For now, we'll raise an error or skip
    raise NotImplementedError, "URL fetching for Anthropic not yet implemented"
  elsif base64
    {
      type: 'image',
      source: {
        type: 'base64',
        media_type: content_type,
        data: base64
      }
    }
  elsif data
    {
      type: 'image',
      source: {
        type: 'base64',
        media_type: content_type,
        data: to_base64
      }
    }
  end
end

#to_base64Object



102
103
104
105
106
# File 'lib/dspy/image.rb', line 102

def to_base64
  return base64 if base64
  return Base64.strict_encode64(data.pack('C*')) if data
  nil
end

#to_openai_formatObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dspy/image.rb', line 49

def to_openai_format
  if url
    format = {
      type: 'image_url',
      image_url: {
        url: url
      }
    }
    format[:image_url][:detail] = detail if detail
    format
  elsif base64
    {
      type: 'image_url',
      image_url: {
        url: "data:#{content_type};base64,#{base64}"
      }
    }
  elsif data
    {
      type: 'image_url',
      image_url: {
        url: "data:#{content_type};base64,#{to_base64}"
      }
    }
  end
end

#validate!Object



108
109
110
111
112
113
114
115
116
# File 'lib/dspy/image.rb', line 108

def validate!
  validate_content_type!
  
  if base64
    validate_size!(Base64.decode64(base64).bytesize)
  elsif data
    validate_size!(data.size)
  end
end

#validate_for_provider!(provider) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/dspy/image.rb', line 118

def validate_for_provider!(provider)
  capabilities = PROVIDER_CAPABILITIES[provider]
  
  unless capabilities
    raise DSPy::LM::IncompatibleImageFeatureError, 
          "Unknown provider '#{provider}'. Supported providers: #{PROVIDER_CAPABILITIES.keys.join(', ')}"
  end
  
  # Check source compatibility
  current_source = if url
                     'url'
                   elsif base64
                     'base64'
                   elsif data
                     'data'
                   end
  
  unless capabilities[:sources].include?(current_source)
    case provider
    when 'anthropic'
      if current_source == 'url'
        raise DSPy::LM::IncompatibleImageFeatureError,
              "Anthropic doesn't support image URLs. Please provide base64 or raw data instead."
      end
    end
  end
  
  # Check parameter compatibility
  if detail && !capabilities[:parameters].include?('detail')
    case provider
    when 'anthropic'
      raise DSPy::LM::IncompatibleImageFeatureError,
            "Anthropic doesn't support the 'detail' parameter. This feature is OpenAI-specific."
    end
  end
end