Retrieval-Augmented Generation and Generative AI Applications
Embeddings, Dense Retrieval, and Vector Search · 15 min
Any retrieval system starts from the same basic problem: given a query and a large corpus of candidate passages, decide which passages are actually relevant. Older lexical retrieval methods score this relevance by matching literal terms, which struggles when a relevant passage uses different words for the same idea, a synonym, a paraphrase, or a related concept that never shares an exact keyword with the query. Dense retrieval takes a different approach: it represents both the query and every candidate passage as a dense vector, or embedding, positioned in a shared numerical space such that texts with similar meaning end up near one another, regardless of whether they share any exact words. Relevance is then scored with a similarity measure over these vectors, most commonly cosine similarity, which asks how closely two vectors point in the same direction rather than how large they are. This reframes retrieval as a geometry problem — finding the vectors nearest a query vector — which is what makes it tractable to search over enormous, constantly growing text collections.
Karpukhin et al.'s 2020 paper on Dense Passage Retrieval, or DPR, is an influential concrete implementation of this idea for open-domain question answering. DPR uses a dual-encoder architecture: one BERT-based encoder converts questions into embeddings, and a separate BERT-based encoder converts passages into embeddings, and the two are trained together with a contrastive objective so that a question's embedding lands close to embeddings of passages that actually answer it and far from embeddings of unrelated passages used as negative examples. Karpukhin et al. reported that this dense approach substantially outperformed traditional sparse lexical retrieval on open-domain QA retrieval benchmarks. To see the geometry concretely: suppose a query embedding is q = (1, 0), and three candidate passage embeddings are d1 = (0.8, 0.6), d2 = (0, 1), and d3 = (-1, 0). Cosine similarity for d1 is (1×0.8 + 0×0.6) / (1×1) = 0.8; for d2 it is (1×0 + 0×1) / (1×1) = 0; for d3 it is (1×-1 + 0×0) / (1×1) = -1. Ranking by similarity, d1 is retrieved first, d2 second, and d3 — pointing in the opposite direction from the query — last.
Real corpora used for retrieval hold millions or billions of passage embeddings, and computing exact similarity between a query and every single one of them at search time is too slow for interactive use. FAISS, described by Johnson, Douze, and Jégou, addresses this with approximate nearest-neighbor search: techniques such as product quantization compress each embedding into a much smaller representation, and index structures narrow the search to a promising subset of candidates rather than scanning the entire corpus, trading a small, controllable amount of retrieval accuracy for large gains in search speed and memory footprint, with GPU-accelerated implementations further increasing throughput. This approximate-search layer, sitting between a dense retriever like DPR and a downstream generator, is what makes retrieval over large, real-world corpora practically fast enough to sit inside an interactive system rather than a slow offline batch job — at the cost of an index that must itself be built, stored, and kept up to date as the underlying corpus changes.
Retrieval-Augmented Generation, Tool Calling, and Generative Applications · 15 min
Retrieval on its own only returns a ranked list of passages; retrieval-augmented generation, introduced by Lewis et al. in 2020, connects that retrieval step to a generator so the two work together as one system. A RAG system combines a pretrained sequence-to-sequence generator with a non-parametric retrieval component: given a query, it first retrieves the top-k most relevant passages from an indexed corpus, then conditions the generator on both the original query and the retrieved passages, so the generated text can draw directly on that retrieved evidence rather than relying solely on whatever the generator's parameters happened to memorize during pretraining. This has two practical advantages over a purely parametric model generating from memory alone: the underlying knowledge source can be updated or swapped by changing the retrieval index, without retraining the generator itself, and because the retrieved passages are visible, outputs can in principle be checked or traced back against the specific evidence the generator was given, an important, if imperfect, mitigation for hallucination in knowledge-intensive tasks.
Retrieving text is one way to extend a generator beyond its frozen parameters; calling external tools is another. Schick et al.'s 2023 Toolformer trains a model to use tools, such as a calculator, a search engine, or a calendar, in a self-supervised way: the model itself proposes candidate API calls to insert at various points in training text, actually executes those calls, and keeps only the insertions that measurably improve its ability to predict the text that follows, discarding the rest — meaning it learns when and how to invoke a tool without needing hand-labeled tool-use demonstrations. Yao et al.'s ReAct takes a complementary approach for multi-step tasks: rather than a single forward pass, the model interleaves explicit natural-language reasoning steps with actions, such as issuing a search query, and observations, the results that action returns, so it can revise its plan mid-task based on what a tool actually reports back, rather than committing to one fixed reasoning chain up front. Together, retrieval and tool calling let a generator's output be grounded not just in retrieved text but in the results of actions it takes during generation itself.
Two further application patterns extend generation in different directions. Structured generation constrains a model's output to match a fixed schema, such as a specific set of JSON fields an application expects, which matters whenever generated text needs to be consumed programmatically rather than read by a person — a practical engineering layer on top of the free-text generation capability described above, rather than a change to the underlying model. Multimodal generation follows a related logic in a different modality: Rombach et al.'s 2021 latent diffusion models compress images into a much lower-dimensional latent space using a learned autoencoder, then run the iterative, computationally expensive diffusion denoising process in that compact latent space rather than directly on raw pixels, substantially reducing the compute required to train and sample from high-resolution text-to-image models. Architectures, safety mitigations, and evaluation methods for multimodal generation are all still moving quickly, and — like generative-model evaluation more broadly — this remains an actively evolving area of research rather than a settled one.
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
- Toolformer: Language Models Can Teach Themselves to Use Tools (Schick et al., 2023)
- ReAct: Synergizing Reasoning and Acting in Language Models (Yao et al., 2022)
- High-Resolution Image Synthesis with Latent Diffusion Models (Rombach et al., 2021)
Retrieval-Augmented and Tool-Using Generation
A query becomes a vector, the vector finds nearby evidence in an index, the generator is grounded in that evidence — and where evidence alone isn't enough, tool calls (ReAct, Toolformer) let the model act and observe mid-generation.
- Dense retrieval reframes "is this passage relevant?" as geometry: cosine similarity between a query embedding q=(1,0) and a passage embedding d=(0.8,0.6) is 0.8, versus 0 for an orthogonal passage and -1 for one pointing the opposite way — DPR (Karpukhin et al., 2020) trains dual encoders specifically to make relevant pairs land close together.
- FAISS-style approximate nearest-neighbor search trades a small, controllable amount of accuracy for large gains in speed and memory, which is what makes retrieval over billions of vectors fast enough for an interactive system rather than an offline batch job.
- RAG (Lewis et al., 2020) doesn't just retrieve — it conditions the generator on the retrieved passages, so the knowledge source can be updated by changing the index alone, without retraining the generator, and outputs can in principle be traced back to specific evidence.
Recall Practice
Glossary
- Dense Embedding
- A learned, fixed-length vector representation of text such that semantically similar texts end up near one another in the vector space, regardless of exact word overlap.
- Cosine Similarity
- A similarity measure between two vectors equal to their dot product divided by the product of their magnitudes, capturing how closely they point in the same direction; used to rank retrieved passages by relevance to a query.
- Dense Passage Retrieval (DPR)
- A 2020 dual-encoder retrieval method (Karpukhin et al.) that trains separate question and passage encoders contrastively, reported to outperform traditional sparse lexical retrieval on open-domain QA benchmarks.
- Approximate Nearest-Neighbor (ANN) Search / FAISS
- Search techniques, such as those in Facebook AI's FAISS library (Johnson, Douze & Jégou), that trade a small, controllable amount of retrieval accuracy for large gains in speed and memory efficiency when searching millions or billions of embeddings.
- Retrieval-Augmented Generation (RAG)
- An architecture (Lewis et al., 2020) that conditions a generator's output on passages retrieved from an external index at query time, letting the knowledge source be updated without retraining the generator.
- ReAct / Toolformer
- Two complementary approaches to tool-augmented generation: ReAct (Yao et al., 2022) interleaves reasoning, action, and observation steps at inference time; Toolformer (Schick et al., 2023) self-supervises a model at training time to learn when API calls improve its predictions.
Rank Passages by Hand and Trace a RAG + Tool-Use Pipeline
A virtual, paper-based worksheet — no real embedding model, vector database, or API calls of any kind. Learners are given a supplied query and three toy passages, each with a hand-assigned 2D embedding coordinate (mirroring the lesson's worked example), and must compute the cosine similarity between the query and each passage by hand, rank the passages, and select the top match. They then draft a short grounded-generation answer that explicitly cites which retrieved passage supports each claim it makes (as a RAG system's generator would be conditioned to do). Finally, given one supplied multi-step question that plausibly requires a calculator-style computation partway through, learners write out a ReAct-style trace by hand — alternating a reasoning line, an action line naming a tool call, and an observation line stating the (self-supplied, invented) tool result — ending in a final answer, to practice the reasoning-acting-observing pattern without any live tool execution.
Ready to test yourself?
5 questions on this module.