huggingface /sentence-transformers
State-of-the-Art Embeddings, Retrieval, and Reranking
★ 19,285⑂ 3,024👁 147⑃ 1,473● 2,615▤ 74
+0
日增长
19,285
stars
3,024
forks
147
watchers
1,473
prs
2,615
issues
74
releases
README
![GitHub - License][github-license]
![PyPI - Python Version][pypi-package]
![PyPI - Package Version][pypi-package]
![Docs - GitHub.io][docs-package]
Sentence Transformers: Embeddings, Retrieval, and Reranking
This framework provides an easy method to compute embeddings for accessing, using, and training state-of-the-art embedding and reranker models. It can be used to compute embeddings using Sentence Transformer models (quickstart), to calculate similarity scores using Cross-Encoder (a.k.a. reranker) models (quickstart), to generate sparse embeddings using Sparse Encoder models (quickstart) or to compute token-level embeddings for ColBERT-style late-interaction retrieval using Multi-Vector Encoder models (quickstart). This unlocks a wide range of applications, including semantic search, semantic textual similarity, and paraphrase mining.
A wide selection of over 15,000 pre-trained Sentence Transformers models are available for immediate use on 🤗 Hugging Face, including many of the state-of-the-art models from the Massive Text Embeddings Benchmark (MTEB) leaderboard. Additionally, it is easy to train or finetune your own embedding models, reranker models, sparse encoder models or multi-vector encoder models using Sentence Transformers, enabling you to create custom models for your specific use cases.
For the full documentation, see www.SBERT.net.
Installation
We recommend Python 3.10+, PyTorch 2.2+, and transformers v5.0+.
pip install -U sentence-transformers
See Installation in the docs for uv, conda, source, and editable installs, CUDA setup, and extras ([image], [audio], [video], [train], [onnx], [openvino], [dev]).
Getting Started
See Quickstart in our documentation.
Log in with hf auth login for higher Hugging Face Hub rate limits and access to private or gated models your account can read. See the login instructions.
Embedding Models
First download a pretrained embedding a.k.a. Sentence Transformer model.
python
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
Then provide some texts to the model.
python
sentences = [
"The weather is lovely today.",
"It's so sunny outside!",
"He drove to the stadium.",
]
embeddings = model.encode(sentences)
print(embeddings.shape)
= (3, 384)
And that's already it. We now have numpy arrays with the embeddings, one for each text. We can use these to compute similarities.
python
similarities = model.similarity(embeddings, embeddings)
print(similarities)
tensor([[1.0000, 0.6660, 0.1046],
[0.6660, 1.0000, 0.1411],
[0.1046, 0.1411, 1.0000]])
Reranker Models
First download a pretrained reranker a.k.a. Cross Encoder model.
python
from sentence_transformers import CrossEncoder
1. Load a pretrained CrossEncoder model
model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L6-v2")
Then provide some texts to the model.
python
The texts for which to predict similarity scores
quTop issues
#695Multilingual Information Retrieval (MS-Marco Bi-Encoders)62 comments · 2021-01-15#1011EN-DE MS-Marco43 comments · 2021-06-17#350Fine Tuning Models34 comments · 2020-08-11#1795Memory leak in SentenceTransformer.encode during the first ~10000 predictions32 comments · 2022-12-23#2922Please future prove `clean_up_tokenization_spaces` 28 comments · 2024-09-03#617Different results each model run27 comments · 2020-12-11#3072In Batch Negatives26 comments · 2024-11-19#671Require some neutral network construction guide to use Asym26 comments · 2021-01-07
Latest release
v6.3.0 - Token-pooling-aware training and `kernels` support v6.3.0 2026-11-11Train multi-vector models for smaller document representations with token-pooling-aware and Matryoshka training. This release also adds optional kernels for MaxSim, Ward clustering, and transformer backbones, native AWS Neuron inference, faster utilities, and fixes across encoding, training, and evaluation. [!TIP] I share much more about training multi-vector models with token-pooling-aware losses in my new Token Pooling-Aware Training for Multi-Vector Embedding Models blogpost. Give it a read! Install with: bash Inference only, use one of: pip install "sentence-transformers==6.