将检索接入提示
基于有依据且带引用的上下文作答
将检索接入提示 是 CoddyKit 上的免费 NLP Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NLP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NLP Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Putting the Pieces Together
You can retrieve relevant chunks and call an LLM. Now you connect them: feed the retrieved text into the prompt as context.
The Augmented Prompt
A RAG prompt has three parts: an instruction, the retrieved context, and the user question. The model reads all three together.
Joining the Retrieved Chunks
First stitch your top chunks into one block of text. A blank line between them keeps each passage readable to the model.
context = "\n\n".join(hits)Building the Prompt String
Wrap the context and question in clear labels. A simple template tells the model exactly what to use and what to answer.
prompt = f"Context:\n{context}\n\nQuestion: {question}\nAnswer:"Telling the Model to Stay Grounded
Add an instruction to answer only from the context. This curbs hallucination and keeps the response tied to your sources.
Handling Unknown Answers
Tell the model to say it does not know when the context is silent. Allowing I do not know beats a confident wrong guess.
Sending It to the LLM
Pass the assembled prompt to your model call. The LLM now answers using your retrieved context, not just its training.
answer = llm(prompt)Showing the Sources
Return the chunks you used alongside the answer. These citations let users verify the claim and build trust.
Watch the Context Length
Too many chunks overflow the context window. Keep k small and trim long passages so the prompt fits the model budget.
A Minimal RAG Function
The full flow is tiny: embed, search, build prompt, generate. This one function captures an entire RAG pipeline.
def rag(q):
hits = retrieve(q)
return llm(build_prompt(hits, q))Improving Retrieval Quality
If answers are weak, the fix is usually retrieval, not the LLM. Better chunks and re-ranking raise quality more than a bigger model.
Quick Check
Think about what goes into a RAG prompt.
Recap
You join retrieved chunks into context, build an instruction-plus-question prompt, generate a grounded answer, and show sources. ✅
常见问题解答
「将检索接入提示」课时是免费的吗?
是的 — 「将检索接入提示」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NLP Academy 课程的其余内容,请升级到 CoddyKit PRO。 NLP Academy 课程共包含 4 节课。
「将检索接入提示」这节课中我会学到什么?
基于有依据且带引用的上下文作答 你通过在浏览器中直接运行的动手代码来练习 NLP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NLP Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NLP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「将检索接入提示」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NLP Academy 课中编写并运行代码吗?
能。每节 NLP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。