0Pricing
LangChain / RAG / Vector DBs · 课时

评估 RAG 系统性能

学习评估 RAG 应用回答的准确性、相关性和连贯性的指标与技术

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

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

Why Evaluate RAG Systems?

When you build a Retrieval Augmented Generation (RAG) application, it's crucial to know if it's working as intended. Evaluation helps us understand if our system is providing accurate, relevant, and helpful answers.

Without proper evaluation, it's hard to tell if changes to your RAG pipeline (like new embedding models or chunking strategies) are actually making it better or worse.

Key Aspects of RAG Evaluation

Evaluating a RAG system involves looking at different aspects. We generally focus on:

  • Retrieval Quality: Are the right documents being found?
  • Generation Quality: Is the LLM producing good answers based on those documents?

These two parts are often evaluated separately and then together to get a full picture.

The Importance of Ground Truth

To evaluate effectively, especially for automated metrics, you need a 'ground truth' dataset. This means having:

  • A set of user queries.
  • The truly relevant documents for each query.
  • The ideal, correct answer for each query.

This reference data allows us to compare our RAG system's outputs against what's considered correct.

Retrieval Metrics: Precision & Recall

For the retrieval component, we often use metrics like Precision and Recall.

  • Precision: Out of all the documents retrieved, how many were actually relevant? High precision means fewer irrelevant documents.
  • Recall: Out of all the truly relevant documents, how many did our system manage to retrieve? High recall means fewer missed relevant documents.

These help us gauge how good our document search is.

Code: Calculating Retrieval Metrics

This Python snippet demonstrates how to calculate precision and recall for a mock retrieval scenario. relevant_docs are the 'ground truth' and retrieved_docs are what our system found.

def evaluate_retrieval(relevant_docs, retrieved_docs):
    true_positives = len(relevant_docs.intersection(retrieved_docs))

    precision = true_positives / len(retrieved_docs) if len(retrieved_docs) > 0 else 0
    recall = true_positives / len(relevant_docs) if len(relevant_docs) > 0 else 0

    print(f"Precision: {precision:.2f}")
    print(f"Recall: {recall:.2f}")

if __name__ == "__main__":
    # Example 1: Perfect retrieval
    relevant_set_1 = {"docA", "docB", "docC"}
    retrieved_set_1 = {"docA", "docB", "docC"}
    print("--- Example 1: Perfect Retrieval ---")
    evaluate_retrieval(relevant_set_1, retrieved_set_1)

    # Example 2: Some missing, some irrelevant
    relevant_set_2 = {"docX", "docY", "docZ"}
    retrieved_set_2 = {"docX", "docA", "docY"}
    print("\n--- Example 2: Mixed Retrieval ---")
    evaluate_retrieval(relevant_set_2, retrieved_set_2)

Generation Metrics: Faithfulness

For the generated answer, faithfulness (also called 'factuality') is key. It measures whether the LLM's answer is truly supported by the retrieved documents.

A RAG system should not 'hallucinate' or make up information. If a fact isn't in the retrieved context, the LLM shouldn't include it in its answer.

Generation Metrics: Answer Relevance

Answer relevance assesses if the generated answer directly addresses the user's original query. An answer might be faithful to the retrieved documents but still not relevant if the documents themselves were off-topic.

This metric helps ensure the RAG system stays focused on the user's actual information need.

Human vs. Automated Evaluation

While automated metrics are efficient, human evaluation is often essential for subjective qualities like fluency, tone, and nuanced relevance.

  • Automated: Fast, scalable, good for objective metrics (precision, recall, some faithfulness checks).
  • Human: Gold standard for subjective quality, crucial for complex queries, but expensive and slow.

A hybrid approach, using both, is often best for comprehensive RAG evaluation.

Tools for RAG Evaluation

Several libraries and frameworks help streamline RAG evaluation:

  • LangChain: Provides built-in evaluation modules, including reference-free metrics and integrations with LLM-as-a-judge.
  • Ragas: Specifically designed for RAG evaluation, offering metrics like faithfulness, answer relevance, context precision, and context recall.
  • TruLens: Offers observability and evaluation for LLM applications, including RAG, with deep insights into chain execution.

These tools automate much of the metric calculation and reporting.

Quick Check: RAG Metrics

Which of the following are key metrics used to evaluate the quality of the generated answer in a RAG system?

Recap: Evaluating RAG Performance

We've learned that evaluating RAG systems is vital for ensuring they deliver accurate and relevant information. Key takeaways include:

  • Evaluation covers both retrieval (finding relevant docs) and generation (creating good answers).
  • Ground truth data is essential for reliable evaluation.
  • Metrics like Precision and Recall measure retrieval quality.
  • Faithfulness and Answer Relevance assess the quality of the generated response.
  • A combination of automated and human evaluation provides the most comprehensive insights.
  • Tools like LangChain, Ragas, and TruLens can assist in setting up robust evaluation pipelines.

常见问题解答

「评估 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 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 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