Sub topic 6
Turning Text into Embeddings
This chapter assumes you know basic Python, can read vectors and the dot product, and have a rough idea of how chunks are produced (covered in the previous chapter). It explains what an embedding is, how a RAG system uses one, and the errors that most often break retrieval.
What an embedding is
An embedding is a fixed-length list of numbers that represents a piece of text. An embedding model reads a chunk and returns a vector, for example 768 or 1536 numbers. Texts with similar meaning get vectors that point in similar directions, even when they share no words. "How do I reset my password?" and "Steps to recover account access" end up close together.
Embeddings are what let RAG search by meaning instead of exact words. Every chunk is embedded once at ingestion time. The query is embedded at question time. Retrieval then compares the query vector to the stored chunk vectors.
Calling an embedding model
Most providers expose embeddings through an HTTP API that takes a list of strings and returns a list of vectors. The open-source sentence-transformers library does the same locally. This example uses it.
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")
chunks = [
"To reset your password, open Settings and choose Security.",
"Invoices are emailed on the first day of each month.",
]
vectors = model.encode(chunks, normalize_embeddings=True)
query_vec = model.encode(
"How do I recover my account?", normalize_embeddings=True
)
print(vectors.shape) # (2, 384)
scores = vectors @ query_vec
print(scores) # the password chunk scores higherThe model returns 384 numbers per text. Because normalize_embeddings=True scales each vector to length 1, the dot product equals cosine similarity. That makes comparison fast and the scores easy to read.
Rules that prevent common errors
- Use one model for both sides. Vectors from different models live in different spaces. Comparing them gives meaningless scores. If you change models, re-embed the whole corpus.
- Record the model name and dimension. Store them with the index. A dimension mismatch raises an error. A same-dimension model swap fails silently.
- Respect the input limit. Each model accepts a maximum number of tokens. Longer text is usually cut off without warning, so the tail of a large chunk is never embedded. Keep chunks below the limit.
- Check for query prefixes. Some models expect a prefix such as
query:orpassage:. Skipping it lowers quality. Read the model card. - Batch your calls. Send many chunks per request. One call per chunk is slow and may hit rate limits.
Choosing a model
| Factor | What to check |
|---|---|
| Quality | Retrieval scores on a benchmark close to your domain, then your own test set |
| Dimension | Higher dimensions cost more storage and search time |
| Input limit | Must fit your chunk size in tokens |
| Language | Multilingual corpora need a multilingual model |
| Cost and hosting | API price per token versus running a model yourself |
Benchmark rankings are a starting point. Domain vocabulary such as legal or medical terms can change which model wins, so test on your own questions.
Exercise
You switch to a new embedding model with the same dimension as the old one, but you only embed new documents with it and keep the old vectors. Retrieval quality drops with no errors. Why, and what is the fix?
Show answer
The old and new vectors come from different models, so they sit in different vector spaces, and similarity scores between them are meaningless. The matching dimension hides the problem because no error is raised. Re-embed the entire corpus with the new model, and embed queries with the same model.