How to use HuggingFace embeddings in LangChain
Quick answer
Use the
HuggingFaceEmbeddings class from langchain.embeddings to load HuggingFace models as embeddings in LangChain. Initialize it with the model name, then generate embeddings for your text to use with vector stores like FAISS.PREREQUISITES
Python 3.8+pip install langchain huggingface_hub faiss-cpuHuggingFace API token (optional for some models)
Setup
Install the required packages and set up environment variables if you plan to use private HuggingFace models.
pip install langchain huggingface_hub faiss-cpu Step by step
This example shows how to load a HuggingFace embedding model in LangChain, generate embeddings for sample texts, and store them in a FAISS vector store.
from langchain_openai import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
# Initialize HuggingFace embeddings with a model
embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
# Sample texts to embed
texts = ["Hello world", "LangChain with HuggingFace embeddings", "Vector search example"]
# Generate embeddings
embeddings = [embedding_model.embed_query(text) for text in texts]
# Create FAISS vector store from texts and embeddings
vector_store = FAISS.from_texts(texts, embedding_model)
# Query vector store
query = "How to use embeddings?"
query_embedding = embedding_model.embed_query(query)
# Search top 2 similar texts
results = vector_store.similarity_search_by_vector(query_embedding, k=2)
for i, doc in enumerate(results, 1):
print(f"Result {i}: {doc.page_content}") output
Result 1: LangChain with HuggingFace embeddings Result 2: Hello world
Common variations
- Use different HuggingFace models by changing
model_name, e.g.,"sentence-transformers/all-mpnet-base-v2". - Use async embedding calls if supported by the model wrapper.
- Integrate with other vector stores like
ChromaorPinecone.
Troubleshooting
- If you get authentication errors, ensure your HuggingFace API token is set in
HUGGINGFACEHUB_API_TOKENenvironment variable. - For slow embedding generation, check model size and consider smaller models.
- If embeddings are empty or errors occur, verify model compatibility with LangChain's
HuggingFaceEmbeddingsclass.
Key Takeaways
- Use
HuggingFaceEmbeddingsin LangChain to leverage HuggingFace models for embeddings. - Initialize with the model name and generate embeddings for texts to use with vector stores like
FAISS. - Set
HUGGINGFACEHUB_API_TOKENenvironment variable if accessing private or gated models. - You can switch models easily by changing the
model_nameparameter. - Troubleshoot by verifying token setup and model compatibility.