测试与评估您的 RAG 应用
创建测试集,并在发布前使用实用指标衡量检索质量和答案质量,从而增强对首个 RAG 应用的信心。
测试与评估您的 RAG 应用 是 CoddyKit 上的免费 LLM Apps in Production (RAG + Vector DB + Caching) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LLM Apps in Production (RAG + Vector DB + Caching) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Evaluate RAG
A RAG app can look fine on a few queries and fail badly on others. Without measurement you cannot tell if a change helped or hurt.
Evaluation gives you a repeatable score to guide improvements.
Two Things to Measure
RAG quality has two parts:
- Retrieval: did we fetch the right documents?
- Generation: did the answer use them correctly?
A bad answer can come from either, so measure both.
Building a Test Set
Create a small set of questions with known correct answers and the documents that contain them. Even 20 to 50 examples are enough to start.
testset = [
{'q': 'What is the refund window?',
'answer': '30 days',
'source': 'policy.md'}
]Retrieval Metric: Hit Rate
Hit rate (or recall@k) checks whether the correct source appears in the top-k retrieved chunks. High hit rate means retrieval is doing its job.
def hit(retrieved, expected_source):
return any(d.metadata['source'] == expected_source
for d in retrieved)Faithfulness
Faithfulness asks: is the answer supported by the retrieved context, or did the model make things up? An LLM judge can score this automatically.
Answer Relevance
Answer relevance measures whether the response actually addresses the question, regardless of sources. A faithful answer can still be off-topic.
LLM as a Judge
You can use a strong model to grade outputs against the expected answer, returning a pass or score with a reason.
judge_prompt = (
'Question: {q}\nExpected: {gold}\n'
'Got: {pred}\nIs it correct? Answer yes or no.'
)Running the Evaluation
Loop over the test set, run your pipeline, and aggregate scores into a single report you can compare across versions.
scores = []
for case in testset:
pred = rag.invoke(case['q'])
scores.append(grade(case, pred))
print(sum(scores) / len(scores))Comparing Configurations
Change one variable — chunk size, k, prompt, model — rerun the same test set, and compare scores. This turns guesswork into evidence-based tuning.
Watching for Regressions
Keep the test suite in CI. When a change drops a metric, you catch the regression before users do. Treat evaluation like unit tests for AI quality.
Improving From Results
Use failures to guide fixes:
- Low hit rate? Adjust chunking or retrieval
- Low faithfulness? Strengthen grounding instructions
- Low relevance? Improve the prompt
Quick Check
Test your evaluation knowledge.
Recap
You learned to evaluate your RAG app:
- Measure both retrieval and generation
- Build a small test set with known answers
- Use hit rate, faithfulness, and answer relevance
- Let an LLM judge grade outputs
- Compare configs and guard against regressions in CI
Evaluation turns RAG improvement into a measurable, repeatable process.
常见问题解答
「测试与评估您的 RAG 应用」课时是免费的吗?
是的 — 「测试与评估您的 RAG 应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LLM Apps in Production (RAG + Vector DB + Caching) 课程的其余内容,请升级到 CoddyKit PRO。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。
「测试与评估您的 RAG 应用」这节课中我会学到什么?
创建测试集,并在发布前使用实用指标衡量检索质量和答案质量,从而增强对首个 RAG 应用的信心。 你通过在浏览器中直接运行的动手代码来练习 LLM Apps in Production (RAG + Vector DB + Caching),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 LLM Apps in Production (RAG + Vector DB + Caching) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 LLM Apps in Production (RAG + Vector DB + Caching) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「测试与评估您的 RAG 应用」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 LLM Apps in Production (RAG + Vector DB + Caching) 课中编写并运行代码吗?
能。每节 LLM Apps in Production (RAG + Vector DB + Caching) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 选择 LLM 提供商
- 数据加载与文本分块基础
- 构建简单的 RAG 流水线
- 测试与评估您的 RAG 应用