工程化05:评测体系

从离线测试集、线上抽检、人工标注到指标追踪,建立完整的AI应用评测体系。

字数 1492 阅读时长 ≈ 5 分钟 2026-7-19 2026-7-27
工程化05:评测体系

AI 应用的效果好坏不能凭感觉,需要建立一套完整的评测体系。从离线测试到线上评估,从自动指标到人工标注,全方位衡量模型效果。

评测维度

评测层次

层次说明方法
离线评测开发阶段的评估测试集、自动指标
线上评估上线后的真实评估A/B测试、用户反馈
人工评测高质量的主观评估标注平台、专家评审
业务指标最终业务效果转化率、留存率

评测指标

指标类型说明示例
质量指标输出内容的质量BLEU、ROUGE、人工评分
准确性指标回答的正确性准确率、召回率、F1
流畅性指标语言表达的自然度困惑度、流畅度评分
安全性指标内容的安全性有害内容比例
效率指标响应速度和成本延迟、Token消耗

离线测试集

测试集构建

步骤说明示例
需求收集收集业务需求用户常见问题
数据标注人工标注标准答案标注问题和回答
数据集划分划分训练/测试/验证集80%/10%/10%
数据质量检查标注质量标注一致性检查

测试集管理

@Service
public class TestDatasetService {
    private final List<TestSample> testSamples = new ArrayList<>();
    
    public void addSample(TestSample sample) {
        testSamples.add(sample);
    }
    
    public List<TestSample> getSamplesByCategory(String category) {
        return testSamples.stream()
            .filter(s -> s.getCategory().equals(category))
            .collect(Collectors.toList());
    }
    
    public List<TestSample> getRandomSamples(int count) {
        Collections.shuffle(testSamples);
        return testSamples.stream().limit(count).collect(Collectors.toList());
    }
    
    public int getSampleCount() {
        return testSamples.size();
    }
}

record TestSample(String id, String category, String prompt, 
                  String expectedAnswer, List<String> acceptableAnswers) {}

离线评测流程

1. 准备测试集 → 2. 模型生成回答 → 3. 自动评估 → 4. 分析结果 → 5. 迭代优化

自动评估指标

文本生成指标

指标说明适用场景
BLEU基于n-gram的相似度机器翻译、摘要生成
ROUGE基于召回率的评估文本摘要
METEOR考虑词干和同义词机器翻译
BERTScore基于语义相似度问答、对话
ChrF基于字符级别的评估拼写检查

问答评估指标

指标说明计算方式
准确率回答完全正确的比例correct / total
召回率正确回答覆盖的比例correct / relevant
F1准确率和召回率的调和平均2 * P * R / (P + R)
EM完全匹配率exact_match / total
MRR平均倒数排名mean(1/rank)

评估实现示例

@Service
public class EvaluationService {
    public EvaluationResult evaluate(List<TestSample> samples, String modelOutput) {
        int correct = 0;
        int total = samples.size();
        
        for (TestSample sample : samples) {
            if (isCorrect(sample, modelOutput)) {
                correct++;
            }
        }
        
        double accuracy = (double) correct / total;
        return new EvaluationResult(accuracy, correct, total);
    }
    
    private boolean isCorrect(TestSample sample, String output) {
        if (sample.expectedAnswer().equals(output)) {
            return true;
        }
        return sample.acceptableAnswers().stream()
            .anyMatch(a -> a.equals(output));
    }
    
    public double calculateBLEU(String reference, String candidate) {
        // BLEU 计算逻辑
        return 0.0;
    }
    
    public double calculateROUGE(String reference, String candidate) {
        // ROUGE 计算逻辑
        return 0.0;
    }
}

record EvaluationResult(double accuracy, int correct, int total) {}

线上抽检

抽检策略

策略说明适用场景
定时抽检固定时间间隔抽检持续监控
随机抽检随机抽取样本整体评估
异常抽检针对异常情况抽检问题排查
关键场景抽检针对关键场景重点保障

抽检实现

@Service
public class OnlineSamplingService {
    private final ScheduledExecutorService scheduler;
    private final double samplingRate = 0.1; // 10% 抽检率
    
    @PostConstruct
    public void startSampling() {
        scheduler.scheduleAtFixedRate(this::sampleAndEvaluate, 
            0, 10, TimeUnit.MINUTES);
    }
    
    private void sampleAndEvaluate() {
        List<OnlineSample> samples = fetchRandomSamples(samplingRate);
        for (OnlineSample sample : samples) {
            EvaluationResult result = evaluationService.evaluate(
                List.of(new TestSample(
                    sample.id(), 
                    sample.category(), 
                    sample.prompt(), 
                    sample.userFeedback(), 
                    List.of())),
                sample.modelOutput()
            );
            // 记录评估结果
            metricService.record("online.accuracy", result.accuracy());
        }
    }
    
    private List<OnlineSample> fetchRandomSamples(double rate) {
        // 从生产环境获取随机样本
        return List.of();
    }
}

record OnlineSample(String id, String category, String prompt, 
                    String modelOutput, String userFeedback) {}

线上指标监控

指标说明监控方式
实时准确率当前回答的准确率实时计算
错误率趋势错误率的变化趋势趋势图
用户满意度用户对回答的满意度评分收集
问题类型分布不同类型问题的比例分布图

人工标注

标注流程

1. 样本收集 → 2. 任务分配 → 3. 标注执行 → 4. 质量检查 → 5. 结果汇总

标注标准

维度标准评分范围
准确性回答是否正确1-5分
完整性回答是否完整1-5分
相关性回答是否相关1-5分
流畅性语言是否自然1-5分
安全性是否包含有害内容安全/不安全

标注质量控制

方法说明
标注指南提供详细的标注规范
示例标注提供标注示例
交叉验证多人标注同一样本
定期审核定期检查标注质量
标注培训对标注人员进行培训

A/B 测试

A/B 测试设计

步骤说明示例
目标设定明确测试目标提升回答准确率
假设设定设定测试假设新版本准确率更高
分组设计设计用户分组实验组/对照组
样本量计算确定所需样本量统计显著性
测试执行执行测试收集数据
结果分析分析测试结果统计检验

A/B 测试实现

@Service
public class ABTestService {
    private final Map<String, ABTest> activeTests = new ConcurrentHashMap<>();
    
    public String getVariant(String testId, String userId) {
        ABTest test = activeTests.get(testId);
        if (test == null) {
            return "control";
        }
        
        int hash = userId.hashCode();
        double normalized = Math.abs(hash % 1000) / 1000.0;
        
        double cumulative = 0;
        for (Map.Entry<String, Double> entry : test.variants().entrySet()) {
            cumulative += entry.getValue();
            if (normalized < cumulative) {
                return entry.getKey();
            }
        }
        return "control";
    }
    
    public void recordResult(String testId, String variant, boolean success) {
        ABTest test = activeTests.get(testId);
        if (test != null) {
            test.recordResult(variant, success);
        }
    }
    
    public ABTestResult getResult(String testId) {
        ABTest test = activeTests.get(testId);
        return test != null ? test.getResult() : null;
    }
}

record ABTest(String id, Map<String, Double> variants, 
              AtomicInteger[] counters) {}

A/B 测试统计

统计方法说明适用场景
Z检验比较两组比例差异大样本
t检验比较两组均值差异小样本
卡方检验比较分类数据分类变量
置信区间估计真实值范围结果报告

评测体系最佳实践

评测流程

离线评测 → A/B测试 → 线上评估 → 持续监控 → 迭代优化

评测工具链

工具用途示例
测试框架自动化测试JUnit、pytest
评估指标自动指标计算sacreBLEU、BERTScore
标注平台人工标注LabelStudio、众包平台
监控系统线上监控Prometheus、Grafana
统计分析数据分析pandas、scipy

评测频率

阶段频率说明
开发阶段每次提交自动化测试
发布前全量测试离线评测
灰度阶段实时监控A/B测试
全量阶段每日/每周定期评估

常见问题与解决方案

问题1:离线评测与线上效果不一致

表现:离线评测效果好,但线上效果差

解决方案

  • 检查测试集是否代表真实场景
  • 增加测试集多样性
  • 模拟线上环境进行测试

问题2:人工标注成本高

表现:人工标注需要大量人力

解决方案

  • 优先使用自动评估
  • 选择性标注关键样本
  • 使用众包平台降低成本

问题3:指标冲突

表现:不同指标给出不同结论

解决方案

  • 明确评估目标
  • 选择最重要的指标
  • 综合考虑多个指标

问题4:评测结果不稳定

表现:评测结果波动大

解决方案

  • 增加测试样本量
  • 固定评测参数
  • 使用统计检验

项目判断清单

  • 需要评估模型效果 → 建立离线测试集
  • 需要上线前验证 → 执行离线评测
  • 需要对比不同方案 → 实施 A/B 测试
  • 需要持续监控效果 → 建立线上抽检
  • 需要高质量评估 → 实施人工标注
  • 需要衡量业务价值 → 追踪业务指标
  • 需要迭代优化 → 建立评测反馈闭环
  • 需要保障效果稳定 → 建立持续评测体系