Embedding Models in LangChain
Interchangeable but not equivalent. Choosing the right engine.
1. The Abstraction Layer
LangChain abstracts embedding providers into a single class: Embeddings.
Method: embed_query(text) -> List[float].
This means you can swap OpenAI for HuggingFace with one line of code:
from langchain_openai import OpenAIEmbeddings
from langchain_huggingface import HuggingFaceEmbeddings
# model = OpenAIEmbeddings()
model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")2. Model Selection Trade-offs
- OpenAI (
text-embedding-3-small): High quality, cheap, but sends data to API (Privacy risk). - Local (
all-MiniLM-L6-v2): Free, private, runs on CPU, but lower semantic understanding. - Cohere (
embed-english-v3.0): Specialized for RAG (retrieval quality).
3. The Silent Failure: Mixing Models
CRITICAL WARNING: If you embed your documents with Model A, and embed your user query with Model B... It will not work. The vectors live in different mathematical universes. You will get random results. Once you pick an embedding model for an index, you are married to it.
4. Summary
LangChain makes it easy to experiment. But make a choice early. Changing models later requires re-embedding your entire database ($$$).
Key Intuition: "Vectors are not portable. They are model-specific signatures."