Module: Lammy::Embeddings

Defined in:
lib/lammy/embeddings.rb

Class Method Summary collapse

Class Method Details

.handle(klass, method_name, settings) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lammy/embeddings.rb', line 5

def self.handle(klass, method_name, settings)
  # Unbind the original method
  original_method = klass.instance_method(method_name)

  # Redefine the method
  klass.define_method(method_name) do |*args, &block|
    # # Initialize chunking settings
    # @chunk_by_size = nil

    # # Make `chunk_by_size` method available within the instance
    # define_singleton_method(:chunk_by_size) do |size|
    #   @chunk_by_size = size
    # end

    # Call the original method to get the input
    input = original_method.bind(self).call(*args, &block)

    # # Tokenize the input
    # if @chunk_by_size.blank?
    #   input = [ input ]
    # else
    #   tokenizer = Tokenizers.from_pretrained("bert-base-cased")
    #   input = tokenizer.encode(input).tokens
    # end
    input = [input]

    case settings[:model]
    when *OpenAI::EMBEDDINGS
      client = OpenAI.new(settings)
      client.embeddings(input)
    else
      raise "Unsupported model: #{settings[:model]}"
    end
  end
end