0Pricing
LangChain / RAG / Vector DBs · 课时

为 RAG 构建黄金测试集

创建经过整理的问题—答案数据集,以便长期客观地衡量和比较 RAG 质量。

为 RAG 构建黄金测试集 是 CoddyKit 上的免费 LangChain / RAG / Vector DBs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LangChain / RAG / Vector DBs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LangChain / RAG / Vector DBs 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why You Need a Test Set

Eyeballing a few answers does not tell you if a change helped or hurt. A golden test set of question-answer pairs gives you repeatable, comparable measurements.

Anatomy of a Test Case

Each case captures what to ask, what is correct, and where the answer lives.

  • question
  • ground_truth answer
  • relevant_sources (expected docs)

A Sample Dataset

Store cases as simple records you can load and iterate over.

testset = [
    {"question": "What is the refund window?",
     "ground_truth": "30 days from purchase.",
     "sources": ["policy.pdf#p2"]},
    {"question": "Who approves expenses?",
     "ground_truth": "The department manager.",
     "sources": ["handbook.pdf#p7"]},
]

Manual vs. Synthetic

You can write cases by hand for accuracy, or generate them by prompting an LLM over your documents for scale. A hybrid approach is common: generate, then review.

Generating Questions with an LLM

Feed a chunk to the model and ask it to produce a question whose answer is contained in that chunk, plus the answer itself.

prompt = (
  "Read the passage and write one question a user might ask, "
  "plus the exact answer.\n\nPassage: " + chunk.page_content
)
qa = llm.invoke(prompt)

Reviewing Synthetic Cases

LLM-generated pairs can be ambiguous or unanswerable. Human review filters out weak cases before they pollute your metrics.

Retrieval vs. Generation Metrics

Separate two questions: did we fetch the right docs (retrieval), and did we write the right answer (generation)? Each is measured differently.

Context Recall

Context recall checks whether the expected source appears among the retrieved chunks. It isolates retrieval quality from the LLM.

def context_recall(retrieved_ids, expected_ids):
    hits = sum(1 for e in expected_ids if e in retrieved_ids)
    return hits / len(expected_ids)

Answer Correctness

Compare the generated answer to the ground truth. Exact match is brittle, so use an LLM judge or semantic similarity for fuzzy correctness.

judge_prompt = (
  "Is the ANSWER correct given the REFERENCE? Reply yes or no.\n"
  "REFERENCE: " + truth + "\nANSWER: " + answer
)
verdict = llm.invoke(judge_prompt)

Running the Suite

Loop over every case, run your pipeline, and aggregate scores so one number summarizes the whole system.

scores = []
for case in testset:
    docs = retriever.invoke(case["question"])
    ans = rag_chain.invoke(case["question"])
    scores.append(evaluate(case, docs, ans))
print(sum(scores) / len(scores))

Track Results Over Time

Save each run with a timestamp and the config used. When a metric drops, you can pinpoint the change that caused the regression.

Quick Check

Test your understanding of RAG evaluation.

Recap

You built an evaluation foundation:

  • A golden test set of question, ground truth, and sources
  • Generate then review synthetic cases
  • Measure retrieval (context recall) and generation (answer correctness) separately
  • Track scores across runs

常见问题解答

「为 RAG 构建黄金测试集」课时是免费的吗?

是的 — 「为 RAG 构建黄金测试集」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LangChain / RAG / Vector DBs 课程的其余内容,请升级到 CoddyKit PRO。 LangChain / RAG / Vector DBs 课程共包含 4 节课。

「为 RAG 构建黄金测试集」这节课中我会学到什么?

创建经过整理的问题—答案数据集,以便长期客观地衡量和比较 RAG 质量。 你通过在浏览器中直接运行的动手代码来练习 LangChain / RAG / Vector DBs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 LangChain / RAG / Vector DBs 需要有经验吗?

无需任何先前经验。CoddyKit 上的 LangChain / RAG / Vector DBs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「为 RAG 构建黄金测试集」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 LangChain / RAG / Vector DBs 课中编写并运行代码吗?

能。每节 LangChain / RAG / Vector DBs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 集成所有 RAG 组件
  2. 查询与答案生成
  3. 评估 RAG 系统性能
  4. 为 RAG 构建黄金测试集
← 返回 LangChain / RAG / Vector DBs