检索增强生成(RAG)
通过检索相关文档并将其注入提示词,将您自己的数据与 LLM 结合,生成有依据且及时更新的答案。
检索增强生成(RAG) 是 CoddyKit 上的免费 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Powered SaaS: Stripe + Auth + Billing + Deploy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is RAG?
Retrieval-Augmented Generation gives an LLM access to external knowledge at query time. Instead of relying only on training data, you fetch relevant text and add it to the prompt.
- Answers stay current without retraining
- Reduces hallucinations
- Lets the model cite your private documents
The RAG Pipeline
A typical pipeline has two phases:
- Indexing: split documents into chunks, embed them, store vectors
- Retrieval + generation: embed the query, find similar chunks, feed them to the LLM
Chunking Documents
Split long documents into smaller chunks (often 200-500 tokens) with slight overlap. Good chunking keeps related ideas together so retrieval returns coherent context.
Creating Embeddings
An embedding model turns text into a numeric vector. Similar meanings produce nearby vectors. You embed every chunk during indexing.
const emb = await client.embeddings.create({
model: "text-embedding-3-small",
input: chunkText,
});
const vector = emb.data[0].embedding;Storing Vectors
Vectors live in a vector database such as pgvector, Pinecone, or Qdrant. Each record stores the vector plus metadata (source, title, chunk id) for later filtering and citation.
Retrieving Relevant Chunks
At query time you embed the user question and run a similarity search (cosine distance) to get the top-k closest chunks.
SELECT content FROM docs
ORDER BY embedding <=> $1
LIMIT 5;Building the Augmented Prompt
Insert the retrieved chunks into the prompt as context, then ask the model to answer using only that context.
const prompt = "Context:\n" + chunks.join("\n---\n") +
"\n\nQuestion: " + userQuestion +
"\nAnswer using only the context above.";Citing Sources
Because each chunk carries metadata, you can show citations next to the answer. This builds trust and lets users verify claims against the original document.
Handling No Good Match
If similarity scores are all low, the knowledge base probably lacks the answer. Detect this with a threshold and have the model reply that it does not know, rather than guessing.
Keeping the Index Fresh
When source documents change, re-embed and upsert the affected chunks. Track a content hash per chunk so you only re-index what actually changed, saving embedding cost.
Evaluating RAG Quality
Measure two things: retrieval quality (did we fetch the right chunks?) and answer quality (is the response grounded?). Use a test set of question and answer pairs and check whether the cited chunks contain the supporting facts.
Quick Check
Check your understanding of RAG.
Recap
You learned the full RAG flow:
- Chunk and embed documents into a vector store
- Embed the query and retrieve top-k similar chunks
- Augment the prompt and answer with citations
- Handle low-confidence matches and keep the index fresh
RAG grounds your AI features in your own data without retraining.
常见问题解答
「检索增强生成(RAG)」课时是免费的吗?
是的 — 「检索增强生成(RAG)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程的其余内容,请升级到 CoddyKit PRO。 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程共包含 4 节课。
「检索增强生成(RAG)」这节课中我会学到什么?
通过检索相关文档并将其注入提示词,将您自己的数据与 LLM 结合,生成有依据且及时更新的答案。 你通过在浏览器中直接运行的动手代码来练习 AI Powered SaaS: Stripe + Auth + Billing + Deploy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Powered SaaS: Stripe + Auth + Billing + Deploy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「检索增强生成(RAG)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课中编写并运行代码吗?
能。每节 AI Powered SaaS: Stripe + Auth + Billing + Deploy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。