327 lines
10 KiB
Python
327 lines
10 KiB
Python
"""
|
|||
|
|
Tests for report aggregator
|
||
|
|
"""
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
from rag_evaluation.report.aggregator import (
|
||
|
|
MetricStats,
|
||
|
|
EvaluationSummary,
|
||
|
|
ResultAggregator
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def sample_results():
|
||
|
|
"""创建示例评估结果"""
|
||
|
|
return [
|
||
|
|
{
|
||
|
|
'question_index': 0,
|
||
|
|
'question_text': 'What is machine learning?',
|
||
|
|
'metrics': {
|
||
|
|
'context_recall': 0.8,
|
||
|
|
'context_precision': 0.9,
|
||
|
|
'mrr': 0.85,
|
||
|
|
'ndcg': 0.88,
|
||
|
|
'faithfulness': 0.9,
|
||
|
|
'answer_relevancy': 0.85,
|
||
|
|
'correctness': 0.8
|
||
|
|
},
|
||
|
|
'retrieval_time_ms': 100,
|
||
|
|
'generation_time_ms': 200,
|
||
|
|
'metadata': {'source': 'ml_basics.txt'}
|
||
|
|
},
|
||
|
|
{
|
||
|
|
'question_index': 1,
|
||
|
|
'question_text': 'What is deep learning?',
|
||
|
|
'metrics': {
|
||
|
|
'context_recall': 0.7,
|
||
|
|
'context_precision': 0.75,
|
||
|
|
'mrr': 0.7,
|
||
|
|
'ndcg': 0.72,
|
||
|
|
'faithfulness': 0.85,
|
||
|
|
'answer_relevancy': 0.8,
|
||
|
|
'correctness': 0.75
|
||
|
|
},
|
||
|
|
'retrieval_time_ms': 120,
|
||
|
|
'generation_time_ms': 180,
|
||
|
|
'metadata': {'source': 'dl_intro.txt'}
|
||
|
|
},
|
||
|
|
{
|
||
|
|
'question_index': 2,
|
||
|
|
'question_text': 'How to use Python lists?',
|
||
|
|
'metrics': {
|
||
|
|
'context_recall': 0.9,
|
||
|
|
'context_precision': 0.95,
|
||
|
|
'mrr': 0.9,
|
||
|
|
'ndcg': 0.92,
|
||
|
|
'faithfulness': 0.95,
|
||
|
|
'answer_relevancy': 0.9,
|
||
|
|
'correctness': 0.9
|
||
|
|
},
|
||
|
|
'retrieval_time_ms': 90,
|
||
|
|
'generation_time_ms': 150,
|
||
|
|
'metadata': {'source': 'python_guide.md'}
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def aggregator():
|
||
|
|
"""创建聚合器实例"""
|
||
|
|
return ResultAggregator()
|
||
|
|
|
||
|
|
|
||
|
|
class TestResultAggregator:
|
||
|
|
"""测试结果聚合器"""
|
||
|
|
|
||
|
|
def test_aggregate_results_basic(self, aggregator, sample_results):
|
||
|
|
"""测试基本聚合功能"""
|
||
|
|
summary = aggregator.aggregate_results(
|
||
|
|
evaluation_id='eval-001',
|
||
|
|
knowledge_base_id='kb-001',
|
||
|
|
testset_id='ts-001',
|
||
|
|
results=sample_results
|
||
|
|
)
|
||
|
|
|
||
|
|
assert summary.evaluation_id == 'eval-001'
|
||
|
|
assert summary.knowledge_base_id == 'kb-001'
|
||
|
|
assert summary.testset_id == 'ts-001'
|
||
|
|
assert summary.total_questions == 3
|
||
|
|
assert summary.successful_questions == 3
|
||
|
|
assert summary.failed_questions == 0
|
||
|
|
assert 0 <= summary.overall_score <= 100
|
||
|
|
|
||
|
|
def test_aggregate_empty_results(self, aggregator):
|
||
|
|
"""测试空结果列表"""
|
||
|
|
with pytest.raises(ValueError, match="cannot be empty"):
|
||
|
|
aggregator.aggregate_results(
|
||
|
|
evaluation_id='eval-001',
|
||
|
|
knowledge_base_id='kb-001',
|
||
|
|
testset_id='ts-001',
|
||
|
|
results=[]
|
||
|
|
)
|
||
|
|
|
||
|
|
def test_metric_stats_calculation(self, aggregator, sample_results):
|
||
|
|
"""测试指标统计计算"""
|
||
|
|
summary = aggregator.aggregate_results(
|
||
|
|
evaluation_id='eval-001',
|
||
|
|
knowledge_base_id='kb-001',
|
||
|
|
testset_id='ts-001',
|
||
|
|
results=sample_results
|
||
|
|
)
|
||
|
|
|
||
|
|
# 验证所有指标都被计算
|
||
|
|
expected_metrics = [
|
||
|
|
'context_recall', 'context_precision', 'mrr', 'ndcg',
|
||
|
|
'faithfulness', 'answer_relevancy', 'correctness'
|
||
|
|
]
|
||
|
|
|
||
|
|
for metric in expected_metrics:
|
||
|
|
assert metric in summary.metric_stats
|
||
|
|
stats = summary.metric_stats[metric]
|
||
|
|
assert isinstance(stats, MetricStats)
|
||
|
|
assert stats.count == 3
|
||
|
|
assert 0 <= stats.mean <= 1
|
||
|
|
assert 0 <= stats.median <= 1
|
||
|
|
assert stats.min <= stats.mean <= stats.max
|
||
|
|
|
||
|
|
def test_overall_score_calculation(self, aggregator, sample_results):
|
||
|
|
"""测试整体评分计算"""
|
||
|
|
summary = aggregator.aggregate_results(
|
||
|
|
evaluation_id='eval-001',
|
||
|
|
knowledge_base_id='kb-001',
|
||
|
|
testset_id='ts-001',
|
||
|
|
results=sample_results
|
||
|
|
)
|
||
|
|
|
||
|
|
# 整体评分应该在合理范围内
|
||
|
|
assert 70 <= summary.overall_score <= 95
|
||
|
|
# 应该是检索和生成指标的加权平均
|
||
|
|
assert isinstance(summary.overall_score, float)
|
||
|
|
|
||
|
|
def test_pass_rate_calculation(self, aggregator, sample_results):
|
||
|
|
"""测试通过率计算"""
|
||
|
|
summary = aggregator.aggregate_results(
|
||
|
|
evaluation_id='eval-001',
|
||
|
|
knowledge_base_id='kb-001',
|
||
|
|
testset_id='ts-001',
|
||
|
|
results=sample_results
|
||
|
|
)
|
||
|
|
|
||
|
|
# 所有结果都应该通过(值都高于阈值)
|
||
|
|
for metric_name, stats in summary.metric_stats.items():
|
||
|
|
assert 0 <= stats.pass_rate <= 1
|
||
|
|
# 大部分应该通过
|
||
|
|
assert stats.pass_rate >= 0.66 # 至少2/3通过
|
||
|
|
|
||
|
|
def test_average_time_calculation(self, aggregator, sample_results):
|
||
|
|
"""测试平均时间计算"""
|
||
|
|
summary = aggregator.aggregate_results(
|
||
|
|
evaluation_id='eval-001',
|
||
|
|
knowledge_base_id='kb-001',
|
||
|
|
testset_id='ts-001',
|
||
|
|
results=sample_results
|
||
|
|
)
|
||
|
|
|
||
|
|
# 验证平均时间
|
||
|
|
expected_retrieval = (100 + 120 + 90) / 3
|
||
|
|
expected_generation = (200 + 180 + 150) / 3
|
||
|
|
|
||
|
|
assert abs(summary.avg_retrieval_time_ms - expected_retrieval) < 0.1
|
||
|
|
assert abs(summary.avg_generation_time_ms - expected_generation) < 0.1
|
||
|
|
|
||
|
|
def test_aggregate_with_failed_results(self, aggregator):
|
||
|
|
"""测试包含失败结果的聚合"""
|
||
|
|
results = [
|
||
|
|
{
|
||
|
|
'question_index': 0,
|
||
|
|
'metrics': {'context_recall': 0.8, 'faithfulness': 0.9}
|
||
|
|
},
|
||
|
|
{
|
||
|
|
'question_index': 1,
|
||
|
|
'metrics': None, # 失败的评估
|
||
|
|
'error': 'Timeout'
|
||
|
|
},
|
||
|
|
{
|
||
|
|
'question_index': 2,
|
||
|
|
'metrics': {'context_recall': 0.7, 'faithfulness': 0.85}
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
summary = aggregator.aggregate_results(
|
||
|
|
evaluation_id='eval-001',
|
||
|
|
knowledge_base_id='kb-001',
|
||
|
|
testset_id='ts-001',
|
||
|
|
results=results
|
||
|
|
)
|
||
|
|
|
||
|
|
assert summary.total_questions == 3
|
||
|
|
assert summary.successful_questions == 2
|
||
|
|
assert summary.failed_questions == 1
|
||
|
|
|
||
|
|
|
||
|
|
class TestGroupBySource:
|
||
|
|
"""测试按来源分组"""
|
||
|
|
|
||
|
|
def test_group_by_source(self, aggregator, sample_results):
|
||
|
|
"""测试按来源文件分组"""
|
||
|
|
grouped = aggregator.group_by_source(sample_results)
|
||
|
|
|
||
|
|
assert len(grouped) == 3
|
||
|
|
assert 'ml_basics.txt' in grouped
|
||
|
|
assert 'dl_intro.txt' in grouped
|
||
|
|
assert 'python_guide.md' in grouped
|
||
|
|
|
||
|
|
assert len(grouped['ml_basics.txt']) == 1
|
||
|
|
assert len(grouped['dl_intro.txt']) == 1
|
||
|
|
assert len(grouped['python_guide.md']) == 1
|
||
|
|
|
||
|
|
def test_group_by_source_unknown(self, aggregator):
|
||
|
|
"""测试没有来源信息的分组"""
|
||
|
|
results = [
|
||
|
|
{'question_index': 0, 'metrics': {}},
|
||
|
|
{'question_index': 1, 'metrics': {}}
|
||
|
|
]
|
||
|
|
|
||
|
|
grouped = aggregator.group_by_source(results)
|
||
|
|
|
||
|
|
assert 'unknown' in grouped
|
||
|
|
assert len(grouped['unknown']) == 2
|
||
|
|
|
||
|
|
|
||
|
|
class TestFailedCases:
|
||
|
|
"""测试失败案例识别"""
|
||
|
|
|
||
|
|
def test_identify_completely_failed_cases(self, aggregator):
|
||
|
|
"""测试识别完全失败的案例"""
|
||
|
|
results = [
|
||
|
|
{
|
||
|
|
'question_index': 0,
|
||
|
|
'question_text': 'Test question',
|
||
|
|
'metrics': None,
|
||
|
|
'error': 'Timeout error'
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
failed = aggregator.identify_failed_cases(results)
|
||
|
|
|
||
|
|
assert len(failed) == 1
|
||
|
|
assert failed[0]['reason'] == 'evaluation_failed'
|
||
|
|
assert failed[0]['error'] == 'Timeout error'
|
||
|
|
|
||
|
|
def test_identify_low_metric_cases(self, aggregator):
|
||
|
|
"""测试识别低指标案例"""
|
||
|
|
results = [
|
||
|
|
{
|
||
|
|
'question_index': 0,
|
||
|
|
'question_text': 'Test question',
|
||
|
|
'metrics': {
|
||
|
|
'context_recall': 0.3, # 低于阈值
|
||
|
|
'faithfulness': 0.9
|
||
|
|
},
|
||
|
|
'retrieved_contexts': ['ctx1'],
|
||
|
|
'generated_answer': 'answer',
|
||
|
|
'ground_truth_answer': 'truth'
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
failed = aggregator.identify_failed_cases(results, threshold=0.5)
|
||
|
|
|
||
|
|
assert len(failed) == 1
|
||
|
|
assert failed[0]['reason'] == 'low_metrics'
|
||
|
|
assert 'context_recall' in failed[0]['low_metrics']
|
||
|
|
assert failed[0]['low_metrics']['context_recall'] == 0.3
|
||
|
|
|
||
|
|
def test_no_failed_cases(self, aggregator, sample_results):
|
||
|
|
"""测试没有失败案例"""
|
||
|
|
failed = aggregator.identify_failed_cases(sample_results, threshold=0.5)
|
||
|
|
|
||
|
|
# 所有样本结果都应该通过
|
||
|
|
assert len(failed) == 0
|
||
|
|
|
||
|
|
|
||
|
|
class TestMetricDistribution:
|
||
|
|
"""测试指标分布计算"""
|
||
|
|
|
||
|
|
def test_calculate_distribution(self, aggregator, sample_results):
|
||
|
|
"""测试计算指标分布"""
|
||
|
|
dist = aggregator.calculate_metric_distribution(
|
||
|
|
sample_results,
|
||
|
|
'context_recall',
|
||
|
|
bins=10
|
||
|
|
)
|
||
|
|
|
||
|
|
assert dist['metric'] == 'context_recall'
|
||
|
|
assert dist['count'] == 3
|
||
|
|
assert len(dist['distribution']) == 10
|
||
|
|
|
||
|
|
# 验证分布总和为100%
|
||
|
|
total_percentage = sum(b['percentage'] for b in dist['distribution'])
|
||
|
|
assert abs(total_percentage - 100.0) < 0.1
|
||
|
|
|
||
|
|
def test_distribution_empty_results(self, aggregator):
|
||
|
|
"""测试空结果的分布"""
|
||
|
|
dist = aggregator.calculate_metric_distribution(
|
||
|
|
[],
|
||
|
|
'context_recall',
|
||
|
|
bins=10
|
||
|
|
)
|
||
|
|
|
||
|
|
assert dist['count'] == 0
|
||
|
|
assert dist['distribution'] == []
|
||
|
|
|
||
|
|
def test_distribution_bins(self, aggregator):
|
||
|
|
"""测试不同分箱数量"""
|
||
|
|
results = [
|
||
|
|
{'metrics': {'test_metric': 0.1}},
|
||
|
|
{'metrics': {'test_metric': 0.5}},
|
||
|
|
{'metrics': {'test_metric': 0.9}}
|
||
|
|
]
|
||
|
|
|
||
|
|
dist_5 = aggregator.calculate_metric_distribution(results, 'test_metric', bins=5)
|
||
|
|
dist_10 = aggregator.calculate_metric_distribution(results, 'test_metric', bins=10)
|
||
|
|
|
||
|
|
assert len(dist_5['distribution']) == 5
|
||
|
|
assert len(dist_10['distribution']) == 10
|