RAG & LLMs / 3. EMBEDDING (BI-ENCODER)
Stage 3: Embedding — The Bi-Encoder
Converting text into dense vectors using BERT encoder-only models
EXPLANATION
Embedding models convert text into high-dimensional vectors where semantic similarity equals geometric proximity. These are BERT-family encoder-only transformer models. When you call embed_query(), the text is tokenized, run through all BERT layers with bidirectional self-attention, then mean-pooled into a single dense vector. Popular choices: • BAAI/bge-large-en-v1.5 → best open source, 1024 dims • all-MiniLM-L6-v2 → fast, 384 dims, great for dev • text-embedding-3-small → OpenAI (API) • models/text-embedding-004 → Google Gemini (API)
DATA FLOW
"What is machine learning?"
↓
Tokenize: [CLS] What is machine learning ? [SEP]
↓
BERT Encoder (12 layers of self-attention)
All tokens attend to ALL other tokens ← bidirectional
↓
Mean pooling over all token vectors
↓
[0.23, -0.87, 0.45, 0.12, ...] ← 1024-dim vector
↓
Stored in ChromaDBCODE