Initial commit
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
commit
4ecf7b351d
143 files changed
+24156
No files matched your search
@@ -0,0 +1,491 @@
|
||||
"""
|
||||
Unit tests for RAG evaluation metrics
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from rag_evaluation.evaluation.metrics import (
|
||||
ContextRecall,
|
||||
ContextPrecision,
|
||||
MRR,
|
||||
NDCG,
|
||||
Faithfulness,
|
||||
AnswerRelevancy,
|
||||
Correctness,
|
||||
MetricsCalculator
|
||||
)
|
||||
|
||||
|
||||
class TestContextRecall:
|
||||
"""测试上下文召回率指标"""
|
||||
|
||||
@pytest.fixture
|
||||
def metric(self):
|
||||
"""创建指标实例"""
|
||||
return ContextRecall()
|
||||
|
||||
def test_perfect_recall(self, metric):
|
||||
"""测试完美召回率(所有相关文档都被检索到)"""
|
||||
retrieved = ["doc1", "doc2", "doc3"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 1.0
|
||||
|
||||
def test_partial_recall(self, metric):
|
||||
"""测试部分召回率"""
|
||||
retrieved = ["doc1", "doc2"]
|
||||
ground_truth = ["doc1", "doc2", "doc3", "doc4"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.5
|
||||
|
||||
def test_zero_recall(self, metric):
|
||||
"""测试零召回率(没有检索到任何相关文档)"""
|
||||
retrieved = ["doc5", "doc6"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_retrieved(self, metric):
|
||||
"""测试空检索结果"""
|
||||
retrieved = []
|
||||
ground_truth = ["doc1", "doc2"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_ground_truth(self, metric):
|
||||
"""测试空基准真值"""
|
||||
retrieved = ["doc1", "doc2"]
|
||||
ground_truth = []
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_extra_retrieved_docs(self, metric):
|
||||
"""测试检索到额外文档(不影响召回率)"""
|
||||
retrieved = ["doc1", "doc2", "doc5", "doc6"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 2.0 / 3.0
|
||||
|
||||
def test_duplicate_handling(self, metric):
|
||||
"""测试重复文档处理"""
|
||||
retrieved = ["doc1", "doc1", "doc2"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
# 集合去重后应该是 2/3
|
||||
assert score == 2.0 / 3.0
|
||||
|
||||
|
||||
class TestContextPrecision:
|
||||
"""测试上下文精确率指标"""
|
||||
|
||||
@pytest.fixture
|
||||
def metric(self):
|
||||
"""创建指标实例"""
|
||||
return ContextPrecision()
|
||||
|
||||
def test_perfect_precision(self, metric):
|
||||
"""测试完美精确率(所有检索到的文档都相关)"""
|
||||
retrieved = ["doc1", "doc2", "doc3"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 1.0
|
||||
|
||||
def test_partial_precision(self, metric):
|
||||
"""测试部分精确率"""
|
||||
retrieved = ["doc1", "doc2", "doc5", "doc6"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.5
|
||||
|
||||
def test_zero_precision(self, metric):
|
||||
"""测试零精确率(所有检索到的文档都不相关)"""
|
||||
retrieved = ["doc5", "doc6", "doc7"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_retrieved(self, metric):
|
||||
"""测试空检索结果"""
|
||||
retrieved = []
|
||||
ground_truth = ["doc1", "doc2"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_ground_truth(self, metric):
|
||||
"""测试空基准真值"""
|
||||
retrieved = ["doc1", "doc2"]
|
||||
ground_truth = []
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_duplicate_handling(self, metric):
|
||||
"""测试重复文档处理"""
|
||||
retrieved = ["doc1", "doc1", "doc5"]
|
||||
ground_truth = ["doc1", "doc2"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
# 集合去重: retrieved_set = {"doc1", "doc5"}, ground_truth_set = {"doc1", "doc2"}
|
||||
# overlap = {"doc1"}, precision = 1/2
|
||||
assert score == 0.5
|
||||
|
||||
|
||||
class TestMRR:
|
||||
"""测试平均倒数排名指标"""
|
||||
|
||||
@pytest.fixture
|
||||
def metric(self):
|
||||
"""创建指标实例"""
|
||||
return MRR()
|
||||
|
||||
def test_first_relevant_at_rank_1(self, metric):
|
||||
"""测试第一个相关文档排在第1位"""
|
||||
retrieved = ["doc1", "doc2", "doc3"]
|
||||
ground_truth = ["doc1", "doc4"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 1.0
|
||||
|
||||
def test_first_relevant_at_rank_2(self, metric):
|
||||
"""测试第一个相关文档排在第2位"""
|
||||
retrieved = ["doc5", "doc1", "doc2"]
|
||||
ground_truth = ["doc1", "doc4"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.5
|
||||
|
||||
def test_first_relevant_at_rank_3(self, metric):
|
||||
"""测试第一个相关文档排在第3位"""
|
||||
retrieved = ["doc5", "doc6", "doc1"]
|
||||
ground_truth = ["doc1", "doc4"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 1.0 / 3.0
|
||||
|
||||
def test_no_relevant_documents(self, metric):
|
||||
"""测试没有相关文档"""
|
||||
retrieved = ["doc5", "doc6", "doc7"]
|
||||
ground_truth = ["doc1", "doc2"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_retrieved(self, metric):
|
||||
"""测试空检索结果"""
|
||||
retrieved = []
|
||||
ground_truth = ["doc1", "doc2"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_ground_truth(self, metric):
|
||||
"""测试空基准真值"""
|
||||
retrieved = ["doc1", "doc2"]
|
||||
ground_truth = []
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_multiple_relevant_documents(self, metric):
|
||||
"""测试多个相关文档(只计算第一个)"""
|
||||
retrieved = ["doc5", "doc1", "doc2", "doc3"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.5 # 1/2,因为doc1在第2位
|
||||
|
||||
|
||||
class TestNDCG:
|
||||
"""测试归一化折损累计增益指标"""
|
||||
|
||||
@pytest.fixture
|
||||
def metric(self):
|
||||
"""创建指标实例"""
|
||||
return NDCG()
|
||||
|
||||
def test_perfect_ranking(self, metric):
|
||||
"""测试完美排序"""
|
||||
retrieved = ["doc1", "doc2", "doc3"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 1.0
|
||||
|
||||
def test_partial_relevance(self, metric):
|
||||
"""测试部分相关"""
|
||||
retrieved = ["doc1", "doc5", "doc2"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert 0.0 < score < 1.0
|
||||
|
||||
def test_no_relevant_documents(self, metric):
|
||||
"""测试没有相关文档"""
|
||||
retrieved = ["doc5", "doc6", "doc7"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth)
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_lists(self, metric):
|
||||
"""测试空列表"""
|
||||
score = metric.calculate([], ["doc1"])
|
||||
assert score == 0.0
|
||||
|
||||
score = metric.calculate(["doc1"], [])
|
||||
assert score == 0.0
|
||||
|
||||
def test_ndcg_at_k(self, metric):
|
||||
"""测试NDCG@k(截断)"""
|
||||
retrieved = ["doc5", "doc6", "doc1", "doc2"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
|
||||
# NDCG@2 应该比 NDCG@4 低
|
||||
score_2 = metric.calculate(retrieved, ground_truth, k=2)
|
||||
score_4 = metric.calculate(retrieved, ground_truth, k=4)
|
||||
assert score_2 < score_4
|
||||
|
||||
def test_k_larger_than_retrieved(self, metric):
|
||||
"""测试k大于检索结果数量"""
|
||||
retrieved = ["doc1", "doc2"]
|
||||
ground_truth = ["doc1", "doc2", "doc3"]
|
||||
score = metric.calculate(retrieved, ground_truth, k=10)
|
||||
# 应该使用全部检索结果
|
||||
assert 0.0 < score <= 1.0
|
||||
|
||||
|
||||
class TestFaithfulness:
|
||||
"""测试忠实度指标"""
|
||||
|
||||
@pytest.fixture
|
||||
def metric(self):
|
||||
"""创建指标实例"""
|
||||
return Faithfulness()
|
||||
|
||||
def test_high_faithfulness(self, metric):
|
||||
"""测试高忠实度(答案主要基于上下文)"""
|
||||
question = "什么是机器学习?"
|
||||
answer = "机器学习是人工智能的一个分支,它使计算机能够从数据中学习。"
|
||||
contexts = ["机器学习是人工智能的一个分支", "机器学习使计算机能够从数据中学习"]
|
||||
score = metric.calculate(question, answer, contexts)
|
||||
# 由于有较高的词重叠,应该有较高的分数
|
||||
assert score > 0.3
|
||||
|
||||
def test_low_faithfulness(self, metric):
|
||||
"""测试低忠实度(答案与上下文无关)"""
|
||||
question = "什么是机器学习?"
|
||||
answer = "深度学习是神经网络的一种应用。"
|
||||
contexts = ["机器学习是人工智能的一个分支"]
|
||||
score = metric.calculate(question, answer, contexts)
|
||||
# 词重叠较少,分数应该较低
|
||||
assert score < 0.5
|
||||
|
||||
def test_empty_answer(self, metric):
|
||||
"""测试空答案"""
|
||||
score = metric.calculate("问题", "", ["上下文"])
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_contexts(self, metric):
|
||||
"""测试空上下文"""
|
||||
score = metric.calculate("问题", "答案", [])
|
||||
assert score == 0.0
|
||||
|
||||
def test_exact_match(self, metric):
|
||||
"""测试完全匹配"""
|
||||
question = "测试"
|
||||
answer = "机器学习是人工智能"
|
||||
contexts = ["机器学习是人工智能"]
|
||||
score = metric.calculate(question, answer, contexts)
|
||||
assert score == 1.0
|
||||
|
||||
|
||||
class TestAnswerRelevancy:
|
||||
"""测试答案相关性指标"""
|
||||
|
||||
@pytest.fixture
|
||||
def metric(self):
|
||||
"""创建指标实例"""
|
||||
return AnswerRelevancy()
|
||||
|
||||
def test_high_relevancy(self, metric):
|
||||
"""测试高相关性"""
|
||||
question = "什么是机器学习?"
|
||||
answer = "机器学习是人工智能的一个分支,它使计算机能够从数据中学习并改进。"
|
||||
score = metric.calculate(question, answer)
|
||||
# 答案包含问题中的关键词,应该有较高相关性
|
||||
assert score > 0.3
|
||||
|
||||
def test_low_relevancy(self, metric):
|
||||
"""测试低相关性"""
|
||||
question = "什么是机器学习?"
|
||||
answer = "今天天气很好,适合出去散步。"
|
||||
score = metric.calculate(question, answer)
|
||||
# 答案与问题无关,相关性应该很低
|
||||
assert score < 0.2
|
||||
|
||||
def test_empty_question(self, metric):
|
||||
"""测试空问题"""
|
||||
score = metric.calculate("", "答案")
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_answer(self, metric):
|
||||
"""测试空答案"""
|
||||
score = metric.calculate("问题", "")
|
||||
assert score == 0.0
|
||||
|
||||
def test_keyword_overlap(self, metric):
|
||||
"""测试关键词重叠"""
|
||||
question = "机器学习包括哪些类型?"
|
||||
answer = "机器学习包括监督学习、无监督学习和强化学习等类型。"
|
||||
score = metric.calculate(question, answer)
|
||||
# 由于包含多个共同关键词,相关性应该较高
|
||||
assert score > 0.4
|
||||
|
||||
|
||||
class TestCorrectness:
|
||||
"""测试正确性指标"""
|
||||
|
||||
@pytest.fixture
|
||||
def metric(self):
|
||||
"""创建指标实例"""
|
||||
return Correctness()
|
||||
|
||||
def test_perfect_match(self, metric):
|
||||
"""测试完全匹配"""
|
||||
answer = "机器学习是人工智能的一个分支"
|
||||
ground_truth = "机器学习是人工智能的一个分支"
|
||||
score = metric.calculate(answer, ground_truth)
|
||||
assert score == 1.0
|
||||
|
||||
def test_partial_match(self, metric):
|
||||
"""测试部分匹配"""
|
||||
answer = "机器学习是人工智能的一个分支,它可以从数据中学习"
|
||||
ground_truth = "机器学习是人工智能的一个分支"
|
||||
score = metric.calculate(answer, ground_truth)
|
||||
# 应该有较高的分数(因为包含完整答案)
|
||||
assert score > 0.5
|
||||
|
||||
def test_no_match(self, metric):
|
||||
"""测试完全不匹配"""
|
||||
answer = "深度学习使用神经网络进行训练"
|
||||
ground_truth = "机器学习是人工智能的一个分支"
|
||||
score = metric.calculate(answer, ground_truth)
|
||||
# 应该有很低的分数
|
||||
assert score < 0.3
|
||||
|
||||
def test_empty_answer(self, metric):
|
||||
"""测试空答案"""
|
||||
score = metric.calculate("", "参考答案")
|
||||
assert score == 0.0
|
||||
|
||||
def test_empty_ground_truth(self, metric):
|
||||
"""测试空参考答案"""
|
||||
score = metric.calculate("答案", "")
|
||||
assert score == 0.0
|
||||
|
||||
def test_case_insensitive(self, metric):
|
||||
"""测试大小写不敏感"""
|
||||
answer = "Machine Learning is a branch of AI"
|
||||
ground_truth = "machine learning is a branch of ai"
|
||||
score = metric.calculate(answer, ground_truth)
|
||||
assert score == 1.0
|
||||
|
||||
|
||||
class TestMetricsCalculator:
|
||||
"""测试综合指标计算器"""
|
||||
|
||||
@pytest.fixture
|
||||
def calculator(self):
|
||||
"""创建计算器实例"""
|
||||
return MetricsCalculator()
|
||||
|
||||
def test_calculate_all_metrics(self, calculator):
|
||||
"""测试计算所有指标"""
|
||||
question = "什么是机器学习?"
|
||||
retrieved_contexts = ["ctx1", "ctx2", "ctx3"]
|
||||
generated_answer = "机器学习是人工智能的一个分支"
|
||||
ground_truth_contexts = ["ctx1", "ctx2", "ctx4"]
|
||||
ground_truth_answer = "机器学习是人工智能的一个分支"
|
||||
|
||||
metrics = calculator.calculate_all(
|
||||
question=question,
|
||||
retrieved_contexts=retrieved_contexts,
|
||||
generated_answer=generated_answer,
|
||||
ground_truth_contexts=ground_truth_contexts,
|
||||
ground_truth_answer=ground_truth_answer
|
||||
)
|
||||
|
||||
# 验证所有指标都被计算
|
||||
assert 'context_recall' in metrics
|
||||
assert 'context_precision' in metrics
|
||||
assert 'mrr' in metrics
|
||||
assert 'ndcg' in metrics
|
||||
assert 'faithfulness' in metrics
|
||||
assert 'answer_relevancy' in metrics
|
||||
assert 'correctness' in metrics
|
||||
|
||||
# 验证指标值在合理范围内
|
||||
for key, value in metrics.items():
|
||||
assert 0.0 <= value <= 1.0, f"{key} should be between 0 and 1, got {value}"
|
||||
|
||||
def test_calculate_with_perfect_retrieval(self, calculator):
|
||||
"""测试完美检索情况"""
|
||||
question = "测试问题"
|
||||
retrieved_contexts = ["ctx1", "ctx2", "ctx3"]
|
||||
generated_answer = "测试答案"
|
||||
ground_truth_contexts = ["ctx1", "ctx2", "ctx3"]
|
||||
ground_truth_answer = "测试答案"
|
||||
|
||||
metrics = calculator.calculate_all(
|
||||
question=question,
|
||||
retrieved_contexts=retrieved_contexts,
|
||||
generated_answer=generated_answer,
|
||||
ground_truth_contexts=ground_truth_contexts,
|
||||
ground_truth_answer=ground_truth_answer
|
||||
)
|
||||
|
||||
# 检索指标应该是完美的
|
||||
assert metrics['context_recall'] == 1.0
|
||||
assert metrics['context_precision'] == 1.0
|
||||
assert metrics['mrr'] == 1.0
|
||||
assert metrics['ndcg'] == 1.0
|
||||
|
||||
def test_calculate_with_no_retrieval(self, calculator):
|
||||
"""测试没有检索结果的情况"""
|
||||
question = "测试问题"
|
||||
retrieved_contexts = []
|
||||
generated_answer = "测试答案"
|
||||
ground_truth_contexts = ["ctx1", "ctx2"]
|
||||
ground_truth_answer = "测试答案"
|
||||
|
||||
metrics = calculator.calculate_all(
|
||||
question=question,
|
||||
retrieved_contexts=retrieved_contexts,
|
||||
generated_answer=generated_answer,
|
||||
ground_truth_contexts=ground_truth_contexts,
|
||||
ground_truth_answer=ground_truth_answer
|
||||
)
|
||||
|
||||
# 检索指标应该很低
|
||||
assert metrics['context_recall'] == 0.0
|
||||
assert metrics['context_precision'] == 0.0
|
||||
|
||||
def test_metrics_range_validation(self, calculator):
|
||||
"""测试所有指标值都在[0, 1]范围内"""
|
||||
metrics = calculator.calculate_all(
|
||||
question="随机问题" * 10,
|
||||
retrieved_contexts=["ctx1", "ctx2", "ctx3", "ctx4", "ctx5"],
|
||||
generated_answer="这是一个关于计算机科学的详细回答,涵盖了多个技术领域",
|
||||
ground_truth_contexts=["ctx1", "ctx2", "ctx3"],
|
||||
ground_truth_answer="计算机科学包括算法、数据结构、编程等内容"
|
||||
)
|
||||
|
||||
for metric_name, value in metrics.items():
|
||||
assert 0.0 <= value <= 1.0, \
|
||||
f"Metric {metric_name} = {value} is not in [0, 1] range"
|
||||
|
||||
def test_consistent_retrieval_metrics(self, calculator):
|
||||
"""测试检索指标的一致性"""
|
||||
# 当检索结果和ground_truth完全相同时
|
||||
contexts = ["ctx1", "ctx2", "ctx3", "ctx4", "ctx5"]
|
||||
|
||||
metrics = calculator.calculate_all(
|
||||
question="问题",
|
||||
retrieved_contexts=contexts,
|
||||
generated_answer="答案",
|
||||
ground_truth_contexts=contexts,
|
||||
ground_truth_answer="答案"
|
||||
)
|
||||
|
||||
# 所有检索指标应该都是1.0
|
||||
assert metrics['context_recall'] == 1.0
|
||||
assert metrics['context_precision'] == 1.0
|
||||
assert metrics['mrr'] == 1.0
|
||||
assert metrics['ndcg'] == 1.0
|
||||
Reference in new issue
Block a user