Module: Emb

Defined in:
lib/emb.rb,
lib/emb/batch.rb,
lib/emb/multi.rb,
lib/emb/proxy.rb,
lib/emb/client.rb,
lib/emb/version.rb,
lib/emb/middleware.rb,
lib/emb/configuration.rb

Defined Under Namespace

Classes: BatchModelProxy, BatchProxy, Client, Configuration, Middleware, MultiProxy, Proxy

Constant Summary collapse

BATCH_KEY =
:emb
BATCH_BLOCK =
lambda do |items, loader, _args|
  items.group_by(&:first).each do |client, client_items|
    pairs = client_items.flat_map { |_, model, text| Array(text).flat_map { |t| [model.to_s, t] } }
    results = Array(client.send_command('EMB.MULTI', *pairs))

    offset = 0
    client_items.each do |item|
      _, _, text = item
      texts = Array(text)
      values = results[offset, texts.size].map { |entry| entry&.unpack('e*') }
      offset += texts.size

      # eager Proxy#[] shape: vector for a single text, vectors for many
      loader.call(item, values.size == 1 ? values.first : values)
    end
  end
end
VERSION =
Gem.loaded_specs['emb']&.version&.to_s || '0.0.0'

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Object



30
# File 'lib/emb.rb', line 30

def [](name)    = default_client[name]

.batchObject



31
# File 'lib/emb.rb', line 31

def batch       = default_client.batch

.build_batch_loader(client, model, text) ⇒ Object



27
28
29
# File 'lib/emb/batch.rb', line 27

def build_batch_loader(client, model, text)
  BatchLoader.for([client, model, text]).batch(key: BATCH_KEY, &BATCH_BLOCK)
end

.configurationObject



21
22
23
# File 'lib/emb.rb', line 21

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



25
26
27
28
# File 'lib/emb.rb', line 25

def configure
  yield configuration if block_given?
  configuration
end

.debug!Object



44
45
46
# File 'lib/emb.rb', line 44

def debug!
  @debug = true
end

.debug?Boolean

Returns:

  • (Boolean)


41
# File 'lib/emb.rb', line 41

def debug? = @debug

.helpObject



35
# File 'lib/emb.rb', line 35

def help        = default_client.help

.info(name) ⇒ Object



33
# File 'lib/emb.rb', line 33

def info(name)  = default_client.info(name)

.modelsObject



32
# File 'lib/emb.rb', line 32

def models      = default_client.models

.multiObject



39
# File 'lib/emb.rb', line 39

def multi(&)    = default_client.multi(&)

.newObject



13
# File 'lib/emb.rb', line 13

def new(...) = Client.new(...)

.pingObject



36
# File 'lib/emb.rb', line 36

def ping        = default_client.ping

.readyObject



37
# File 'lib/emb.rb', line 37

def ready       = default_client.ready

.ready?Boolean

Returns:

  • (Boolean)


38
# File 'lib/emb.rb', line 38

def ready?      = default_client.ready?

.reset_registry!Object



40
# File 'lib/emb.rb', line 40

def reset_registry! = default_client.reset_registry!

.send_commandObject



42
# File 'lib/emb.rb', line 42

def send_command(*) = default_client.send_command(*)

.setupObject Also known as: config



15
16
17
# File 'lib/emb.rb', line 15

def setup(...)
  @default_client = Client.new(...)
end

.statsObject



34
# File 'lib/emb.rb', line 34

def stats       = default_client.stats