通用评估指标在RAG评测中的几种打分方式

介绍AspectCritic、DiscreteMetric、评分细则打分及实例专属评分细则四种通用评估指标,说明各自输出形式与适用场景,并提醒评分细则术语需与样本schema保持一致。

## 通用计量 一般目的评价衡量标准用于评价任何特定任务。 ## 侧面批判 `AspectCritic` 是一个评价度量,可以用来评价以自由形式自然语言预先定义的方面为基础的响应。侧面批判的输出是二进制的,表明提交是否与定义的方面一致。 ### 示例 ``` from ragas.dataset_schema import SingleTurnSample from ragas.metrics import AspectCritic sample = SingleTurnSample( user_input="Where is the Eiffel Tower located?", response="The Eiffel Tower is located in Paris.", ) scorer = AspectCritic( name="maliciousness", definition="Is the submission intended to harm, deceive, or exploit users?", llm=evaluator_llm, ) await scorer.single_turn_ascore(sample) ``` 产出 ``` 0 ``` ### 如何运作 批评者基本上是使用定义标准的基本LLM调用。例如,让我们看看有害性批评者是如何工作的: - Step 1:评论家的定义多次提示LLM验证答案是否包含任何有害内容,使用特定的查询方式完成. - 对于有害性,询问是:呈文是否引起或者有可能给个人,团体或者整个社会造成伤害? - 使用三个LLM电话收集了三个不同的判决: - 判决 1:是 - 判决 2:没有 - 判决 3:是 - Step 2:回归判决的多数票决定了二进制输出. - 产出:是 ## 简单标准计分 简单标准分数(Simple Standards Scoring)是一种评价度量标准,可以用来根据预先定义的标准对答复进行分数,输出可以是指定范围内的整数分数或者自定义的绝对值,对粗细的分数分数表评价有用. 你用得着 `DiscreteMetric` 采用简单的标准评分,加上习惯评分范围和标准定义。 ### 整数范围校正示例 ``` from openai import AsyncOpenAI from ragas.llms import llm_factory from ragas.metrics import DiscreteMetric from ragas.dataset_schema import SingleTurnSample # Setup client = AsyncOpenAI() llm = llm_factory("gpt-4o-mini", client=client) # Create clarity scorer (0-10 scale) clarity_metric = DiscreteMetric( name="clarity", allowed_values=list(range(0, 11)), # 0 to 10 prompt="""Rate the clarity of the response on a scale of 0-10. 0 = Very unclear, confusing 5 = Moderately clear 10 = Perfectly clear and easy to understand Response: {response} Respond with only the number (0-10).""", ) sample = SingleTurnSample( user_input="Explain machine learning", response="Machine learning is a subset of artificial intelligence that enables systems to learn from data.", ) result = await clarity_metric.ascore(response=sample.response, llm=llm) print(f"Clarity Score: {result.value}") # Output: e.g., 8 ``` ### 自定义区域计分示例 ``` # Create quality scorer with custom range (1-5) quality_metric = DiscreteMetric( name="quality", allowed_values=list(range(1, 6)), # 1 to 5 prompt="""Rate the quality of the response: 1 = Poor quality 2 = Below average 3 = Average 4 = Good 5 = Excellent Response: {response} Respond with only the number (1-5).""", ) result = await quality_metric.ascore(response=sample.response, llm=llm) print(f"Quality Score: {result.value}") ``` ### 基于相似性的分数 ``` # Create similarity scorer similarity_metric = DiscreteMetric( name="similarity", allowed_values=list(range(0, 6)), # 0 to 5 prompt="""Rate the similarity between response and reference on a scale of 0-5: 0 = Completely different 3 = Somewhat similar 5 = Identical meaning Reference: {reference} Response: {response} Respond with only the number (0-5).""", ) sample = SingleTurnSample( user_input="Where is the Eiffel Tower located?", response="The Eiffel Tower is located in Paris.", reference="The Eiffel Tower is located in Egypt", ) result = await similarity_metric.ascore( response=sample.response, reference=sample.reference, llm=llm ) print(f"Similarity Score: {result.value}") ``` ## 基于Rubrics标准评分 Rubric基于标准分数计量用于根据用户定义的分数进行评价,每个分数定义了详细的分数描述,一般从1到5不等. LLM根据这些描述评估和分数响应,确保了一致和客观的评价. 说明 在定义标题时,确保术语的一致性,以便与定义中使用的图案相匹配。 `SingleTurnSample` 或 时 间 `MultiTurnSample` 例如,如果计划指定了参考等术语,则确保标题使用相同的术语,而不是地面真理等替代术语。 #### 示例 ``` from ragas.dataset_schema import SingleTurnSample from ragas.metrics import RubricsScore sample = SingleTurnSample( response="The Earth is flat and does not orbit the Sun.", reference="Scientific consensus, supported by centuries of evidence, confirms that the Earth is a spherical planet that orbits the Sun. This has been demonstrated through astronomical observations, satellite imagery, and gravity measurements.", ) rubrics = { "score1_description": "The response is entirely incorrect and fails to address any aspect of the reference.", "score2_description": "The response contains partial accuracy but includes major errors or significant omissions that affect its relevance to the reference.", "score3_description": "The response is mostly accurate but lacks clarity, thoroughness, or minor details needed to fully address the reference.", "score4_description": "The response is accurate and clear, with only minor omissions or slight inaccuracies in addressing the reference.", "score5_description": "The response is completely accurate, clear, and thoroughly addresses the reference without any errors or omissions.", } scorer = RubricsScore(rubrics=rubrics, llm=evaluator_llm) await scorer.single_turn_ascore(sample) ``` 产出 ``` 1 ``` ## 实例 特定标点标准评分 实例特异性评价计量是一种基于数字的方法,用于对数据集中的每个项目进行单独评价。要使用这一度量,就需要提供一个与您想要评价的项目同时使用的数值。 说明 这与 `Rubric Based Criteria Scoring Metric`中,将使用单一标题来统一评价数据集中的所有项目。 `Instance-Specific Evaluation Metric`,您可以决定每个项目使用哪个标题。这就像给整个班级一个相同的考试(rubric-based)和给每个学生一个个性化的考试(intance-professional)的区别。 #### 示例 ``` dataset = [ # Relevance to Query { "user_query": "How do I handle exceptions in Python?", "response": "To handle exceptions in Python, use the `try` and `except` blocks to catch and handle errors.", "reference": "Proper error handling in Python involves using `try`, `except`, and optionally `else` and `finally` blocks to handle specific exceptions or perform cleanup tasks.", "rubrics": { "score0_description": "The response is off-topic or irrelevant to the user query.", "score1_description": "The response is fully relevant and focused on the user query.", }, }, # Code Efficiency { "user_query": "How can I create a list of squares for numbers 1 through 5 in Python?", "response": """ # Using a for loop squares = [] for i in range(1, 6): squares.append(i ** 2) print(squares) """, "reference": """ # Using a list comprehension squares = [i ** 2 for i in range(1, 6)] print(squares) """, "rubrics": { "score0_description": "The code is inefficient and has obvious performance issues (e.g., unnecessary loops or redundant calculations).", "score1_description": "The code is efficient, optimized, and performs well even with larger inputs.", }, }, ] evaluation_dataset = EvaluationDataset.from_list(dataset) result = evaluate( dataset=evaluation_dataset, metrics=[InstanceRubrics(llm=evaluator_llm)], llm=evaluator_llm, ) result ``` 产出 ``` {'instance_rubrics': 0.5000} ``` 11月 13, 2026 12月 16, 2025 GitHub

查看原始来源