ruby_llm 0.1.0.pre43 → 0.1.0.pre44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2433d0b669ef8bd499507edf33ed672d011fc1c95c2de0febd4e83d2593e0b8
4
- data.tar.gz: a818a1c59220404c29c0e562c473480ac630a6fd53fadee65ab102b8955e9ec5
3
+ metadata.gz: 519b87700f2ba3d5aeb8ea3a87d0881f4ef58f974991647b7af4c9f138cbf3d2
4
+ data.tar.gz: 553b6441ad59fe5efe6d51374103690101c2cb8d072f91bf2a850d84b4934601
5
5
  SHA512:
6
- metadata.gz: afa3882a1850692e63f16dbc8c379a9aa71d704096f1eb2d5d88aeefa0c9c17f2585e8e215be0ed126caa0ec6bb7d07b4c12b5b6eaa5a1cf69a054b976b0d9e8
7
- data.tar.gz: a8089658eab75b8200f51d39568dc876cdcc2d61d6581dbd1e6c211f487fa127c9300e0fcbb318f958b6b109f5fb43c2f7f6824acd2f426e4a0a9ae2a7023c63
6
+ metadata.gz: e52cb96479a0f4443521bf3b5b2e98c882fb1c404e3d1527c74f638137bf56c2cb9442cc6e30ef197157fbdaa7dc189b7f6331a31e6f31ddfa28a926035d6578
7
+ data.tar.gz: b1d7948851e6c1ffa5257e214f01e6d6efaf64d32d98dcc66ec8f5d6776df232a92b3b26ff1de78889f4195ff27e41dc7375a4b2530dc1b97edc8fd57bb2c835
data/.rspec_status CHANGED
@@ -35,7 +35,7 @@ example_id | status | run_time |
35
35
  ./spec/ruby_llm/embeddings_spec.rb[1:1:2:1] | passed | 0.65614 seconds |
36
36
  ./spec/ruby_llm/embeddings_spec.rb[1:1:2:2] | passed | 2.16 seconds |
37
37
  ./spec/ruby_llm/error_handling_spec.rb[1:1] | passed | 0.29366 seconds |
38
- ./spec/ruby_llm/image_generation_spec.rb[1:1:1] | passed | 11.61 seconds |
39
- ./spec/ruby_llm/image_generation_spec.rb[1:1:2] | passed | 17.63 seconds |
40
- ./spec/ruby_llm/image_generation_spec.rb[1:1:3] | passed | 8.77 seconds |
41
- ./spec/ruby_llm/image_generation_spec.rb[1:1:4] | passed | 0.00319 seconds |
38
+ ./spec/ruby_llm/image_generation_spec.rb[1:1:1] | passed | 24.16 seconds |
39
+ ./spec/ruby_llm/image_generation_spec.rb[1:1:2] | passed | 14.81 seconds |
40
+ ./spec/ruby_llm/image_generation_spec.rb[1:1:3] | passed | 9.17 seconds |
41
+ ./spec/ruby_llm/image_generation_spec.rb[1:1:4] | passed | 0.00083 seconds |
@@ -3,16 +3,39 @@
3
3
  module RubyLLM
4
4
  # Represents a generated image from an AI model.
5
5
  # Provides an interface to image generation capabilities
6
- # from providers like DALL-E.
6
+ # from providers like DALL-E and Gemini's Imagen.
7
7
  class Image
8
- attr_reader :url, :revised_prompt, :model_id
8
+ attr_reader :url, :data, :mime_type, :revised_prompt, :model_id
9
9
 
10
- def initialize(url:, revised_prompt: nil, model_id: nil)
10
+ def initialize(url: nil, data: nil, mime_type: nil, revised_prompt: nil, model_id: nil)
11
11
  @url = url
12
+ @data = data
13
+ @mime_type = mime_type
12
14
  @revised_prompt = revised_prompt
13
15
  @model_id = model_id
14
16
  end
15
17
 
18
+ def base64?
19
+ !@data.nil?
20
+ end
21
+
22
+ # Returns the raw binary image data regardless of source
23
+ def to_blob
24
+ if base64?
25
+ Base64.decode64(@data)
26
+ else
27
+ # Use Faraday instead of URI.open for better security
28
+ response = Faraday.get(@url)
29
+ response.body
30
+ end
31
+ end
32
+
33
+ # Saves the image to a file path
34
+ def save(path)
35
+ File.binwrite(File.expand_path(path), to_blob)
36
+ path
37
+ end
38
+
16
39
  def self.paint(prompt, model: nil, size: '1024x1024')
17
40
  model_id = model || RubyLLM.config.default_image_model
18
41
  Models.find(model_id) # Validate model exists
@@ -158,6 +158,16 @@ module RubyLLM
158
158
  end
159
159
  end
160
160
 
161
+ def parse_data_uri(uri)
162
+ if uri&.start_with?('data:')
163
+ match = uri.match(/\Adata:([^;]+);base64,(.+)\z/)
164
+ return { mime_type: match[1], data: match[2] } if match
165
+ end
166
+
167
+ # If it's not a data URI, return nil
168
+ nil
169
+ end
170
+
161
171
  class << self
162
172
  def extended(base)
163
173
  base.extend(Methods)
@@ -37,12 +37,13 @@ module RubyLLM
37
37
  raise Error, 'Unexpected response format from Gemini image generation API'
38
38
  end
39
39
 
40
- # Handle response with base64 encoded image data
41
- image_url = "data:#{image_data['mimeType'] || 'image/png'};base64,#{image_data['bytesBase64Encoded']}"
40
+ # Extract mime type and base64 data
41
+ mime_type = image_data['mimeType'] || 'image/png'
42
+ base64_data = image_data['bytesBase64Encoded']
43
+
42
44
  Image.new(
43
- url: image_url,
44
- revised_prompt: '', # Imagen doesn't return revised prompts
45
- model_id: ''
45
+ data: base64_data,
46
+ mime_type: mime_type
46
47
  )
47
48
  end
48
49
  end
@@ -26,6 +26,7 @@ module RubyLLM
26
26
 
27
27
  Image.new(
28
28
  url: image_data['url'],
29
+ mime_type: 'image/png', # DALL-E typically returns PNGs
29
30
  revised_prompt: image_data['revised_prompt'],
30
31
  model_id: data['model']
31
32
  )
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyLLM
4
- VERSION = '0.1.0.pre43'
4
+ VERSION = '0.1.0.pre44'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_llm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre43
4
+ version: 0.1.0.pre44
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carmine Paolino