Class: DSPy::Image
- Inherits:
-
Object
- Object
- DSPy::Image
- 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: [] }, 'gemini' => { sources: %w[base64 data], # Gemini supports inline base64 data, not URLs parameters: [] } }.freeze
Instance Attribute Summary collapse
-
#base64 ⇒ Object
readonly
Returns the value of attribute base64.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#detail ⇒ Object
readonly
Returns the value of attribute detail.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url: nil, base64: nil, data: nil, content_type: nil, detail: nil) ⇒ Image
constructor
A new instance of Image.
- #to_anthropic_format ⇒ Object
- #to_base64 ⇒ Object
- #to_gemini_format ⇒ Object
- #to_openai_format ⇒ Object
- #validate! ⇒ Object
- #validate_for_provider!(provider) ⇒ Object
Constructor Details
#initialize(url: nil, base64: nil, data: nil, content_type: nil, detail: nil) ⇒ Image
Returns a new instance of Image.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/dspy/image.rb', line 29 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
#base64 ⇒ Object (readonly)
Returns the value of attribute base64.
8 9 10 |
# File 'lib/dspy/image.rb', line 8 def base64 @base64 end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
8 9 10 |
# File 'lib/dspy/image.rb', line 8 def content_type @content_type end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/dspy/image.rb', line 8 def data @data end |
#detail ⇒ Object (readonly)
Returns the value of attribute detail.
8 9 10 |
# File 'lib/dspy/image.rb', line 8 def detail @detail end |
#url ⇒ Object (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_format ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/dspy/image.rb', line 80 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_base64 ⇒ Object
127 128 129 130 131 |
# File 'lib/dspy/image.rb', line 127 def to_base64 return base64 if base64 return Base64.strict_encode64(data.pack('C*')) if data nil end |
#to_gemini_format ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/dspy/image.rb', line 106 def to_gemini_format if url # Gemini requires base64 for inline data, URLs not supported for inline_data raise NotImplementedError, "URL fetching for Gemini not yet implemented. Use base64 or data instead." elsif base64 { inline_data: { mime_type: content_type, data: base64 } } elsif data { inline_data: { mime_type: content_type, data: to_base64 } } end end |
#to_openai_format ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/dspy/image.rb', line 53 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
133 134 135 136 137 138 139 140 141 |
# File 'lib/dspy/image.rb', line 133 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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/dspy/image.rb', line 143 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 when 'gemini' if current_source == 'url' raise DSPy::LM::IncompatibleImageFeatureError, "Gemini doesn't support image URLs for inline data. 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." when 'gemini' raise DSPy::LM::IncompatibleImageFeatureError, "Gemini doesn't support the 'detail' parameter. This feature is OpenAI-specific." end end end |