Class: Langchain::LLM::HuggingFace
- 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
Instance Method Summary collapse
-
#default_dimensions ⇒ Integer
Returns the # of vector dimensions for the embeddings.
-
#embed(text:) ⇒ Langchain::LLM::HuggingFaceResponse
Generate an embedding for a given text.
-
#initialize(api_key:, default_options: {}) ⇒ HuggingFace
constructor
Intialize the HuggingFace LLM.
Methods inherited from Base
#chat, #chat_parameters, #complete, #default_dimension, #summarize
Methods included from DependencyHelper
Constructor Details
#initialize(api_key:, default_options: {}) ⇒ HuggingFace
Intialize the HuggingFace LLM
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() end |
Instance Method Details
#default_dimensions ⇒ Integer
Returns the # of vector dimensions for the embeddings
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 (text: "test")..size end end |
#embed(text:) ⇒ Langchain::LLM::HuggingFaceResponse
Generate an embedding for a given text
50 51 52 53 54 55 56 |
# File 'lib/langchain/llm/hugging_face.rb', line 50 def (text:) response = client.( input: text, model: @defaults[:embedding_model] ) Langchain::LLM::HuggingFaceResponse.new(response, model: @defaults[:embedding_model]) end |