Skip to the page
Tecyfy

What is SentenceTransformers?

The open-source Python library most people use to turn text into embeddings on their own machine, with thousands of ready models and a few lines of code.

beginner · Also called Sentence Transformers, SBERT

If you want embeddingsEmbeddingA list of numbers that stands for the meaning of a piece of text, an image or other data. Things with similar meaning get similar numbers, so a computer can find them. without paying an API for every text, SentenceTransformers is usually where you start. It is a Python library that loads an embedding model, runs your texts through it and hands back vectors, all on your own laptop or server.

Where it came from

In 2019 Nils Reimers and Iryna Gurevych, at the Technical University of Darmstadt, pointed out a problem with BERT. To find the most similar pair among 10,000 sentences, BERT had to compare every pair directly, about 50 million comparisons, roughly 65 hours. Their fix, Sentence-BERT, trained the model to give each sentence its own vector, so that similar sentences get nearby vectors. Then you embed each sentence once and compare vectors with cosine similarityCosine SimilarityA measure of how closely 2 vectors point the same way, from 1 for the same direction to −1 for opposite; with embeddings, a higher score means closer in meaning.. The same job took about 5 seconds.

The library they released with it grew into SentenceTransformers, now maintained at Hugging Face, which hosts thousands of compatible models.

What using it looks like

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
docs = ["How do I reset my password?", "Our office opens at 9."]
query = "I can't sign in"

doc_vectors = model.encode(docs)
query_vector = model.encode([query])
print(model.similarity(query_vector, doc_vectors))

The model named here is small and fast and gives 384 numbers per text. The password question will score higher than the office hours, though it shares no words with "I can't sign in". That is the whole idea of vector searchVector SearchFinding the stored items whose embeddings are closest to a query's embedding, so a search matches meaning rather than exact words..

What else it does

  • Cross-encoders. Instead of embedding texts separately, a cross-encoder reads the query and one document together and scores how well they match. It is slower but more accurate, so it is used for rerankingRerankingA second search step that reads each candidate passage together with the question and scores how well it answers, so the best few of a fast first search go to the model in the right order. the top results that vector search found.
  • Training your own. It can fine-tune an embedding model on pairs of texts from your own domain, which often helps more than switching to a bigger model.
  • Images and text. Some models it loads embed pictures and text into the same space, so a text query can find an image.

When to use it, and when an API is better

Use it when you have many texts to embed and want no per-call cost, when data cannot leave your servers, or when you want to fine-tune. A hosted embedding API is simpler when volumes are small, when you want the strongest models without running a GPU, or when you would rather not maintain the setup. The embedding cost calculator shows what an API would charge for your volume.

Sources

Where each fact comes from

  1. 1Sentence-BERT, sentence embeddings using Siamese BERT-networks
  2. 2Quickstart
  3. 3all-MiniLM-L6-v2