← 返回首页 📖 📖 Wiki知识库
📐 学习维度 本篇是 📖 📖 Wiki知识库 🏠 首页 🛤️ 学习路径 📚 全部文章

AI Agent 端到端测试与 E2E 验证工程化深度实战 - 五、LLM-as-Judge 评估实战

本文是 AI Agent 端到端测试与 E2E 验证工程化深度实战 的分章阅读版。

五、LLM-as-Judge 评估实战

5.1 什么时候用 LLM-as-Judge

Trajectory 测不了的:

用 LLM-as-Judge 的核心:用一个 LLM(Claude / GPT)评估另一个 LLM 的输出。

5.2 LLM-as-Judge 三种模式

模式 1:Rubric Scoring(打分)


public class LlmJudge {

    private static final String RUBRIC = """
        请对以下客服回答进行评分(1-5):
        - 准确性:是否基于检索到的资料,未编造
        - 礼貌性:语气是否专业、有同理心
        - 完整性:是否完整回答了用户问题
        - 简洁性:是否冗长
        - 行动性:是否给出下一步建议
        
        用户问题:{question}
        客服回答:{answer}
        参考资料:{context}
        
        输出 JSON:{"accuracy":1-5, "politeness":1-5, "completeness":1-5,
                    "conciseness":1-5, "actionability":1-5, "reason":"..."}
        """;

    public JudgeResult judge(String question, String answer, String context) {
        String prompt = RUBRIC
            .replace("{question}", question)
            .replace("{answer}", answer)
            .replace("{context}", context);
        return judgeChatClient.prompt().user(prompt).call().entity(JudgeResult.class);
    }
}

模式 2:Pairwise Comparison(两两对比)

适合 A/B 实验:让 LLM 在两个回答中选更好的。


public PairwiseResult compare(String question, String answerA, String answerB) {
    String prompt = """
        用户问题:%s
        回答 A:%s
        回答 B:%s
        
        哪个更好?输出 JSON:{"winner":"A"|"B"|"tie","reason":"..."}
        """.formatted(question, answerA, answerB);
    return judgeChatClient.prompt().user(prompt).call().entity(PairwiseResult.class);
}

模式 3:Reference Matching(与参考答案比对)


public MatchResult match(String expected, String actual) {
    String prompt = """
        参考答案:%s
        实际答案:%s
        
        实际答案与参考答案语义一致吗?
        输出 JSON:{"match":true|false,"confidence":0.0-1.0,"reason":"..."}
        """.formatted(expected, actual);
    return judgeChatClient.prompt().user(prompt).call().entity(MatchResult.class);
}

5.3 LLM-as-Judge 的工程化要点

要点 1:Judge 模型 ≠ 被测模型

不能用 GPT-4 评 GPT-4(自夸偏差)。建议:

要点 2:Judge Prompt 要严格


// ❌ 模糊 prompt
String badPrompt = "评价这个回答好不好:" + answer;

// ✅ 严格 rubric
String goodPrompt = """
    你是一名严格的客服质检员。请按以下标准评分:
    1. 准确性(1-5):5=完全基于资料,1=严重编造
    2. 礼貌性(1-5):5=专业同理心,1=生硬冷漠
    
    严格按 JSON 格式输出,禁止 markdown。
    """;

要点 3:缓存 Judge 结果

Judge 调用很贵,相同 (question, answer) 应缓存。


@Cacheable(value = "judge", key = "#question + '::' + #answer.hashCode()")
public JudgeResult judge(String question, String answer, String context) { ... }

要点 4:人工校准

LLM Judge 与人工 Judge 的一致性 ≤ 70%,需要:



相关页面