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

- Page: https://tecyfy.com/concepts/sentencetransformers
- Subject: Data engineering in the AI era (https://tecyfy.com/concepts)
- Level: beginner
- Also called: Sentence Transformers, SBERT

If you want [embeddings](https://tecyfy.com/concepts/embedding) 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 similarity](https://tecyfy.com/concepts/cosine-similarity). 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

```python
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 search](https://tecyfy.com/concepts/vector-search).

## 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 [reranking](https://tecyfy.com/concepts/reranking) 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](/prices/embedding-cost) shows what an API would charge for your volume.

## Related

- [Embedding](https://tecyfy.com/concepts/embedding): A 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.
- [Transformer](https://tecyfy.com/concepts/transformer): The design behind almost every modern language model. It uses attention to let each word in a passage look at every other word, all at once.
- [Sentence Embedding](https://tecyfy.com/concepts/sentence-embedding): One list of numbers for the meaning of a whole sentence or passage, made so that texts which mean the same thing get lists close together. It is what semantic search compares.
- [Vector Search](https://tecyfy.com/concepts/vector-search): Finding the stored items whose embeddings are closest to a query's embedding, so a search matches meaning rather than exact words.
- [Reranking](https://tecyfy.com/concepts/reranking): A 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.
- [Cosine Similarity](https://tecyfy.com/concepts/cosine-similarity): A 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.

## Sources

- [Sentence-BERT, sentence embeddings using Siamese BERT-networks](https://arxiv.org/abs/1908.10084)
- [Quickstart](https://sbert.net/docs/quickstart.html)
- [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)
