emb 0.4.0.pre4 → 0.4.0.pre5

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/emb/proxy.rb +43 -0
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0c6f451b27da29d343a893e88a8ae949620e33213941a785088d0e66f1cbe8ab
4
- data.tar.gz: 7eb7bc8fa4069bedc853d1580c5504a1a1fbbdff432ff458f61152487199de78
3
+ metadata.gz: a9b606e95ab03ea635c8ec7acf1aaa0024a4646cb1a8f41d2e3509c8acefedfa
4
+ data.tar.gz: 78c102fe8b5f547104e5b0e124223f34b4694b19de7c5bccdf2881423fedbbd7
5
5
  SHA512:
6
- metadata.gz: 94e720e10c56ccca0655c7dab9e28ac71d1731a4da5206664338be49de3dd2756f2acc56aa938a0b949f22dbe38927e2231abfee44cdf5339646079d8bf01fd0
7
- data.tar.gz: 115a9d1814d895c856f39f6a557625cdb19abb59628dca39e314f35ace1c97c171c5a44c3c06c53500a6dee4332602f43bc4da04f6dabf0da053524ca43b6030
6
+ metadata.gz: c8cd7137ff7332f70537789a6bd9ddac1cf3d591156c3bf51676e5242236e8aabd247312e1a166a2aa584ad3041d45a3016979b383b640ffc8dbc6ecc2cf8b64
7
+ data.tar.gz: ef7a6c243c471b0026c4b35b73120e10b38a2384e12de142e5ebef1b3a9c3200a8180bf1b109eb8282114f7546060b43eed1a3a76adc6e8ad14b09d4da3a4b4e
data/lib/emb/proxy.rb CHANGED
@@ -38,8 +38,51 @@ module Emb
38
38
  "#<Emb::Proxy #{@name}>"
39
39
  end
40
40
 
41
+ # Embeds one or more raw images (EMB.IMG). Each argument is the encoded
42
+ # bytes of an image (JPEG/PNG/GIF/WebP) and is sent unchanged as an
43
+ # ASCII-8BIT RESP bulk, so no base64/URL encoding is needed:
44
+ #
45
+ # Emb[:clip].image(File.binread('cat.jpg'))
46
+ # Emb[:clip].image(io1, io2) # IO objects are read
47
+ #
48
+ # format: :binary (default) returns the float32-unpacked vectors from the
49
+ # compact BLOB wire (nil for a failed/truncated slot); format: :values
50
+ # returns the decoded dtype/shape/values envelope. The float32 reply decode
51
+ # is the same `unpack('e*')` path text embeddings use.
52
+ def image(image_bytes, *more, format: :binary)
53
+ format = normalize_format(format)
54
+ images = [image_bytes, *more].map { |bytes| self.class.coerce_image_bytes(bytes) }
55
+
56
+ if format == :values
57
+ reply = @client.send_command('EMB.IMG', @name.to_s, 'VALUES', *images)
58
+ envelope = Emb::ValuesReply.parse(reply)
59
+ return envelope if images.size == 1
60
+
61
+ return envelope.merge(values: Emb::ValuesReply.rows(envelope[:shape], envelope[:values]))
62
+ end
63
+
64
+ reply = @client.send_command('EMB.IMG', @name.to_s, *images)
65
+ return unpack_embedding(reply) if images.size == 1
66
+
67
+ Array(reply).map { |entry| entry.nil? ? nil : unpack_embedding(entry) }
68
+ end
69
+
70
+ # coerce_image_bytes normalizes an image argument to a binary byte string
71
+ # without transcoding: a String's bytes are preserved exactly (only the
72
+ # encoding label is set to ASCII-8BIT), and an IO is read fully.
73
+ def self.coerce_image_bytes(bytes)
74
+ data = bytes.respond_to?(:read) ? bytes.read : bytes
75
+ data = data.to_s
76
+ data = data.dup if data.frozen?
77
+ data.force_encoding(Encoding::BINARY)
78
+ end
79
+
41
80
  private
42
81
 
82
+ def unpack_embedding(entry)
83
+ entry.nil? ? nil : entry.unpack('e*')
84
+ end
85
+
43
86
  def normalize_format(format)
44
87
  unless %i[binary values].include?(format)
45
88
  raise ArgumentError, "unknown format #{format.inspect} (expected :binary or :values)"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0.pre4
4
+ version: 0.4.0.pre5
5
5
  platform: ruby
6
6
  authors:
7
7
  - elcuervo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-09-10 00:00:00.000000000 Z
11
+ date: 2026-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: batch-loader