Module: LLM::Huggingface

Defined in:
lib/scout/llm/backends/huggingface.rb

Class Method Summary collapse

Class Method Details

.ask(question, options = {}, &block) ⇒ Object



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/scout/llm/backends/huggingface.rb', line 16

def self.ask(question, options = {}, &block)
  model_options = IndiferentHash.pull_keys options, :model
  model_options = IndiferentHash.add_defaults model_options, :task => "CausalLM"

  model = self.model model_options

  messages = LLM.messages(question)

  system = []
  prompt = []
  messages.each do |message|
    role, content = message.values_at :role, :content
    if role == 'system'
      system << content
    else
      prompt << content
    end
  end

  parameters = options.merge(messages: messages)
  Log.debug "Calling client with parameters: #{Log.fingerprint parameters}"

  model.eval(messages)
end

.embed(text, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/scout/llm/backends/huggingface.rb', line 41

def self.embed(text, options = {})
  model_options = IndiferentHash.pull_keys options, :model
  model_options = IndiferentHash.add_defaults model_options, :task => "Embedding"

  model = self.model model_options

  (Array === text) ? model.eval_list(text) : model.eval(text)
end

.model(model_options) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/scout/llm/backends/huggingface.rb', line 6

def self.model(model_options)
  require 'scout/model/python/huggingface'
  require 'scout/model/python/huggingface/causal'

  model, task, checkpoint, dir = IndiferentHash.process_options model_options, :model, :task, :checkpoint, :dir
  model ||= Scout::Config.get(:model, :huggingface, env: 'HUGGINGFACE_MODEL,HF_MODEL')

  CausalModel.new model, dir, model_options
end