liveBook · Manning

Sentence-transformers

As explained in Build an Advanced RAG Application (From Scratch)

Sentence Transformers is a software library and model framework for generating semantically meaningful vector representations of sentences and text.

sentence-transformers generates dense vector representations for sentences, paragraphs, and reviews. In semantic search, those embeddings represent meaning rather than exact words, so a query can retrieve related text without sharing its surface phrasing. A query such as “furry animal” can therefore find text about dogs or cats. section 3.4 section 3.5 section 4.0

The library packages pre-trained encoder models for sentence-level embedding. Rather than comparing every query-document pair with a computationally expensive cross-encoder, it produces one fixed-size vector per sentence that can be stored, indexed, and compared later. This makes precomputation and fast retrieval practical; Bi-Encoders are preferred when efficiency and compute resources matter. section 4.2 section 4.6

Encode text with all-MiniLM-L6-v2

Install sentence-transformers, load SentenceTransformer("all-MiniLM-L6-v2"), then call encode on a list of strings. For six sample sentences, encoding returns an array shaped (6, 384): one 384-dimensional vector for each input. The displayed model combines a BERT transformer, mean-token pooling, and normalization. section 3.4 section 4.2

Listing 4.1 Generate Embeddings on sample data
from sentence_transformers import SentenceTransformer #A
 
model = SentenceTransformer(“all-MiniLM-L6-v2”) #B
 
sentences = ["This is an example sentence.", "Another example sentence."]
 
embeddings = model.encode(sentences) #C
 
print(len(embeddings)) #D

This minimal runnable listing shows the essential workflow: import SentenceTransformer, load a pretrained model, encode sentences, and inspect the number of generated embeddings. section 4.2

To search, encode the query with the same model and compare its vector with stored document vectors. Cosine similarity measures the angle between vectors; higher similarity indicates closer semantic relation. In a toy corpus, “Machine learning” ranks “Machine learning is based on algorithms” above the other examples. At larger scale, embeddings can be indexed with FAISS for efficient similarity search. section 3.4 section 4.6

A hotel-search pipeline applies the same pattern to review text: extract reviews from a DataFrame, optionally move the model to a CUDA GPU, encode all reviews with a progress bar, and retain the resulting vectors for ranking against free-form hotel preferences. Model selection remains a deployment choice: available encoders differ by use case, dataset size, and open- versus closed-source requirements. section 4.2.1 section 4.6

How Build an Advanced RAG Application (From Scratch) covers it

How deep it goes
Walks through installation, model loading, encoding, cosine ranking, CUDA placement, and scalable FAISS retrieval without detailing encoder internals.
Examples
Uses six toy sentences, a “Machine learning” query, and Paris hotel reviews for a Travelle hotel-search engine.
What it assumes
Starts with pip installation and Python imports while using Pandas DataFrames and optional CUDA checks in the implementation.
What you can do after
Apply it via SentenceTransformer to encode text, precompute review vectors, and rank query matches with similarity metrics.
How it connects
Connects semantic search with embeddings, cosine similarity, Bi-Encoders, Cross-Encoders, FAISS, BERT, SBERT, and CUDA.

Also explained in

Compare how 4 books explain sentence-transformers →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an Advanced RAG Application (From Scratch). The section links point to the parts of the book it draws on.