Hugging Face Embeddings with Sentence Transformers
Embeddings with Hugging Face turn a piece of text into a list of numbers (a vector) that captures its meaning. Texts with similar meaning get vectors that point in similar directions, even when they share no words. Embeddings power semantic search, recommendation, clustering and the retrieval step of RAG (retrieval-augmented generation).
Installing sentence-transformers
The sentence-transformers library wraps Hugging Face models with pooling and normalization so you get one vector per sentence.
pip install sentence-transformersSuccessfully installed sentence-transformers-6.x.x
Your First Embeddings
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
sentences = ["The cat sits on the mat.", "A kitten is resting on a rug.", "Stock prices fell today."]
emb = model.encode(sentences)
print(emb.shape)(3, 384)
all-MiniLM-L6-v2 is small and fast and produces 384 numbers per sentence. encode returns a NumPy array with one row per input.
Measuring Similarity
Cosine similarity compares the direction of two vectors: 1 means the same meaning, around 0 means unrelated.
sims = model.similarity(emb, emb)
print(sims)tensor([[1.0000, 0.6105, 0.0291],
[0.6105, 1.0000, 0.0127],
[0.0291, 0.0127, 1.0000]])
(sample values)The two cat sentences score about 0.61 even though they use different words, while the stock sentence is near zero. That is the power of semantic similarity.
Building a Semantic Search
docs = [
"How to reset your password",
"Shipping times for international orders",
"Refund policy for damaged items",
"Changing the email on your account",
]
doc_emb = model.encode(docs)
query = "my parcel is taking forever to arrive abroad"
q_emb = model.encode([query])
scores = model.similarity(q_emb, doc_emb)[0]
best = scores.argsort(descending=True)[:2]
for i in best:
print(round(float(scores[i]), 3), docs[i])0.512 Shipping times for international orders 0.143 Refund policy for damaged items (sample values)
For a few thousand documents, keeping the vectors in memory is fine. For millions, store them in a vector database such as FAISS, pgvector, Qdrant or Chroma.
Choosing an Embedding Model
- Check the MTEB leaderboard on the Hub to compare models on retrieval tasks.
- Smaller models (MiniLM, bge-small) are fast and good enough for many apps.
- Multilingual models are needed if queries and documents use different languages.
- Some models expect prefixes like "query: " and "passage: " — read the model card.
- Always embed documents and queries with the same model.
Frequently asked questions
What are embeddings in Hugging Face?
Fixed-length numeric vectors produced by a model that represent the meaning of text, images or audio, so you can compare items mathematically.
What is the difference between sentence-transformers and transformers?
sentence-transformers is built on top of transformers. It adds pooling, normalization, similarity helpers and training utilities specifically for producing sentence embeddings.
Which embedding model should I use?
Start with a small, popular model such as all-MiniLM-L6-v2 or a bge-small model, then compare candidates on the MTEB leaderboard and on your own data.