An illustrated machine that shows Retrieval-Augmented Generation: chunking, embedding, a vector database, Top-K search and a grounded answer, with Chinese-English labels
RAG (Retrieval-Augmented Generation) lets an AI look things up before it answers. This machine does it in two halves: on the left it files away documents, on the right it answers a question using what it found. Drag empty space to turn the machine.
RAG 让 AI 先“查资料”再回答。左边整理文档,右边回答问题。拖动空白处可以旋转机器。
First, each document is cut into short passages called chunks. A chunk is often a few hundred tokens (word pieces), and neighbours share a little overlap so no sentence is lost at the cut.
切块:把长文档切成小段,每段通常几百个词元,相邻块会重叠一点,避免句子被切断。
An embedding model reads each chunk and turns its meaning into a long list of numbers, a vector. Real vectors have hundreds or thousands of numbers; here we squeeze them into 3D so you can see them. Chunks that mean similar things land close together.
向量化:嵌入模型把每个文本块变成一串数字(向量)。意思相近的块,位置也相近。
The vectors are stored in a vector database, together with the original text of each chunk. Chunks about the same topic gather into clusters. Pick a cluster, or tap a glowing point, to see its neighbours.
向量数据库同时保存向量和原文。同一主题的块会聚成一团(聚类)。点击发光的点试试。
Now someone asks: "When is the school library open on Saturday?" The question goes through the very same embedding model, so it lands in the same space as the chunks. A different model would make the distances meaningless.
提问也要用同一个嵌入模型变成向量,这样问题和文本块才能互相比较。
The database finds the K chunks whose vectors sit nearest the question, using a measure such as cosine similarity. Slide K: too few may miss the answer, too many bring in noise. Typical apps use about 3 to 5.
相似度检索:找出离问题最近的 K 个块。K 太小会漏掉答案,太大会带来无关内容。
The retrieved chunks are pasted into the prompt next to the question, as extra context. The large language model (LLM) then writes its answer from that text and can point to the chunks it used. The model itself is not retrained.
增强:把检索到的文本块和问题一起交给大语言模型。生成:模型根据这些资料写出答案,并可注明出处。
Without RAG: "Libraries are usually open 9 to 5." A confident guess, because the model never saw this school's notice. With RAG: "Saturdays, 10:00 to 14:00 (source: chunk 3)." Switch RAG off and on to compare. RAG makes mistakes rarer, but it cannot fix a wrong document.
没有 RAG:“图书馆一般九点到五点开。”(猜的)有 RAG:“周六 10:00–14:00(来源:第3块)。”RAG 减少“幻觉”,但不能保证百分之百正确。