Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

sentence-transformers

> 编程语言
Open source

State-of-the-Art Embeddings, Retrieval, and Reranking

19.0K stars0 likes1 views
WebsiteGitHub

About

State-of-the-Art Embeddings, Retrieval, and Reranking

[][#github-license] [][#pypi-package] [][#pypi-package] [][#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+.

bash
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.

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.

…

And we're good to go. You can also use model.rank to avoid having to perform the reranking manually:

…

Sparse Encoder Models

First download a pretrained sparse embedding a.k.a. Sparse Encoder model.

…

Multi-Vector Encoder Models

First download a pretrained multi-vector a.k.a. late-interaction (ColBERT-style) model.

…

Pre-Trained Models

We provide a large list of pretrained models for more than 100 languages. Some models are general purpose models, while others produce embeddings for specific use cases.

  • Pretrained Sentence Transformer (Embedding) Models
  • Pretrained Cross Encoder (Reranker) Models
  • Pretrained Sparse Encoder (Sparse Embeddings) Models
  • Pretrained Multi-Vector Encoder (Late Interaction) Models

Training

Tip: Using an AI coding agent (Claude Code, Codex, Cursor, Gemini CLI, ...)? Install the train-sentence-transformers Hugging Face Agent Skill via hf skills add train-sentence-transformers [--claude] [--global] and ask your agent to fine-tune a model on your data.

This framework allows you to fine-tune your own sentence embedding methods, so that you get task-specific sentence embeddings. You have various options to choose from in order to get perfect sentence embeddings for your specific task.

  • Embedding Models
    • Sentence Transformer > Training Overview
    • Sentence Transformer > Training Examples or training examples on GitHub.
  • Reranker Models
    • Cross Encoder > Training Overview
    • Cross Encoder > Training Examples or training examples on GitHub.
  • Sparse Embedding Models
    • Sparse Encoder > Training Overview
    • Sparse Encoder > Training Examples or training examples on GitHub.
  • Multi-Vector (Late Interaction) Models
    • Multi-Vector Encoder > Training Overview
    • Training examples on GitHub.

Some highlights across the different types of training are:

  • Support of various transformer networks including BERT, RoBERTa, XLM-R, DistilBERT, Electra, BART, ...
  • Multilingual and multi-task learning
  • Evaluation during training to find optimal model
  • 20+ loss functions for embedding models, 10+ loss functions for reranker models and 10+ loss functions for sparse embedding models, allowing you to tune models specifically for semantic search, paraphrase mining, semantic similarity comparison, clustering, triplet loss, contrastive loss, etc.

Companion Blog Posts

The following Hugging Face blog posts complement this documentation with narrative walkthroughs and full training examples:

Training guides:

  • Training and Finetuning Embedding Models: end-to-end training of bi-encoder embedding models.
  • Training and Finetuning Reranker Models: training Cross Encoder models for the second stage of retrieve-and-rerank pipelines.
  • Training and Finetuning Sparse Embedding Models: training SPLADE and other sparse encoders.

Multimodal:

  • Multimodal Embedding & Reranker Models: using text, image, audio, and video models through a single API.
  • Training and Finetuning Multimodal Embedding & Reranker Models: training multimodal models, with a Visual Document Retrieval walkthrough.

Efficiency techniques:

  • Introduction to Matryoshka Embedding Models: variable-size embeddings that can be truncated with minimal quality loss.
  • Train 400x faster Static Embedding Models: CPU-friendly embedding models without attention.
  • Binary and Scalar Embedding Quantization for Significantly Faster & Cheaper Retrieval: post-training compression of embedding vectors.

Application Examples

You can use this framework for:

  • Computing Sentence Embeddings

    • Dense Embeddings
    • Sparse Embeddings
  • Semantic Textual Similarity

    • Dense STS
    • Sparse STS
  • Semantic Search

    • Dense Search
    • Sparse Search
  • Retrieve & Re-Rank

    • Dense only Retrieval
    • Sparse/Dense/Hybrid Retrieval
  • Clustering

  • Paraphrase Mining

  • Translated Sentence Mining

  • Multilingual Image Search, Clustering & Duplicate Detection

and many more use-cases.

For all examples, see examples/sentence_transformer/applications.

Development setup

After cloning the repo (or a fork) to your machine, in a virtual environment, run:

python -m pip install -e ".[dev]"

pre-commit install

To test your changes, run:

pytest

Citing & Authors

If you find this repository helpful, feel free to cite our publication Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks:

bibtex
@inproceedings{reimers-2019-sentence-bert,
    title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
    author = "Reimers, Nils and Gurevych, Iryna",
    booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
    month = "11",
    year = "2019",
    publisher = "Association for Computational Linguistics",
    url = "https://arxiv.

Issues· 1356 open

View all issuesOpen on GitHub
  • #4107

    StaticEmbedding ignores model2vec `mapping`/`weights` tensors, so e.g. `minishlab/potion-code-16M` produces wrong embeddings

    Updated Oct 1, 2026
  • #4105

    weightedmean pooling produces NaNs for 512-token float16 inputs

    Updated Oct 1, 2026
  • #4103

    Weighted mean pooling changes with left padding

    Updated Oct 1, 2026
  • #4068

    compute_count_vector counts explicit sparse zeros as nonzero values

    Updated Sep 30, 2026
  • #4097

    Similarity utilities reject dense tensor lists returned by encode

    Updated Sep 30, 2026
  • #4090

    encode_document() never uses a "passage" or "corpus" prompt

    Updated Sep 29, 2026
  • #3991

    Problems in training multi-modal models with large batch sizes

    Updated Sep 27, 2026
  • #4081

    [Bug] GradCache replay downcasts FP64 embeddings to FP32 in the surrogate dot product

    Updated Sep 26, 2026
  • #4010

    model.safetensors is missing read permissions after saving with SentenceTransformer.save()

    Updated Sep 25, 2026
  • #4070

    `AttributeError: 'str' object has no attribute 'to'` in CachedGISTEmbedLoss

    Updated Sep 25, 2026

> Tags

Python

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言