What are embedding models, and how do they convert text to vectors?
Embedding models, convert text to vectors: explain what embedding models are and how they convert text to numeric vectors for semantic search and RAG. Covers tokenization, encoder architectures, training objectives, and a worked cosine similarity example to make the process concrete.

TL;DR
- Embedding models map text to fixed-size numeric vectors that capture semantic similarity.
- They work by tokenizing text, running an encoder, and pooling token outputs into a single vector of dimension .
- Training often uses contrastive or autoregressive objectives so similar texts end up close in vector space. Key tradeoffs: embedding dimension, model size, latency versus retrieval quality.
In this question, we will learn what embedding models are and how they convert text to vectors, so you can explain the pipeline and show a concrete similarity calculation. We will keep the explanation practical for retrieval-augmented generation scenarios.
We will cover the following:
- The intuition
- How it actually works
- Embedding choices and comparison
- Step-by-step example: compute cosine similarity
- Tradeoffs and failure modes
- Questions the interviewer might ask
- What the interviewer is really testing
Direct answer: Embedding models convert text to dense numeric vectors by tokenizing input, encoding tokens with a neural network, and pooling token representations into a fixed-size vector, trained so semantic neighbors are close in vector space. They enable fast similarity search and RAG by turning retrieval into nearest neighbor queries in -dimensional space.
The intuition (an analogy that makes it click)
Think of the embedding space as a town map. Each phrase or document becomes a house on that map. Nearby houses share similar features. When you ask for related text, you look for houses within a short walking distance. The embedding model is the cartographer that draws the map from raw sentences so geography matches meaning.
How it actually works (the real mechanics, with one concrete worked example)
At a high level the pipeline is: tokenize the text, pass tokens through an encoder, then pool token outputs to produce an embedding vector of dimension . Common encoders are transformers or convolutional networks.
Steps in more detail:
- Tokenization: convert text into subword tokens. This maps words into integer ids.
- Encoding: feed token ids into a neural encoder that produces token-level vectors. If the encoder has output states , each is in .
- Pooling: aggregate token vectors into one vector. Common options are the special CLS token, mean pooling, or attention pooling.
- Normalization: often normalize so embeddings lie on the unit sphere.
- Indexing: store vectors in a nearest neighbor index for retrieval.
Training objectives. A popular choice is a contrastive (InfoNCE style) loss where a query vector should be close to a positive key and far from negatives . One form is
where is a temperature.
Worked example. Suppose we encode two short texts and get 3-dimensional vectors. The table shows components.
| vector | component 1 | component 2 | component 3 |
|---|---|---|---|
| 0.10 | 0.30 | 0.90 | |
| 0.00 | 0.40 | 0.80 |
Compute dot product and norms. The dot product is
The norms are
Cosine similarity is
That high similarity means the model placed those texts very close in meaning on the map.
Embedding choices and comparison
There are several axes to choose from when building embeddings for RAG:
- Architecture: transformer encoders are common because they capture context well. Simpler models can be faster but may lose nuance.
- Pooling: CLS can be effective for models trained with it. Mean pooling is robust across architectures.
- Dimension : larger can encode more subtle distinctions but increases index size and nearest neighbor cost.
- Training objective: contrastive training aligns paired examples; supervised classification can also produce useful vectors but may be less robust to open-domain similarity.
Comparison table for quick decisions:
| choice | strength | tradeoff |
|---|---|---|
| large (1024) | finer distinctions | more memory and slower queries |
| small (128) | fast and compact | may conflate meanings |
| contrastive training | aligns semantics for search | needs good positives and negatives |
| supervised classifier | strong for labeled tasks | narrower generalization |
Step-by-step use in RAG
- Build or select an embedding model and decide . 2. Encode all documents and normalize vectors. 3. Add vectors to a nearest neighbor index (HNSW, IVF, etc.). 4. For a query, encode and search top-k by cosine or inner product. 5. Use retrieved documents as context for the generator.
Tradeoffs and failure modes
Embedding models are powerful but not perfect. Common failure modes include:
- Anchoring on surface tokens: models sometimes focus on shared phrases rather than deeper meaning.
- Domain mismatch: embeddings trained on web data may misplace specialized jargon.
- Dimensionality limits: small forces tradeoffs between concepts.
Questions the interviewer might ask
Some follow-up questions you might get:
How does tokenization affect embeddings? Tokenization changes which subword units the encoder sees. Different tokenizers shift how semantic chunks are represented and can change embeddings, especially for rare or compound words.
Why normalize embeddings before indexing? Normalization makes cosine similarity equal to dot product which simplifies indexes and stabilizes similarity scores across lengths.
What is a good way to select negatives for contrastive training? Hard negatives that are similar but not correct help learning. Random negatives work for scale but may be less effective than mined hard negatives.
When should we use cross-encoder re-ranking? Use a cross-encoder when you need higher precision on a small candidate set. It is slower because it scores pairs instead of computing a single vector for each document.
How does embedding dimension influence index choice? Higher increases vector storage and search cost. Some indexes like product quantization handle large well, while others accelerate lower-dimensional searches.
How do you evaluate embedding quality? Use retrieval metrics like recall@k, MRR, or downstream task metrics in RAG. Also inspect nearest neighbors qualitatively.
Some things to note:
- Always match preprocessing and normalization between query and indexed vectors.
- Evaluate on domain-specific data to detect mismatch.
What the interviewer is really testing
They want to see you understand the full pipeline from raw text to searchable vectors: tokenization, encoding, pooling, training objectives, and indexing. They also want practical judgment about tradeoffs such as dimension, model cost, and whether to re-rank with a cross-encoder. Clear examples and a simple cosine similarity calculation show you can connect theory to implementation.
Further reading in the curriculum
Go deeper on the fundamentals behind this question.
- RAG Fundamentals Why retrieval-augmented generation works, and how to build a pipeline that actually grounds answers.
Related questions
How would you rate the quality of this article?
Prepare for your AI engineering interview
This is one of many detailed questions and explainers on StudyAIDesign. Browse the full set, work through the curriculum, and walk into your interview ready.