
Llm Evaluation Exposed: the Ultimate Guide to Unlocking Superior Ai Output!
引言
大型语言模型 (LLMs) 正在重塑我们与技术的互动方式。从虚拟助手到文本摘要工具,它们的应用范围广泛。但我们如何确保这些模型产生的输出准确、有意义且与上下文相关呢?评估LLMs不仅仅是检查它们是否“看起来正确”。这关系到确保它们的响应在多个维度上与任务要求相一致。
在本博客中,我们将带您了解一个实用的LLMs评估框架,探索我们将使用的工具和指标,并演示使用谷歌的Flan-T5模型的应用。到最后,您将全面了解如何衡量LLMs的性能,以及如何将该框架调整为适合您自己的用例。
为什么评估大型语言模型?
大型语言模型如开放AI的生成预训练变换器、谷歌的Flan-T5等,经过大量数据集的训练,并针对各种任务进行了微调。然而,它们的性能依赖于任务的复杂性、提示的质量,甚至人类对输出的解读细微差别。
评估目标:
- 准确性:模型是否生成了正确的答案?
- 上下文相关性:输出是否与输入提示的含义相匹配?
- 鲁棒性:模型能否处理措辞的变化?
评估框架
为了有效地衡量大型语言模型的性能,我们使用一个三层框架:
Image by Author-LLM evaluation Frameworks
1. Rouge Score
- What it does: Rouge (Recall-Oriented Understudy for Gisting Evaluation) measures word overlap between the generated text and the reference text.
- Why it matters: It’s commonly used in summarization tasks to quantify the similarity between expected and generated outputs.
- Limitation: It focuses solely on word overlap and may miss out on semantic context.
2. 语义相似度
- 它的作用:使用句子嵌入来评估生成输出与参考文本的意义。
- 它的重要性:它捕捉上下文和意义,使其在不需要精确措辞的任务中更具鲁棒性。
- 实现:我们使用句子变换器对句子进行编码并计算余弦相似度。
3. 模糊匹配
- 它的作用:使用启发式方法测量字符串相似度。它检查两个字符串的接近程度,即使它们的措辞略有不同。
- 为什么重要:这对于措辞不同但意义相同的输出非常有帮助(例如,“Python 是一种更好的语言”与“Python 是优越的”)。
- 实现:使用 FuzzyWuzzy 库计算字符串相似度分数。
大型语言模型: 谷歌的 Flan-T5
我们将使用谷歌的 Flan-T5 (Base) 模型进行此次评估。
为什么选择 Flan-T5?
Flan-T5 是 T5(文本到文本转换器)的微调版本,专为基于指令的任务而优化,如回答问题、摘要和分类。
关键特性:
- 通过额外的指令进行微调,以改善其零-shot 和少-shot 学习能力。
- 针对多样化的提示生成简洁、高质量的答案。
- 有效支持多轮对话。
这使其成为展示我们评估管理器的理想选择。
实施:评估框架的实际应用
这里是我们如何逐步实施和测试我们的框架。
1. 安装依赖
首先,安装所需的库:
!pip install langchain langchain-community deepeval google-generativeai rouge-score langchain-google-genai fuzzywuzzy
输出:
作者提供的图片
根据需要导入所有依赖:
from transformers import pipeline
from sentence_transformers import SentenceTransformer, util
from fuzzywuzzy import fuzz
import difflib
输出:
作者提供的图片
2. 加载模型
加载用于文本生成的 Flan-T5 模型和用于语义相似度的句子-BERT:
model_name = "google/flan-t5-base"
generator = pipeline("text2text-generation", model=model_name, device=-1)
semantic_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
输出:
作者图片
3. 评估管理器
这个类定义了用于Rouge、语义相似度和模糊匹配评估的方法:
class EvaluationManager:
@staticmethod
def rouge_evaluation(expected, actual, threshold=0.5):
similarity_ratio = difflib.SequenceMatcher(None, expected.lower(), actual.lower()).ratio()
return {
'score': similarity_ratio,
'passed': similarity_ratio >= threshold,
'details': {'similarity_ratio': similarity_ratio}
}
@staticmethod
def semantic_similarity_evaluation(expected, actual, threshold=0.5):
expected_embedding = semantic_model.encode(expected, convert_to_tensor=True)
actual_embedding = semantic_model.encode(actual, convert_to_tensor=True)
similarity = util.cos_sim(expected_embedding, actual_embedding).item()
return {
'score': similarity,
'passed': similarity >= threshold,
'details': {'cosine_similarity': similarity}
}
@staticmethod
def fuzzy_match(expected, actual, threshold=60):
score = fuzz.ratio(expected.lower(), actual.lower())
return {
'score': score / 100,
'passed': score >= threshold,
'details': {'fuzzy_match_score': score}
}
评估管理器
使用三种方法评估大型语言模型的输出与预期结果的匹配情况:
-
Rouge评估
- 使用
difflib.SequenceMatcher
测量文本重叠。 - 如果相似度比例 ≥ 0.5,则通过。
- 使用
-
语义相似度
- 使用
句子变换器
嵌入和余弦相似度评估含义。 - 如果余弦相似度 ≥ 0.5,则通过。
- 使用
-
模糊匹配
- 使用
fuzz.ratio
近似匹配字符串。 - 如果分数 ≥ 60,则通过。
- 使用
4. 运行评估
run_evaluation
函数接受测试用例,使用 Flan-T5 生成响应,并根据指标进行评估:
def run_evaluation(test_cases):
evaluator = EvaluationManager()
results = []
for case in test_cases:
prompt = f"Answer concisely and precisely: {case['input']}"
generated_response = generator(prompt, max_length=50, truncation=True, num_return_sequences=1)[0]['generated_text']
case['actual_output'] = generated_response.strip()
print(f"\nTest Case: {case['input']}")
print(f"Generated Output: {case['actual_output']}")
print(f"Expected Output: {case['expected_output']}\n")
rouge_result = evaluator.rouge_evaluation(case['expected_output'], case['actual_output'])
print("Rouge Metric:")
print(f"Score: {rouge_result['score']:.2f}")
print(f"Passed: {rouge_result['passed']}")
print(f"Details: {rouge_result['details']}\n")
semantic_result = evaluator.semantic_similarity_evaluation(case['expected_output'], case['actual_output'])
print("Semantic Evaluation:")
print(f"Score: {semantic_result['score']:.2f}")
print(f"Passed: {semantic_result['passed']}")
print(f"Details: {semantic_result['details']}\n")
fuzzy_result = evaluator.fuzzy_match(case['expected_output'], case['actual_output'])
print("Fuzzy Matching:")
print(f"Score: {fuzzy_result['score']:.2f}")
print(f"Passed: {fuzzy_result['passed']}")
print(f"Details: {fuzzy_result['details']}\n")
results.append({
'input': case['input'],
'actual_output': case['actual_output'],
'expected_output': case['expected_output'],
'rouge_score': rouge_result,
'semantic_similarity': semantic_result,
'fuzzy_match': fuzzy_result
})
return results
该函数使用多种指标评估大型语言模型输出与预期结果的匹配情况。
步骤:
-
初始化:加载测试用例并创建一个
EvaluationManager
实例。 -
生成输出:针对每个输入提示大型语言模型(例如,
google/flan-t5-base
)并将结果存储为actual_output
。 -
评估:使用以下方法比较
actual_output
和expected_output
:- Rouge:测量文本重叠。
- 语义相似度:使用嵌入比较含义。
- 模糊匹配:检查近似文本相似性。
-
记录结果:打印每个指标的分数、通过状态和详细信息。
-
存储与返回:将评估结果保存在结果列表中以供分析。
目的:
确保对大型语言模型输出在文本、语义和模糊匹配方面进行全面评估。
5. 定义测试用例并运行
这里是一个测试用例的示例集:
test_cases = [
{'input': "Is Python better than R", 'expected_output': "Yes, Python is better programming language"},
{'input': "What is the capital of India", 'expected_output': "New Delhi"},
]
results = run_evaluation(test_cases)
输出:
作者提供的图片
测试结果总结
测试用例 1:Python 是否优于 R?
- 生成的输出: Python 是比 R 更好的编程语言。
- 预期输出: 是的,Python 是更好的编程语言。
- 分数:
- Rouge:0.83 ✅
- 语义相似度:0.73 ✅
- 模糊匹配:0.83 ✅
测试用例 2:印度的首都是什么?
- 生成的输出: 德里
- 预期输出: 新德里
- 分数:
- Rouge:0.71 ✅
- 语义相似度:0.87 ✅
- 模糊匹配:0.71 ✅
分析
- 所有测试用例在各项指标上均达到了阈值。
- 语义相似度在捕捉意义方面表现出色。
- Rouge 和模糊匹配在处理部分文本匹配方面表现良好。
结论
在本指南中,我们探讨了一个针对大型语言模型(LLMs)的强大评估框架,使用了三个关键指标:Rouge分数、语义相似度和模糊匹配。通过将该框架应用于谷歌的Flan-T5模型,我们展示了这些评估方法如何提供对LLM性能的全面视图。
每个指标在评估LLM生成准确、上下文相关和稳健输出的能力方面都发挥着至关重要的作用:
- Rouge分数侧重于文本重叠,这在摘要等场景中非常有用,尽管确切的措辞可能有所不同,但核心内容保持不变。
- 语义相似度评估意义,即使措辞不同,也能捕捉文本的本质。
- 模糊匹配帮助评估可能措辞不同但意图相同的输出。
通过利用这些评估方法,我们可以确保LLM不仅生成乍看之下似乎正确的文本,而且在不同输入和变体中保持准确性、相关性和灵活性。