Skip to content

Commit 2117287

Browse files
ghyadavkdestin
andauthored
Update Relevance prompt to fix prompt, enhance evaluation steps and define output structure (#41762)
* Update Relevance prompt: - Fix rubric 2 to not check "incorrect" instead check relevace only - Update output to get score and explanation only - Update prompt to be more crisp. Include evaluation steps * change to json objects update examples for rubric 4 and 5 * change to json objects update examples for rubric 4 and 5 * change to json objects update examples for rubric 4 and 5 * Fix formatting * Fix formatting * updating the prompt to cover "Failures" explicitly * Adding CHANGELOG.md * Running black * Adding dummy change * Updating assets.json to point to latest test recording * Updating assets.json to point to latest test recording * chore: Update assets.json * test,fix: Explicitly set caplog log level * chore: Update assets.json --------- Co-authored-by: kdestin <101366538+kdestin@users.noreply.github.com>
1 parent 146a00f commit 2117287

File tree

5 files changed

+200
-61
lines changed

5 files changed

+200
-61
lines changed

sdk/evaluation/azure-ai-evaluation/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
### Bugs Fixed
1111

1212
- Significant improvements to TaskAdherence evaluator. New version has less variance, is much faster and consumes fewer tokens.
13+
- Significant improvements to Relevance evaluator. New version has more concrete rubrics and has less variance, is much faster and consumes fewer tokens.
14+
1315

1416
## 1.9.0 (2025-07-02)
1517

sdk/evaluation/azure-ai-evaluation/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/evaluation/azure-ai-evaluation",
5-
"Tag": "python/evaluation/azure-ai-evaluation_4a5c8207cc"
5+
"Tag": "python/evaluation/azure-ai-evaluation_1bc445d16b"
66
}

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/_evaluators/_relevance/_relevance.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
# ---------------------------------------------------------
22
# Copyright (c) Microsoft Corporation. All rights reserved.
33
# ---------------------------------------------------------
4-
4+
import logging
5+
import math
56
import os
67
from typing import Dict, Union, List
78

89
from typing_extensions import overload, override
910

11+
from azure.ai.evaluation._exceptions import EvaluationException, ErrorBlame, ErrorCategory, ErrorTarget
12+
from ..._common.utils import reformat_conversation_history, reformat_agent_response
13+
1014
from azure.ai.evaluation._model_configurations import Conversation
1115
from azure.ai.evaluation._evaluators._common import PromptyEvaluatorBase
1216

17+
logger = logging.getLogger(__name__)
18+
1319

1420
class RelevanceEvaluator(PromptyEvaluatorBase):
1521
"""
@@ -141,3 +147,49 @@ def __call__( # pylint: disable=docstring-missing-param
141147
:rtype: Union[Dict[str, Union[str, float]], Dict[str, Union[float, Dict[str, List[Union[str, float]]]]]]
142148
"""
143149
return super().__call__(*args, **kwargs)
150+
151+
async def _do_eval(self, eval_input: Dict) -> Dict[str, Union[float, str]]: # type: ignore[override]
152+
"""Do a relevance evaluation.
153+
154+
:param eval_input: The input to the evaluator. Expected to contain
155+
whatever inputs are needed for the _flow method, including context
156+
and other fields depending on the child class.
157+
:type eval_input: Dict
158+
:return: The evaluation result.
159+
:rtype: Dict
160+
"""
161+
if "query" not in eval_input and "response" not in eval_input:
162+
raise EvaluationException(
163+
message="Only text conversation inputs are supported.",
164+
internal_message="Only text conversation inputs are supported.",
165+
blame=ErrorBlame.USER_ERROR,
166+
category=ErrorCategory.INVALID_VALUE,
167+
target=ErrorTarget.CONVERSATION,
168+
)
169+
if not isinstance(eval_input["query"], str):
170+
eval_input["query"] = reformat_conversation_history(eval_input["query"], logger)
171+
if not isinstance(eval_input["response"], str):
172+
eval_input["response"] = reformat_agent_response(eval_input["response"], logger)
173+
llm_output = await self._flow(timeout=self._LLM_CALL_TIMEOUT, **eval_input)
174+
score = math.nan
175+
176+
if isinstance(llm_output, dict):
177+
score = float(llm_output.get("score", math.nan))
178+
reason = llm_output.get("explanation", "")
179+
# Parse out score and reason from evaluators known to possess them.
180+
binary_result = self._get_binary_result(score)
181+
return {
182+
self._result_key: float(score),
183+
f"gpt_{self._result_key}": float(score),
184+
f"{self._result_key}_reason": reason,
185+
f"{self._result_key}_result": binary_result,
186+
f"{self._result_key}_threshold": self._threshold,
187+
}
188+
189+
binary_result = self._get_binary_result(score)
190+
return {
191+
self._result_key: float(score),
192+
f"gpt_{self._result_key}": float(score),
193+
f"{self._result_key}_result": binary_result,
194+
f"{self._result_key}_threshold": self._threshold,
195+
}

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/_evaluators/_relevance/relevance.prompty

Lines changed: 140 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,91 +10,172 @@ model:
1010
presence_penalty: 0
1111
frequency_penalty: 0
1212
response_format:
13-
type: text
13+
type: json_object
1414

1515
inputs:
1616
query:
1717
type: string
1818
response:
1919
type: string
20-
2120
---
21+
2222
system:
23-
# Instruction
24-
## Goal
25-
### You are an expert in evaluating the quality of a RESPONSE from an intelligent system based on provided definition and data. Your goal will involve answering the questions below using the information provided.
26-
- **Definition**: You are given a definition of the communication trait that is being evaluated to help guide your Score.
27-
- **Data**: Your input data include QUERY and RESPONSE.
28-
- **Tasks**: To complete your evaluation you will be asked to evaluate the Data in different ways.
23+
You are a Relevance-Judge, an impartial evaluator that scores how well the RESPONSE addresses the QUERY using the definitions provided.
2924

3025
user:
31-
# Definition
32-
**Relevance** refers to how effectively a response addresses a question. It assesses the accuracy, completeness, and direct relevance of the response based solely on the given information.
33-
34-
# Ratings
35-
## [Relevance: 1] (Irrelevant Response)
36-
**Definition:** The response is unrelated to the question. It provides information that is off-topic and does not attempt to address the question posed.
37-
38-
**Examples:**
39-
**Query:** What is the team preparing for?
40-
**Response:** I went grocery shopping yesterday evening.
41-
42-
**Query:** When will the company's new product line launch?
43-
**Response:** International travel can be very rewarding and educational.
26+
ROLE
27+
====
28+
You are a Relevance Evaluator. Your task is to judge how relevant a RESPONSE is to a QUERY using the Relevance definitions provided.
4429

45-
## [Relevance: 2] (Incorrect Response)
46-
**Definition:** The response attempts to address the question but includes incorrect information. It provides a response that is factually wrong based on the provided information.
30+
INPUT
31+
=====
32+
QUERY: {{query}}
33+
RESPONSE: {{response}}
4734

48-
**Examples:**
49-
**Query:** When was the merger between the two firms finalized?
50-
**Response:** The merger was finalized on April 10th.
35+
TASK
36+
====
37+
Output a JSON object with:
38+
1) a concise explanation of 15-60 words justifying your score based on how well the response is relevant to the query.
39+
2) an integer score from 1 (very poor) to 5 (excellent) using the rubric below.
5140

52-
**Query:** Where and when will the solar eclipse be visible?
53-
**Response:** The solar eclipse will be visible in Asia on December 14th.
41+
The explanation should always precede the score and should clearly justify the score based on the rubric definitions.
42+
Response format exactly as follows:
5443

55-
## [Relevance: 3] (Incomplete Response)
56-
**Definition:** The response addresses the question but omits key details necessary for a full understanding. It provides a partial response that lacks essential information.
44+
{
45+
"explanation": "<15-60 words>",
46+
"score": <1-5>
47+
}
5748

58-
**Examples:**
59-
**Query:** What type of food does the new restaurant offer?
60-
**Response:** The restaurant offers Italian food like pasta.
6149

62-
**Query:** What topics will the conference cover?
63-
**Response:** The conference will cover renewable energy and climate change.
50+
EVALUATION STEPS
51+
================
52+
A. Read the QUERY and RESPONSE carefully.
53+
B. Compare the RESPONSE against the rubric below:
54+
- Does the response directly address the query?
55+
- Is the information complete, partial, or off-topic?
56+
- Is it vague, generic, or insightful?
57+
C. Match the response to the best score from the rubric.
58+
D. Provide a short explanation and the score using the required format.
6459

65-
## [Relevance: 4] (Complete Response)
66-
**Definition:** The response fully addresses the question with accurate and complete information. It includes all essential details required for a comprehensive understanding, without adding any extraneous information.
60+
SCORING RUBRIC
61+
==============
6762

68-
**Examples:**
69-
**Query:** What type of food does the new restaurant offer?
70-
**Response:** The new restaurant offers Italian cuisine, featuring dishes like pasta, pizza, and risotto.
63+
### Score 1 - Irrelevant Response
64+
Definition: The response is unrelated to the question. It provides off-topic information and does not attempt to address the question posed.
7165

72-
**Query:** What topics will the conference cover?
73-
**Response:** The conference will cover renewable energy, climate change, and sustainability practices.
66+
**Example A**
67+
QUERY: What is the team preparing for?
68+
RESPONSE: I went grocery shopping yesterday evening.
7469

75-
## [Relevance: 5] (Comprehensive Response with Insights)
76-
**Definition:** The response not only fully and accurately addresses the question but also includes additional relevant insights or elaboration. It may explain the significance, implications, or provide minor inferences that enhance understanding.
70+
Expected Output:
71+
{
72+
"explanation": "The response is entirely off-topic and doesn't address the question.",
73+
"score": 1
74+
}
7775

78-
**Examples:**
79-
**Query:** What type of food does the new restaurant offer?
80-
**Response:** The new restaurant offers Italian cuisine, featuring dishes like pasta, pizza, and risotto, aiming to provide customers with an authentic Italian dining experience.
8176

82-
**Query:** What topics will the conference cover?
83-
**Response:** The conference will cover renewable energy, climate change, and sustainability practices, bringing together global experts to discuss these critical issues.
77+
**Example B**
78+
QUERY: When will the company's new product line launch?
79+
RESPONSE: International travel can be very rewarding and educational.
8480

81+
Expected Output:
82+
{
83+
"explanation": "The response is completely irrelevant to the product launch question.",
84+
"score": 1
85+
}
86+
87+
88+
### Score 2 – Related but Unhelpful / Superficial
89+
Definition: The response is loosely or formally related to the query but fails to deliver any meaningful, specific, or useful information. This includes vague phrases, non-answers, or failure/error messages.
90+
91+
**Example A**
92+
QUERY: What is the event about?
93+
RESPONSE: It’s something important.
94+
95+
Expected Output:
96+
{
97+
"explanation": "The response vaguely refers to the query topic but lacks specific or informative content.",
98+
"score": 2
99+
}
100+
101+
**Example B**
102+
QUERY: What’s the weather in Paris?
103+
RESPONSE: I tried to find the forecast but the query failed.
104+
105+
Expected Output:
106+
{
107+
"explanation": "The response acknowledges the query but provides no usable weather information. It is related but unhelpful.",
108+
"score": 2
109+
}
110+
111+
### Score 3 - Partially Relevant / Incomplete
112+
Definition: The response addresses the query and includes relevant information, but omits essential components or detail. The answer is on-topic but insufficient to fully satisfy the request.
113+
114+
**Example A**
115+
QUERY: What amenities does the new apartment complex provide?
116+
RESPONSE: The apartment complex has a gym.
117+
118+
Expected Output:
119+
{
120+
"explanation": "The response mentions one amenity but does not provide a fuller list or clarify whether other standard features (like parking or security) are included. It partially addresses the query but lacks completeness.",
121+
"score": 3
122+
}
123+
124+
**Example B**
125+
QUERY: What services does the premium membership include?
126+
RESPONSE: It includes priority customer support.
127+
128+
Expected Output:
129+
{
130+
"explanation": "The response identifies one service but omits other likely components of a premium membership (e.g., exclusive content or discounts). The information is relevant but incomplete.",
131+
"score": 3
132+
}
133+
134+
135+
136+
### Score 4 - Fully Relevant / Sufficient Response
137+
Definition: The response fully addresses the question with accurate and sufficient information, covering all essential aspects. Very minor omissions are acceptable as long as the core information is intact and the intent is clearly conveyed.
138+
139+
**Example A**
140+
QUERY: What amenities does the new apartment complex provide?
141+
RESPONSE: The apartment complex provides a gym, swimming pool, and 24/7 security.
142+
143+
Expected Output:
144+
{
145+
"explanation": "The response mentions multiple key amenities likely to be relevant to most users. While it may not list every feature, it clearly conveys the core offerings of the complex.",
146+
"score": 4
147+
}
85148

149+
**Example B**
150+
QUERY: What services does the premium membership include?
151+
RESPONSE: The premium membership includes priority customer support, exclusive content access, and early product releases.
152+
153+
Expected Output:
154+
{
155+
"explanation": "The response outlines all major services expected from a premium membership. Even if a minor service is not mentioned, the core value is clearly and fully represented.",
156+
"score": 4
157+
}
86158

87-
# Data
88-
QUERY: {{query}}
89-
RESPONSE: {{response}}
90159

160+
### Score 5 - Comprehensive Response with Insights
161+
Definition: The response not only fully and accurately answers the question, but also adds meaningful elaboration, interpretation, or context that enhances the user's understanding. This goes beyond just listing relevant details — it offers insight into why the information matters, how it's useful, or what impact it has.
91162

92-
# Tasks
93-
## Please provide your assessment Score for the previous RESPONSE in relation to the QUERY based on the Definitions above. Your output should include the following information:
94-
- **ThoughtChain**: To improve the reasoning process, think step by step and include a step-by-step explanation of your thought process as you analyze the data based on the definitions. Keep it brief and start your ThoughtChain with "Let's think step by step:".
95-
- **Explanation**: a very short explanation of why you think the input Data should get that Score.
96-
- **Score**: based on your previous analysis, provide your Score. The Score you give MUST be a integer score (i.e., "1", "2"...) based on the levels of the definitions.
163+
**Example A**
164+
QUERY: What amenities does the new apartment complex provide?
165+
RESPONSE: The apartment complex provides a gym, swimming pool, and 24/7 security, designed to offer residents a comfortable and active lifestyle while ensuring their safety.
166+
167+
Expected Output:
168+
{
169+
"explanation": "The response fully lists key amenities and additionally explains how these features contribute to resident experience, enhancing the usefulness of the information.",
170+
"score": 5
171+
}
97172

173+
**Example B**
174+
QUERY: What services does the premium membership include?
175+
RESPONSE: The premium membership includes priority customer support, exclusive content access, and early product releases — tailored for users who want quicker resolutions and first access to new features.
98176

99-
## Please provide your answers between the tags: <S0>your chain of thoughts</S0>, <S1>your explanation</S1>, <S2>your Score</S2>.
100-
# Output
177+
Expected Output:
178+
{
179+
"explanation": "The response covers all essential services and adds valuable insight about the target user and benefits, enriching the response beyond basic listing.",
180+
"score": 5
181+
}

sdk/evaluation/azure-ai-evaluation/tests/e2etests/test_metrics_upload.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def test_logging_metrics(self, caplog, project_scope, azure_ml_client):
9595
# as a parent. This logger does not propagate the logs and cannot be
9696
# captured by caplog. Here we will skip this logger to capture logs.
9797
logger.parent = logging.root
98+
caplog.set_level(logging.DEBUG)
99+
98100
with EvalRun(
99101
run_name="test",
100102
tracking_uri=_get_tracking_uri(azure_ml_client, project_scope),
@@ -126,6 +128,8 @@ def test_log_artifact(self, project_scope, azure_cred, datastore_project_scopes,
126128
# captured by caplog. Here we will skip this logger to capture logs.
127129
logger.parent = logging.root
128130

131+
caplog.set_level(logging.DEBUG)
132+
129133
project_scope = datastore_project_scopes[config_name]
130134
azure_ml_client = LiteMLClient(
131135
subscription_id=project_scope["subscription_id"],

0 commit comments

Comments
 (0)