Embeddings and RAG — how to feed a model your own, up-to-date knowledge instead of relying only on what it learned in training.
This is the multi-page printable view of this section. Click here to print.
Ground it in your data
1 - Embeddings
An embedding turns text (or an image, or audio) into a list of numbers — a vector — that captures its meaning. It’s the mechanism behind RAG, but it’s useful far beyond that.
The core idea
Text with similar meaning maps to nearby vectors; unrelated text maps far apart. “cat” and “kitten” land close together; “car” lands elsewhere. That’s the whole trick — meaning becomes distance you can measure.
flowchart LR
Txt[Text] --> M[Embedding model]
M --> Vec[Vector of numbers]
Vec --> Space[Vector space - similar meaning is nearby]How you use them
- Use the same embedding model for everything you compare — vectors from different models aren’t comparable.
- Measure closeness with cosine similarity (angle between vectors).
- Store and search them in a vector database (see RAG).
Uses beyond RAG
- Semantic search — find by meaning, not keywords.
- Clustering — group similar items automatically.
- Classification — label text by nearest known examples.
- Deduplication — spot near-duplicate content.
- Recommendations — “more like this”.
Practical notes
- Dimensions — vectors have a fixed length (e.g. hundreds to thousands of numbers); more isn’t always better.
- Model choice matters — pick one suited to your language and domain.
- Embeddings are cheap compared to generation; embedding a large corpus is routine.
Strengths & limitations
- Strengths — turns meaning into measurable distance; cheap and reusable; powers search, clustering, and classification.
- Limitations — quality depends on the model and domain; fixed at embed time (re-embed when data changes); misses exact keywords — pair with keyword / hybrid search.
Sources
- Reimers & Gurevych, Sentence-BERT (2019) — arXiv:1908.10084
- OpenAI — Embeddings
2 - RAG (Retrieval-Augmented Generation)
Goal
The goal of RAG is to retrieve relevant information from an external data source and add it to the prompt before the foundation model generates an answer. This lets the model answer from your organization’s data instead of relying only on knowledge learned during training.
Basic pipeline
Collect documents → parse → chunk → embed → store in a vector store → retrieve → augment → generate.
flowchart LR
D[Documents] --> P[Parse]
P --> C[Chunk]
C --> E[Embed]
E --> V[(Vector store)]
Q[User question] --> R[Retrieve]
V --> R
R --> A[Augment prompt]
A --> M[Foundation model]
M --> Ans[Answer]| Step | Role |
|---|---|
| Parse | Read and extract content from PDF, Word, HTML, or other sources |
| Chunking | Split documents into small passages for more precise retrieval |
| Embedding | Convert text into numeric vectors that capture semantic meaning |
| Vector store | Store and index the embeddings |
| Retrieval | Find the passages most relevant to the user’s question |
| Augmentation | Insert the retrieved content into the prompt as context |
| Generation | The foundation model uses that context to produce the answer |
Embeddings
An embedding converts text into a numeric vector that represents its meaning — text with similar meaning maps to nearby vectors. Use the same embedding model for both documents and the query, and measure relevance with cosine similarity.
Vector database
A vector store stores and indexes embeddings and performs similarity search (often approximate-nearest-neighbor for speed). Examples: pgvector, FAISS, Pinecone, Chroma, Weaviate.
When to use RAG
- Internal data changes frequently.
- Answers must be based on company documents.
- You need to cite sources or keep references.
- You want to reduce hallucination with trustworthy context.
- You don’t want to retrain the model every time data changes.
- You need to control the scope of knowledge the model can use.
RAG vs. fine-tuning
Suppose a company has thousands of product manuals updated weekly. With fine-tuning, they may need to retrain the model every time documents change. With RAG, they only update the data source and re-sync the embeddings. The full comparison — including prompting as the third lever — is in Adaptation.
Variants
“RAG” is a family, not one design — standard, advanced, self-correcting, graph, multimodal, and agentic. And some things you’ll hear (hybrid search, re-ranking) are techniques used inside a variant, not separate types. See Types of RAG to tell an architecture from a control loop from a technique.
Strengths & limitations
- Strengths — fresh, private, and citable knowledge without retraining; reduces hallucination; update by re-syncing data.
- Limitations — answer quality is capped by retrieval quality; adds an ingestion pipeline, a vector store, and latency; needs chunking and evaluation tuning.
Sources
- Lewis et al., Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (2020) — arXiv:2005.11401
- Es et al., RAGAS: Automated Evaluation of Retrieval Augmented Generation (2023) — arXiv:2309.15217