【2024大语言模型教程】如何评估RAG效果?三个指标,你学会了吗?

目录

RAG的评估过程:怎么工作的?

拓展思考

-- Groundedness得分是如何计算的?

-可是大语言模型返回的评估效果并不一定客观?

代码实现示例:


RAG的评估过程:怎么工作的?

假设我们有以下评估问题:

"What are the keys to building a career in AI?"

评估过程会如下进行:

  • 查询执行:
with tru_recorder as recording:
    response = sentence_window_engine.query("What are the keys to building a career in AI?")

假设系统返回的回答是:

"The keys to building a career in AI include continuous learning, practical experience, networking, and staying updated with the latest developments in the field."
  • 反馈计算:
    对于这个问题和回答,三个反馈函数会分别计算得分:

a. 答案相关性 (f_qa_relevance):
这个函数会评估回答与问题的相关程度。
假设得分:0.9 (非常相关)

b. 上下文相关性 (f_qs_relevance):
这个函数会评估检索的上下文片段与问题的相关程度。
假设系统检索了3个上下文片段,得分分别为0.8, 0.9, 0.7
平均得分:0.8

c. 扎实性 (f_groundedness):
这个函数会评估回答中的陈述是否有上下文支持。
假设得分:0.85 (大部分陈述都有支持)

  1. 结果记录:
    这些得分会被记录下来,与问题和回答一起存储。

  2. 重复过程:
    对每个评估问题重复这个过程。

  3. 结果分析:
    完成所有问题的评估后,我们可以查看结果:

records, feedback = tru.get_records_and_feedback(app_ids=[])
results = records[["input", "output"] + feedback]
  • 性能排行:
leaderboard = tru.get_leaderboard(app_ids=[])

这会生成一个基于这些得分的总体性能排行。例如,它可能会计算这三个指标的平均值作为总体得分。

通过这种方式,我们可以:

  1. 看到系统在每个问题上的表现。
  2. 识别系统在哪些方面表现良好(例如,答案相关性高),哪些方面需要改进(例如,如果上下文相关性普遍较低)。
  3. 比较不同版本的RAG系统,看哪个版本整体表现更好。

拓展思考

-- Groundedness得分是如何计算的?

其背后的数学原理和代码实现,扎实性评分的核心思想是衡量生成的回答在多大程度上基于给定的上下文信息。这个过程涉及几个步骤:

  1. 分解回答:
    首先,系统会将生成的回答分解成一系列独立的陈述。

  2. 对每个陈述评分:
    对于每个陈述,系统会评估它在多大程度上被上下文支持。

  3. 聚合得分:
    最后,将所有陈述的得分聚合成一个总的扎实性得分。

让我们深入了解这个过程的具体实现:

grounded = Groundedness(groundedness_provider=provider)
f_groundedness = (
    Feedback(grounded.groundedness_measure_with_cot_reasons,
             name="Groundedness"
            )
    .on(context_selection)
    .on_output()
    .aggregate(grounded.grounded_statements_aggregator)
)

这段代码使用了 Groundedness 类,该类实现了扎实性评估的核心逻辑。

具体的评分过程如下:

  1. 分解回答:
    使用自然语言处理技术(如句子分割)将回答分解成独立的陈述。

  2. 对每个陈述评分:
    对于每个陈述,groundedness_measure_with_cot_reasons 方法会执行以下操作:
    a. 将陈述与上下文进行比较。
    b. 使用语言模型(在这里是OpenAI的模型)来判断陈述在多大程度上被上下文支持。
    c. 生成一个介于0和1之间的得分,其中1表示完全支持,0表示完全不支持。

    这个评分过程通常涉及prompt工程,例如:

    Given the context: [上下文内容]
    And the statement: [陈述内容]
    On a scale of 0 to 1, how well is this statement supported by the context? Provide your reasoning.
    
  3. def simple_average(scores):
        return sum(scores) / len(scores)
    

    b. 加权平均,根据陈述的重要性或长度给予不同权重。

    c. 更复杂的聚合函数,可能考虑陈述间的关系或重要性。

假设我们有这样的结果:

陈述1得分:0.9
陈述2得分:0.8
陈述3得分:0.85

使用简单平均,最终的扎实性得分就是:(0.9 + 0.8 + 0.85) / 3 ≈ 0.85

这个0.85的得分表示生成的回答在很大程度上是基于给定上下文的,但可能有一小部分内容不完全被上下文支持或者是模型的推断。

重要的是要注意,这个过程涉及到语言模型的判断,因此结果可能会有一定的主观性和变异性。为了提高可靠性,可以采用以下策略:

  1. 多次评估并取平均值。
  2. 使用更高级的语言模型。
  3. 精心设计prompt以获得更一致的结果。

-可是大语言模型返回的评估效果并不一定客观?

可以采取几种方法来提高评估的客观性和可靠性:

  1. 多模型评估:
    使用多个不同的LLM进行评估,比较结果以减少单个模型的偏见。

  2. 人机协作:
    结合人工评估和LLM评估,特别是对于关键或有争议的案例。

  3. 规则基础评估:
    对于一些可以明确定义的标准,可以使用基于规则的系统而不是LLM。

  4. 统计方法:
    多次运行评估并使用统计方法来增加结果的稳定性。

  5. 验证集:
    使用人工标注的验证集来校准和评估LLM的评分性能。

  6. 明确的评分标准:
    为LLM提供更详细和明确的评分标准,减少主观判断的空间。

  7. 结果解释:
    要求LLM不仅给出分数,还要提供详细的理由,以便人工审核。

  8. 持续监控和调整:
    定期审查评估结果,识别和纠正系统性偏差。

代码实现示例:
def multi_model_groundedness_score(statement, context, models=['gpt-3.5-turbo', 'gpt-4', 'text-davinci-003']):
    scores = []
    for model in models:
        score = evaluate_groundedness(statement, context, model)
        scores.append(score)
    
    return {
        'mean_score': statistics.mean(scores),
        'median_score': statistics.median(scores),
        'std_dev': statistics.stdev(scores),
        'individual_scores': scores
    }

def evaluate_groundedness(statement, context, model):
    prompt = f"""
    Given the context: {context}
    And the statement: {statement}
    
    Evaluate the groundedness of the statement based on the context.
    Score from 0 to 1, where:
    0 = Completely unsupported by the context
    0.5 = Partially supported or inferred from the context
    1 = Fully supported by the context
    
    Provide your score and a detailed explanation of your reasoning.
    """
    
    response = call_llm_api(model, prompt)
    
    # Parse the response to extract score and explanation
    # This would require some NLP or regex to extract the numerical score
    
    return {'score': extracted_score, 'explanation': extracted_explanation}

# Example usage
result = multi_model_groundedness_score(
    "AI is revolutionizing healthcare through early disease detection.",
    "Recent studies show AI models can detect certain cancers in early stages."
)

print(f"Mean score: {result['mean_score']}")
print(f"Score distribution: {result['individual_scores']}")
print(f"Standard deviation: {result['std_dev']}")

这种方法通过使用多个模型和提供详细的评分标准来增加客观性。同时,通过要求解释,我们可以进行人工审核来验证评分的合理性。

  • 17
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值