Class: Langchain::LLM::HuggingFace

Inherits:
Base
  • Object
show all
Defined in:
lib/langchain/llm/hugging_face.rb

Overview

Wrapper around the HuggingFace Inference API: huggingface.co/inference-api

Gem requirements:

gem "hugging-face", "~> 0.3.4"

Usage:

llm = Langchain::LLM::HuggingFace.new(api_key: ENV["HUGGING_FACE_API_KEY"])

Constant Summary collapse

DEFAULTS =
{
  embedding_model: "sentence-transformers/all-MiniLM-L6-v2"
}.freeze
EMBEDDING_SIZES =
{
  "sentence-transformers/all-MiniLM-L6-v2": 384
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#client, #defaults

Instance Method Summary collapse

Methods inherited from Base

#chat, #chat_parameters, #complete, #default_dimension, #summarize

Methods included from DependencyHelper

#depends_on

Constructor Details

#initialize(api_key:, default_options: {}) ⇒ HuggingFace

Intialize the HuggingFace LLM

Parameters:

  • api_key (String)

    The API key to use



27
28
29
30
31
32
# File 'lib/langchain/llm/hugging_face.rb', line 27

def initialize(api_key:, default_options: {})
  depends_on "hugging-face", req: "hugging_face"

  @client = ::HuggingFace::InferenceApi.new(api_token: api_key)
  @defaults = DEFAULTS.merge(default_options)
end

Instance Method Details

#default_dimensionsInteger

Returns the # of vector dimensions for the embeddings

Returns:

  • (Integer)

    The # of vector dimensions



36
37
38
39
40
41
42
# File 'lib/langchain/llm/hugging_face.rb', line 36

def default_dimensions
  # since Huggin Face can run multiple models, look it up or generate an embedding and return the size
  @default_dimensions ||= @defaults[:dimensions] ||
    EMBEDDING_SIZES.fetch(@defaults[:embedding_model].to_sym) do
      embed(text: "test").embedding.size
    end
end

#embed(text:) ⇒ Langchain::LLM::HuggingFaceResponse

Generate an embedding for a given text

Parameters:

  • text (String)

    The text to embed

Returns:



50
51
52
53
54
55
56
# File 'lib/langchain/llm/hugging_face.rb', line 50

def embed(text:)
  response = client.embedding(
    input: text,
    model: @defaults[:embedding_model]
  )
  Langchain::LLM::HuggingFaceResponse.new(response, model: @defaults[:embedding_model])
end