检索增强生成(RAG)基础
学习 RAG 如何在查询时检索相关上下文并将其输入提示词,使 LLM 的响应建立在您自己的文档之上。
检索增强生成(RAG)基础 是 CoddyKit 上的免费 Prompt Engineering & LLM Optimization for Developers 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Prompt Engineering & LLM Optimization for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Prompt Engineering & LLM Optimization for Developers 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
The Knowledge Gap
An LLM only knows what it was trained on. It cannot answer questions about your private docs or recent events. RAG (Retrieval-Augmented Generation) closes this gap by fetching relevant text and putting it in the prompt.
The Core Idea
Instead of fine-tuning the model on your data, you retrieve the most relevant snippets at query time and let the model answer using them as context. Cheaper, faster to update, and easy to cite.
Step 1: Chunking
Documents are split into small chunks (a few hundred tokens each). Chunks small enough to be precise, large enough to keep meaning.
chunks = split(document, size=500, overlap=50)Step 2: Embeddings
Each chunk is converted to a vector with an embedding model. Similar meanings produce nearby vectors, enabling semantic search.
vector = embed("Refunds are processed in 5 days.")
// -> [0.012, -0.43, 0.88, ...]Step 3: The Vector Store
Vectors are saved in a vector database like Pinecone, Weaviate, or pgvector. It supports fast nearest-neighbor search over millions of chunks.
Step 4: Retrieve at Query Time
When a user asks a question, you embed the question and find the top-k most similar chunks.
q = embed(userQuestion);
results = store.search(q, topK=4);Step 5: Augment the Prompt
The retrieved chunks are inserted into the prompt as context, and the model is told to answer using only that context.
Use only the context below to answer.
Context:
{retrieved_chunks}
Question: {user_question}Grounding and Citations
Because the answer is built from real chunks, you can show citations back to the source documents, and you can instruct the model to say I do not know when the context lacks the answer.
Why Not Just Fine-Tune?
- RAG updates instantly: change a doc, re-index, done.
- Fine-tuning is slow and bakes knowledge in.
- RAG gives traceable sources; fine-tuning does not.
Common RAG Problems
Poor chunking, weak embeddings, or retrieving too few chunks all hurt quality. If answers are wrong, inspect what was retrieved first; the issue is usually retrieval, not the model.
Improving Retrieval
- Add overlap between chunks.
- Use hybrid keyword + vector search.
- Re-rank results with a cross-encoder.
- Tune top-k for your context window.
Quick Check
Test your understanding of RAG.
Recap
RAG chunks documents, embeds them into a vector store, retrieves the most relevant chunks for each query, and augments the prompt. It grounds answers, enables citations, and stays current without retraining.
常见问题解答
「检索增强生成(RAG)基础」课时是免费的吗?
是的 — 「检索增强生成(RAG)基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Prompt Engineering & LLM Optimization for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Prompt Engineering & LLM Optimization for Developers 课程共包含 4 节课。
「检索增强生成(RAG)基础」这节课中我会学到什么?
学习 RAG 如何在查询时检索相关上下文并将其输入提示词,使 LLM 的响应建立在您自己的文档之上。 你通过在浏览器中直接运行的动手代码来练习 Prompt Engineering & LLM Optimization for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Prompt Engineering & LLM Optimization for Developers 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Prompt Engineering & LLM Optimization for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「检索增强生成(RAG)基础」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Prompt Engineering & LLM Optimization for Developers 课中编写并运行代码吗?
能。每节 Prompt Engineering & LLM Optimization for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。