Class: Emb::MultiProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/emb/multi.rb

Defined Under Namespace

Classes: PairCollector

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ MultiProxy

Returns a new instance of MultiProxy.



5
6
7
8
# File 'lib/emb/multi.rb', line 5

def initialize(client)
  @client = client
  @pairs = []
end

Instance Method Details

#[](name) ⇒ Object



10
11
12
# File 'lib/emb/multi.rb', line 10

def [](name)
  PairCollector.new(@pairs, name.to_sym)
end

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/emb/multi.rb', line 14

def run
  chunk = @client.respond_to?(:batch_size) && @client.batch_size ? @client.batch_size : Emb.configuration.batch_size

  # Chunk at batch_size pairs per EMB.MULTI so a single command stays well
  # under the server's max_pairs cap and within client read timeouts.
  @pairs.each_slice(chunk).flat_map do |slice|
    args = slice.flat_map { |pair| [pair[:model].to_s, pair[:text]] }

    @client
      .send_command('EMB.MULTI', *args)
      .map { |entry| entry&.unpack('e*') }
  end
end