0Pricing
AI Engineering Academy · 课时

根据人工评估校准评判模型

收集人类偏好判断的真实标签数据集,使用 Cohen's Kappa 衡量评审者与人类的一致性,并通过提示调优减少系统性偏差。

根据人工评估校准评判模型 是 CoddyKit 上的免费 AI Engineering Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Engineering Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Engineering Academy 课程共包含 4 节课。

为什么校准不可或缺

未经校准的 LLM 评判器可能系统性地给出高于或低于人类的分数,偏好某种特定的写作风格,或无法处理评分标准未预料到的边界情况。如果使用这样的评判器做出部署决策,您实际上是在信任一个有偏差的测量工具。校准会将评判器与人类真实标准进行验证,并量化其分数在生产环境中使用前的可信程度。

构建校准数据集

请创建一个包含 100–500 个示例回答的校准集,并确保这些回答已经由人类评分。请注意多样性:包含高质量回答(5 分)、中等质量回答(3 分)和明显较差的回答(1 分)。让 3–5 名相互独立的人类评分者为每个示例评分,以衡量评分者间的一致性,并通过取平均值或众数计算共识真实分数。

# Calibration dataset structure:
# [
#   {
#     'question': 'What causes inflation?',
#     'response': '...',
#     'human_scores': [4, 4, 3, 5, 4],  # 5 raters
#     'human_consensus': 4,              # mean or mode
#     'rater_ids': ['r1', 'r2', 'r3', 'r4', 'r5']
#   },
#   ...
# ]

# Typical calibration set composition:
# 30% excellent responses (score 4-5)
# 40% adequate responses (score 2-4)
# 30% poor responses (score 1-2)
# Include adversarial / edge cases

衡量评分者间的一致性

在将人类分数视为真实标准前,请先确认人类评分者之间达成了一致。科恩 κ用于衡量两名评分者之间超出随机水平的一致性:高于 0.6 表示可接受,高于 0.8 表示优秀。对于 5 分制量表,请计算加权 κ(使用线性权重)。评分者间一致性较低,意味着任务定义不够明确——请先改进评分者指南,再使用这些分数校准评判器。

from sklearn.metrics import cohen_kappa_score
import numpy as np

def measure_inter_rater_agreement(rater1_scores: list, rater2_scores: list) -> dict:
    kappa = cohen_kappa_score(rater1_scores, rater2_scores, weights='linear')
    exact_agreement = sum(a == b for a, b in zip(rater1_scores, rater2_scores)) / len(rater1_scores)
    adjacent_agreement = sum(abs(a - b) <= 1 for a, b in zip(rater1_scores, rater2_scores)) / len(rater1_scores)
    return {
        'weighted_kappa': round(kappa, 3),
        'exact_agreement': round(exact_agreement, 3),
        'adjacent_agreement': round(adjacent_agreement, 3),
        'acceptable': kappa >= 0.6
    }

在校准数据上运行评判器

请使用计划在生产环境中采用的同一提示,让 LLM 评判器处理校准集中的每个示例。为每个示例收集评判器分数以及人类共识分数。请让这两组列表保持对齐,以便逐项比较。这种成对比较可以揭示系统性偏差和分数范围差异。

async def run_judge_on_calibration(calibration_set: list) -> list:
    pairs = []
    for item in calibration_set:
        judge_result = await async_judge(item['question'], item['response'])
        pairs.append({
            'question': item['question'],
            'human': item['human_consensus'],
            'judge': judge_result.correctness,
            'judge_rationale': judge_result.rationale
        })
    return pairs

计算评判器与人类评分的相关性

请计算评判器分数与人类共识分数之间的皮尔逊相关性。它用于衡量线性一致性:相关性为 1.0 表示评判器能够完美追踪人类排序。同时计算平均绝对误差(MAE),以了解平均分数差距。在 5 分制量表中,相关性高于 0.7 且 MAE 低于 0.8 分,通常表明评判器已经足够可靠,可以用于生产环境。

from scipy.stats import pearsonr, spearmanr
import numpy as np

def calibration_metrics(pairs: list) -> dict:
    humans = [p['human'] for p in pairs]
    judges = [p['judge'] for p in pairs]
    pearson_r, p_val = pearsonr(humans, judges)
    spearman_r, _ = spearmanr(humans, judges)
    mae = np.mean([abs(h - j) for h, j in zip(humans, judges)])
    return {
        'pearson_r': round(pearson_r, 3),
        'spearman_r': round(spearman_r, 3),
        'p_value': round(p_val, 5),
        'mae': round(mae, 3),
        'reliable': pearson_r >= 0.7 and mae <= 0.8
    }

识别系统性偏差

除了相关性之外,还要查找系统性偏差:评判器是否持续给出过高或过低的分数?绘制评判器分数与人类分数的关系图,并观察回归线是否未经过原点。如果人类平均给出 3 分时,评判器却给出 4 分,则说明它存在分数膨胀偏差。可以通过调整评分标准,或对原始分数应用线性校准变换来纠正这种偏差。

import numpy as np
from scipy import stats

def detect_score_bias(pairs: list) -> dict:
    humans = np.array([p['human'] for p in pairs])
    judges = np.array([p['judge'] for p in pairs])
    slope, intercept, r, p, se = stats.linregress(judges, humans)
    bias = np.mean(judges - humans)  # positive = judge over-scores
    return {
        'mean_bias': round(bias, 3),  # > 0 means judge inflates
        'calibration_slope': round(slope, 3),
        'calibration_intercept': round(intercept, 3),
        # Corrected score = slope * judge_score + intercept
        'correction_formula': f'human_score ≈ {slope:.2f} * judge + {intercept:.2f}'
    }

应用校准修正

获得校准回归的斜率和截距后,请应用它们,将原始评判器分数转换为更符合人类判断的校准分数。这种线性修正简单而有效。请将修正后的分数限制在有效范围(1–5)内,以防止回归产生超出范围的值。同时保存原始分数和修正后的分数,以便随着校准数据增加进行回溯比较。

def calibrate_score(raw_judge_score: float, slope: float, intercept: float,
                    min_score: int = 1, max_score: int = 5) -> float:
    corrected = slope * raw_judge_score + intercept
    return max(min_score, min(max_score, round(corrected, 1)))

# Example: if judge inflates by 0.5 points on average
# slope=0.85, intercept=0.3
# raw_score=4 -> corrected = 0.85*4 + 0.3 = 3.7

发现系统性错误模式

请按照问题类型、回答长度和主题领域对校准误差进行分组,以发现有规律的失效模式。评判器可能不擅长处理技术性编程问题,却能准确处理事实性问题。它可能对很长的回答评分过高,却对简短精炼的回答评分过低。这些模式有助于针对性改进评分标准,或为校准效果最弱的领域设计特定主题的评判提示。

from collections import defaultdict

def error_by_category(pairs: list) -> dict:
    by_cat = defaultdict(list)
    for p in pairs:
        error = abs(p['judge'] - p['human'])
        by_cat[p.get('category', 'unknown')].append(error)
    return {
        cat: {
            'mean_error': round(sum(errs)/len(errs), 2),
            'max_error': max(errs),
            'n': len(errs)
        }
        for cat, errs in by_cat.items()
    }

随着数据增长重新校准

校准不是一次性工作。随着收集到更多人类评分,以及模型更新改变评判器的行为,请每季度重新运行校准。持续跟踪校准指标——如果皮尔逊相关性降至 0.7 以下,请调查模型更新是否改变了评判器的评分分布。请自动化校准流程,使重新运行只需执行一条命令即可生成更新后的修正系数。

def run_calibration_pipeline(calibration_data: list) -> dict:
    # 1. Measure human inter-rater agreement
    ira = measure_inter_rater_agreement(
        [d['rater_1'] for d in calibration_data],
        [d['rater_2'] for d in calibration_data]
    )
    # 2. Run judge on all items
    pairs = run_judge_on_calibration(calibration_data)
    # 3. Compute correlation metrics
    metrics = calibration_metrics(pairs)
    # 4. Detect bias
    bias = detect_score_bias(pairs)
    return {'ira': ira, 'metrics': metrics, 'bias': bias}

记录评判器配置

请将评判模型、提示版本、校准数据集的规模和日期、校准指标以及所有修正系数记录在一个纳入版本控制的评判器配置文件中。这样可以建立审计轨迹,让未来的团队成员了解分数为何呈现当前形式,也能帮助您判断指标变化究竟源于评判器重新配置,还是真实质量发生了变化。

# judge_config.yaml
# judge_model: gpt-4o-2025-01-01
# judge_prompt_version: v3.2
# calibration_date: 2026-05-15
# calibration_set_size: 312
# calibration_metrics:
#   pearson_r: 0.81
#   spearman_r: 0.79
#   mae: 0.54
# bias_correction:
#   slope: 0.88
#   intercept: 0.45
# next_recalibration: 2026-08-15

针对特定维度的评判提示

一个同时对多个维度评分的评判提示,通常会产生彼此相关的分数——评判器因为给出了较高的清晰度分数,也给出较高的正确性分数,这是受第一印象锚定的影响。针对特定维度的提示会通过单独的 API 调用分别评估每项标准。虽然成本更高,但这样产生的分数更可靠,也是真正相互独立的测量结果。对于预期分数会独立变化的维度,请使用不同的提示。

async def multi_dimension_judge(question: str, answer: str) -> dict:
    # Run each dimension as a separate judge call
    correctness, helpfulness, clarity = await asyncio.gather(
        judge_single_dimension(question, answer, 'correctness'),
        judge_single_dimension(question, answer, 'helpfulness'),
        judge_single_dimension(question, answer, 'clarity')
    )
    return {
        'correctness': correctness,
        'helpfulness': helpfulness,
        'clarity': clarity,
        'composite': 0.5 * correctness + 0.3 * helpfulness + 0.2 * clarity
    }

快速检查

测试您对根据人类评分校准 LLM 评判模型的理解。

课程回顾

本课中您学到了:包含人类共识分数的校准数据集为衡量评判器可靠性提供真实标准;皮尔逊相关性和 MAE可以量化评判器对人类判断的追踪程度;线性偏差修正可以调整系统性的过高或过低评分。接下来,我们将构建与持续集成/持续交付集成的持续评估流程。

常见问题解答

「根据人工评估校准评判模型」课时是免费的吗?

是的 — 「根据人工评估校准评判模型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Engineering Academy 课程的其余内容,请升级到 CoddyKit PRO。 AI Engineering Academy 课程共包含 4 节课。

「根据人工评估校准评判模型」这节课中我会学到什么?

收集人类偏好判断的真实标签数据集,使用 Cohen's Kappa 衡量评审者与人类的一致性,并通过提示调优减少系统性偏差。 你通过在浏览器中直接运行的动手代码来练习 AI Engineering Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 AI Engineering Academy 需要有经验吗?

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

「根据人工评估校准评判模型」课时需要多长时间?

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

我能在这节 AI Engineering Academy 课中编写并运行代码吗?

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

此课程中的所有课时

  1. LLM 作为评判者模式
  2. 逐项评估与成对评估
  3. 根据人工评估校准评判模型
  4. 构建持续评估流程
← 返回 AI Engineering Academy