liveBook · Manning

Sentence-transformers

As explained in Domain-Specific Small Language Models

A library for producing dense text vectors with transformer encoders for embedding and similarity search.

sentence-transformers is a Python open-source library for producing dense vector representations of sentences, paragraphs, and images with encoder-only Transformer models. Similar texts occupy nearby positions in the resulting vector space, enabling semantic retrieval and similarity comparison. section 13.2 section 2.1.3

In retrieval-augmented generation, embed document chunks and user queries with the same model, then rank candidate passages using cosine similarity or another distance metric. The embeddings can be persisted for later retrieval. section 13.2 section 13.5

Operational choices shape retrieval quality and deployment cost:

  • Choose an embedding model such as all-mpnet-base-v2, which maps text into a 768-dimensional vector space and is fine-tuned with contrastive learning. section 13.2
  • Run encoding on CPU when hardware acceleration is unavailable, or use compatible GPU infrastructure when the surrounding search workflow requires it. section 13.2 section 2.1.3
  • Pair generated vectors with a similarity index such as FAISS for efficient nearest-neighbor retrieval instead of brute-force comparison. section 2.1.3

Retrieve relevant document chunks

Load SentenceTransformer("all-mpnet-base-v2"), encode preprocessed sentence chunks, encode the query "blocked KV-cache", calculate cosine similarities, and select the top five results. A related classification workflow encodes superhero descriptions, normalizes vectors, and searches a FAISS L2 index for "He throws webs". section 13.2 section 2.1.3

How Domain-Specific Small Language Models covers it

How deep it goes
Explains encoding, vector-space similarity, model selection, and retrieval integration, but defers detailed model architecture and training procedures.
Examples
Demonstrates document-chunk retrieval for RAG and superhero-text classification with a normalized FAISS index.
What it assumes
Assumes Python data-processing familiarity while teaching document chunking, embedding generation, normalization, and similarity retrieval within the workflow.
What you can do after
Apply it via Python APIs to encode text, compare query and document vectors, and feed retrieved passages into a RAG pipeline.
How it connects
Connects sentence-transformers with RAG, dense vector embeddings, Transformer architecture, cosine similarity, FAISS, and semantic search.

Also explained in

Compare how 2 books explain sentence-transformers →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Domain-Specific Small Language Models. The section links point to the parts of the book it draws on.