Files
langchain/docs/extras/integrations/text_embedding/huggingfacehub.ipynb
T
JaéGeRandBagatur b8669b249e Added Hugging face inference api (#10280)
Embed documents without locally downloading the HF model


---------

Co-authored-by: Bagatur <baskaryan@gmail.com>
2023-09-06 14:55:48 -07:00

3.8 KiB

Hugging Face

Let's load the Hugging Face Embedding class.

In [ ]:
!pip install langchain sentence_transformers
In [2]:
from langchain.embeddings import HuggingFaceEmbeddings
In [3]:
embeddings = HuggingFaceEmbeddings()
In [3]:
text = "This is a test document."
In [5]:
query_result = embeddings.embed_query(text)
In [6]:
query_result[:3]
Out [6]:
[-0.04895168915390968, -0.03986193612217903, -0.021562768146395683]
In [7]:
doc_result = embeddings.embed_documents([text])

Hugging Face Inference API

We can also access embedding models via the Hugging Face Inference API, which does not require us to install sentence_transformers and download models locally.

In [1]:
import getpass

inference_api_key = getpass.getpass("Enter your HF Inference API Key:\n\n")
Enter your HF Inference API Key:

 ········
In [4]:
from langchain.embeddings import HuggingFaceInferenceAPIEmbeddings

embeddings = HuggingFaceInferenceAPIEmbeddings(
    api_key=inference_api_key,
    model_name="sentence-transformers/all-MiniLM-l6-v2"
)

query_result = embeddings.embed_query(text)
query_result[:3]
Out [4]:
[-0.038338541984558105, 0.1234646737575531, -0.028642963618040085]