使用 LLM 评估 LLM 输出
了解 LLM 评审为何有效,以及与人工评估相比有哪些不足。
使用 LLM 评估 LLM 输出 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。
为什么使用 LLM 作为评审?
传统评估指标——BLEU、ROUGE、完全匹配——适用于结构化输出,但无法处理帮助性、准确性、语调和创造力等细腻特征。
人工评估能够捕捉细节,但速度慢且成本高。LLM 评审提供了一条中间道路:以低成本、大规模地进行自动评估,同时理解语义、上下文和主观质量。
LLM 评审为何有效
LLM 评审之所以有效,是因为它们与被评估模型具有相同的语言理解能力。它们可以评估:
- 回应是否事实准确,而不仅是与参考答案在词汇上相似
- 回应对于所述目的是否有帮助
- 语调是否符合要求
- 摘要是否涵盖关键要点
这些都是简单的字符串匹配指标无法衡量的特征。
简单的 LLM 评审
最基本的 LLM 评审方式是:让模型按照数字量表为回应评分,并给出简短的理由。这是所有更高级评审模式的基础。
import anthropic
import json
client = anthropic.Anthropic(api_key='sk-ant-...')
def simple_llm_judge(question, response, criterion):
judge_prompt = (
f'Rate the following response on {criterion} from 1 to 5.\n\n'
f'Question: {question}\n'
f'Response: {response}\n\n'
f'Return JSON: {{"score": <1-5>, "reason": "<one sentence>"}}'
)
r = client.messages.create(
model='claude-opus-4-5',
max_tokens=100,
messages=[{'role': 'user', 'content': judge_prompt}]
)
try:
result = json.loads(r.content[0].text)
return result['score'], result['reason']
except Exception:
return None, r.content[0].text
score, reason = simple_llm_judge(
question='What is recursion in programming?',
response='Recursion is when a function calls itself.',
criterion='clarity and completeness'
)
print(f'Score: {score}/5 — {reason}')LLM 评审为何失败:位置偏差
位置偏差:当同时呈现两个回应(A 和 B)时,LLM 评审倾向于选择先出现的回应,而不考虑质量。研究表明,使用简单的评审提示时,这种偏差会影响 60% 至 70% 的成对比较。
这意味着您呈现选项的顺序会改变评审结论。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
def demonstrate_position_bias(question, response_a, response_b):
def ask_judge(first, second, order):
prompt = (
f'Question: {question}\n\n'
f'Response 1: {first}\n\n'
f'Response 2: {second}\n\n'
f'Which response is better? Reply with 1 or 2.'
)
r = client.messages.create(
model='claude-opus-4-5',
max_tokens=10,
messages=[{'role': 'user', 'content': prompt}]
)
choice = r.content[0].text.strip()
# Map back to original labels
if order == 'AB':
return 'A' if choice == '1' else 'B'
else: # BA
return 'B' if choice == '1' else 'A'
result_ab = ask_judge(response_a, response_b, 'AB')
result_ba = ask_judge(response_b, response_a, 'BA')
print(f'Order A-B: Judge picked {result_ab}')
print(f'Order B-A: Judge picked {result_ba}')
if result_ab != result_ba:
print('Position bias detected: different results!')
return result_ab, result_baLLM 评审为何失败:冗长偏差
冗长偏差:LLM 评审往往会给更长、更详细的回应更高的评分,即使简洁的回应在客观上更好。一段用 400 个词表达本可用 50 个词表达的内容,往往比简洁版本获得更高的分数。
请明确指示评审降低不必要长度的评分,以减轻这种偏差。
import anthropic
client = anthropic.Anthropic(api_key='sk-ant-...')
def length_aware_judge(question, response):
judge_prompt = (
f'Evaluate this response for quality. Be aware of verbosity bias: '
f'do NOT score longer responses higher just because they are longer.\n\n'
f'Question: {question}\n'
f'Response: {response}\n\n'
f'Evaluate on:\n'
f'1. Accuracy (does it correctly answer the question?)\n'
f'2. Conciseness (does it avoid unnecessary filler?)\n'
f'3. Helpfulness (does it serve the user well?)\n\n'
f'Penalize responses that add filler, repetition, or irrelevant information.\n'
f'Score each 1-5 and provide an overall score. Return JSON.'
)
r = client.messages.create(
model='claude-opus-4-5',
max_tokens=200,
messages=[{'role': 'user', 'content': judge_prompt}]
)
print(r.content[0].text)LLM 评审为何失败:自我偏好
自我偏好偏差:当克劳德评审两个回应时,往往更偏好具有克劳德风格的回应。当 GPT-4 评审时,则更偏好具有 GPT-4 风格的回应。这是一种会影响所有 LLM 评审的系统性偏差。
缓解方法:使用多个不同的模型作为评审,并汇总它们的分数。出现分歧说明这是一个需要人工审查的边界案例。
import anthropic
import openai
anthropic_client = anthropic.Anthropic(api_key='sk-ant-...')
openai_client = openai.OpenAI(api_key='sk-...')
def multi_model_judge(question, response):
judge_prompt = (
f'Rate this response 1-10 for overall quality.\n'
f'Q: {question}\nA: {response}\n'
f'Reply with only a number.'
)
# Judge 1: Claude
r_claude = anthropic_client.messages.create(
model='claude-opus-4-5',
max_tokens=10,
messages=[{'role': 'user', 'content': judge_prompt}]
)
score_claude = float(r_claude.content[0].text.strip())
# Judge 2: GPT-4o
r_gpt = openai_client.chat.completions.create(
model='gpt-4o',
max_tokens=10,
messages=[{'role': 'user', 'content': judge_prompt}]
)
score_gpt = float(r_gpt.choices[0].message.content.strip())
avg = (score_claude + score_gpt) / 2
print(f'Claude judge: {score_claude}, GPT judge: {score_gpt}, Average: {avg}')
if abs(score_claude - score_gpt) > 2:
print('WARNING: High disagreement — consider human review')
return avg评分膨胀
评分膨胀:LLM 评审往往会给大多数回应较高的分数(满分 5 分中的 4 至 5 分),压缩分数分布,使优秀与卓越难以区分。原本应得 5 分制 3 分的回应,通常会得到 4 至 4.5 分。
解决方法:使用能够强制校准的评分标准,或使用相对评分(成对比较)而非绝对评分。
# Anti-inflation judge prompt with explicit score anchors
CALIBRATED_JUDGE_PROMPT = (
'Rate this response 1-5 using these STRICT score definitions:\n'
'1 = Completely wrong, harmful, or completely off-topic\n'
'2 = Partially relevant but contains significant errors or omissions\n'
'3 = Correct and addresses the question but lacks depth or precision\n'
'4 = Correct, reasonably complete, and clearly expressed\n'
'5 = Exceptional: correct, complete, insightful, and concise\n\n'
'Only give 5 if the response is genuinely outstanding.\n'
'Give 3 for any adequate-but-not-impressive response.\n\n'
'Question: {question}\n'
'Response: {response}\n\n'
'Score (1-5) and one-sentence reason:'
)
# Compare to non-anchored prompt which tends to cluster at 4-5
print('Anchored rubrics force the judge to use the full scale')LLM 评审最适用的情况
在以下情况下,LLM 评审最可靠:
- 评估标准清晰且定义明确
- 回应质量差异较大(明显优秀与明显糟糕)
- 领域属于评审模型的知识范围
- 评估的是语调、帮助性等主观特征,而人工评审者对此也存在分歧
在评估最新知识、高度技术化的领域,或需要专业知识才能发现的细微事实错误时,LLM 评审最不可靠。
必须进行人工评估的情况
请在以下情况下保留人工参与:
- 评估专业领域(医疗、法律、安全)中的回应
- 为 LLM 评审建立真实基准校准
- 涉及高风险的决策,因为 LLM 评审的错误可能造成实际后果
- 评审模型几乎没有训练信号的新任务
- 检测需要领域专业知识才能发现的细微事实错误
def triage_for_human_review(question, response, llm_score, confidence_threshold=0.7):
"""
Route low-confidence or high-stakes evaluations to human review.
"""
# Route to human if judge is uncertain
if llm_score is None:
return 'human_review', 'LLM judge failed to produce a score'
# Route to human for borderline scores (near decision boundaries)
if 2.5 <= llm_score <= 3.5:
return 'human_review', f'Borderline score {llm_score} — needs human judgment'
# Route to human for domain-specific high-risk content
HIGH_RISK_KEYWORDS = ['medication', 'legal advice', 'financial advice', 'security']
if any(kw in question.lower() for kw in HIGH_RISK_KEYWORDS):
return 'human_review', 'High-risk domain — human verification required'
# Else: LLM score is sufficient
return 'auto_accept', f'Score {llm_score} — LLM judgment sufficient'
routing, reason = triage_for_human_review('What medication should I take?', 'Take aspirin.', 4.5)
print(f'{routing}: {reason}')构建评估流程
实用的 LLM 评审流程如下:生成回应 → 运行 LLM 评审 → 将边界案例分流给人工 → 汇总分数 → 报告质量指标。请记录所有内容,以便形成审计轨迹。
import anthropic
import json
client = anthropic.Anthropic(api_key='sk-ant-...')
def evaluate_batch(examples, product_under_test, criteria):
results = []
for ex in examples:
response = product_under_test(ex['question'])
score, reason = simple_llm_judge(ex['question'], response, criteria)
results.append({
'question': ex['question'],
'response': response,
'score': score,
'reason': reason,
'needs_review': score is None or 2.5 <= (score or 0) <= 3.5
})
# Summarize
valid_scores = [r['score'] for r in results if r['score'] is not None]
avg_score = sum(valid_scores) / len(valid_scores) if valid_scores else 0
review_count = sum(1 for r in results if r['needs_review'])
print(f'Average score: {avg_score:.2f}/5')
print(f'Cases needing review: {review_count}/{len(results)}')
return results, avg_score
# results, avg = evaluate_batch(test_cases, my_product, 'helpfulness')无参考答案评估与基于参考答案的评估
LLM 评审可以采用两种模式:
- 基于参考答案:评审将回应与已知正确答案进行比较。准确性较高,但需要带标签的数据。
- 无参考答案:评审根据回应本身的优点进行评估(是否一致?是否有帮助?是否写得好?)。灵活性更高,但在事实准确性方面不够精确。
如果您有金标准答案,请使用基于参考答案的评估。对于摘要、语调评估或创作质量等开放式任务,请使用无参考答案的评估。
知识检查:位置偏差
在 LLM 作为评审的评估中,什么是位置偏差?它会造成什么影响?
回顾:LLM 评审评估
LLM 评审模型能够理解细微差异、语义准确性和主观质量——这些都是传统指标无法衡量的内容。它们在以下方面会失效:位置偏差(偏好第一个选项)、冗长偏差(偏好较长的回答)、自我偏好(偏好自身的风格)以及评分膨胀(分数集中在 5 分制的 4—5 分)。可以通过随机调整回答顺序、明确要求避免冗长、使用定义每个分数等级的锚定评分标准,以及使用多个不同模型作为评审模型来缓解这些问题。对于临界分数和高风险领域,请交由人工评估者处理。请利用 LLM 处理大规模评估,利用人工评估者进行校准和高风险决策。
用 AI 导师学习 AI Prompt Engineering — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 53
- 课程
- 199
常见问题解答
「使用 LLM 评估 LLM 输出」课时是免费的吗?
是的 — 「使用 LLM 评估 LLM 输出」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。
「使用 LLM 评估 LLM 输出」这节课中我会学到什么?
了解 LLM 评审为何有效,以及与人工评估相比有哪些不足。 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Prompt Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「使用 LLM 评估 LLM 输出」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Prompt Engineering 课中编写并运行代码吗?
能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 LLM 评估 LLM 输出
- 基于评分标准的评分提示词
- 比较式评审:A 与 B
- LLM 评审中的校准与偏差