Class: RagRuby::Embedders::HuggingFace

Inherits:
Base
  • Object
show all
Defined in:
lib/rag_ruby/embedders/hugging_face.rb

Constant Summary collapse

ENDPOINT =
"https://api-inference.huggingface.co/pipeline/feature-extraction"

Instance Method Summary collapse

Constructor Details

#initialize(model: "sentence-transformers/all-MiniLM-L6-v2", api_key: nil) ⇒ HuggingFace

Returns a new instance of HuggingFace.

Raises:

  • (ArgumentError)


12
13
14
15
16
# File 'lib/rag_ruby/embedders/hugging_face.rb', line 12

def initialize(model: "sentence-transformers/all-MiniLM-L6-v2", api_key: nil)
  @model = model
  @api_key = api_key || ENV["HUGGINGFACE_API_KEY"]
  raise ArgumentError, "HuggingFace API key is required (set HUGGINGFACE_API_KEY or pass api_key:)" unless @api_key
end

Instance Method Details

#dimension ⇒ Object



44
45
46
# File 'lib/rag_ruby/embedders/hugging_face.rb', line 44

def dimension
  384
end

#embed(text) ⇒ Object



18
19
20
# File 'lib/rag_ruby/embedders/hugging_face.rb', line 18

def embed(text)
  embed_batch([text]).first
end

#embed_batch(texts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rag_ruby/embedders/hugging_face.rb', line 22

def embed_batch(texts)
  uri = URI.parse("#{ENDPOINT}/#{@model}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.open_timeout = 30
  http.read_timeout = 120

  req = Net::HTTP::Post.new(uri)
  req["Authorization"] = "Bearer #{@api_key}"
  req["Content-Type"] = "application/json"
  req.body = JSON.generate(inputs: texts)

  response = http.request(req)
  unless response.is_a?(Net::HTTPSuccess)
    raise RagRuby::Error, "HuggingFace API error (#{response.code}): #{response.body}"
  end

  embeddings = JSON.parse(response.body)
  # HF returns [[token_embeddings]] for each text — mean pool if needed
  embeddings.map { |e| e.first.is_a?(Array) ? mean_pool(e) : e }
end