Skip to content

Commit de2928b

Browse files
authored
Custom eval sample fix (#45989)
* Sample-Fix * fix samples
1 parent e043089 commit de2928b

5 files changed

Lines changed: 28 additions & 20 deletions

File tree

sdk/ai/azure-ai-projects/samples/evaluations/custom_evaluators/answer_length_evaluator/answer_length_evaluator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33

44
class AnswerLengthEvaluator:
5-
def __init__(self, *, model_config):
6-
self.model_config = model_config
5+
def __init__(self, *, config: str, threshold, **kwargs):
6+
self.config = config
7+
self.threshold = threshold
78

89
def __call__(self, *args, **kwargs):
9-
return {"result": evaluate_answer_length(kwargs.get("response"))}
10+
return {
11+
"result": evaluate_answer_length(kwargs.get("response")),
12+
}
1013

1114

1215
def evaluate_answer_length(answer: str | None):

sdk/ai/azure-ai-projects/samples/evaluations/custom_evaluators/friendly_evaluator/common_util/util.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ def build_evaluation_messages(query: str, response: str) -> list:
4141
]
4242

4343

44-
def parse_evaluation_result(raw_result: str) -> dict:
44+
def parse_evaluation_result(raw_result: str, threshold: int = 3) -> dict:
4545
"""Parse the LLM's JSON response into a structured evaluation result.
4646
4747
:param raw_result: The raw string output from the LLM.
48+
:param threshold: The minimum score to be considered "Pass".
4849
:return: A dict with score, label, reason, and explanation.
4950
"""
5051
import json
@@ -56,16 +57,16 @@ def parse_evaluation_result(raw_result: str) -> dict:
5657
text = text.split("\n", 1)[1] if "\n" in text else text[3:]
5758
text = text.rsplit("```", 1)[0]
5859
result = json.loads(text.strip())
59-
score = int(result.get("score", 3))
60+
score = int(result.get("score", threshold))
6061
return {
6162
"score": max(1, min(5, score)),
62-
"label": result.get("label", "Pass" if score >= 3 else "Fail"),
63+
"label": result.get("label", "Pass" if score >= threshold else "Fail"),
6364
"reason": result.get("reason", "No reason provided"),
6465
"explanation": result.get("explanation", "No explanation provided"),
6566
}
6667
except (json.JSONDecodeError, ValueError, KeyError):
6768
return {
68-
"score": 3,
69+
"score": threshold,
6970
"label": "Pass",
7071
"reason": "Could not parse LLM response",
7172
"explanation": f"Raw LLM output: {raw_result}",

sdk/ai/azure-ai-projects/samples/evaluations/custom_evaluators/friendly_evaluator/friendly_evaluator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ class FriendlyEvaluator:
1515
- azure_deployment: The deployment/model name.
1616
- api_version: The API version (default: "2024-06-01").
1717
- api_key: (Optional) The API key. If not provided, DefaultAzureCredential is used.
18+
:param threshold: The minimum score (1-5) to be considered "Pass" (default: 3).
1819
"""
1920

20-
def __init__(self, *, model_config: dict):
21+
def __init__(self, *, model_config: dict, threshold: int = 3, **kwargs):
2122
self.model_config = model_config
23+
self.threshold = threshold
2224
api_key = model_config.get("api_key")
2325

2426
if api_key:
@@ -61,4 +63,4 @@ def __call__(self, *, query: str, response: str, **kwargs) -> dict:
6163
raw_result = completion.choices[0].message.content
6264
if raw_result is None:
6365
raise ValueError("No content in completion response")
64-
return parse_evaluation_result(raw_result)
66+
return parse_evaluation_result(raw_result, self.threshold)

sdk/ai/azure-ai-projects/samples/evaluations/sample_eval_upload_custom_evaluator.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@
8181
entry_point="answer_length_evaluator:AnswerLengthEvaluator",
8282
init_parameters={
8383
"type": "object",
84-
"properties": {"model_config": {"type": "string"}},
85-
"required": ["model_config"],
84+
"properties": {
85+
"config": {"type": "string"},
86+
"threshold": {"type": "number"}
87+
},
88+
"required": ["config", "threshold"],
8689
},
8790
data_schema={
8891
"type": "object",
@@ -93,7 +96,7 @@
9396
"required": ["query", "response"],
9497
},
9598
metrics={
96-
"score": EvaluatorMetric(
99+
"result": EvaluatorMetric(
97100
type=EvaluatorMetricType.ORDINAL,
98101
desirable_direction=EvaluatorMetricDirection.INCREASE,
99102
min_value=1,
@@ -138,7 +141,8 @@
138141
"name": evaluator_name,
139142
"evaluator_name": evaluator_name,
140143
"initialization_parameters": {
141-
"model_config": f"{model_deployment_name}",
144+
"config": "example config value",
145+
"threshold": 3,
142146
},
143147
}
144148
]

sdk/ai/azure-ai-projects/samples/evaluations/sample_eval_upload_friendly_evaluator.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@
9999
"api_key": {"type": "string"},
100100
},
101101
"required": ["azure_endpoint", "api_key"],
102-
}
102+
},
103+
"threshold": {"type": "number"},
103104
},
104-
"required": ["model_config"],
105+
"required": ["model_config", "threshold"],
105106
},
106107
data_schema={
107108
"type": "object",
@@ -157,11 +158,8 @@
157158
"name": evaluator_name,
158159
"evaluator_name": evaluator_name,
159160
"initialization_parameters": {
160-
"model_config": {
161-
"azure_endpoint": azure_openai_endpoint,
162-
"api_key": f"{azure_openai_api_key}",
163-
"api_version": "2024-06-01",
164-
},
161+
"deployment_name": f"{model_deployment_name}", # provide model_config or, deployment name passed is used to construct the model_config for the evaluator.
162+
"threshold": 3,
165163
},
166164
}
167165
]

0 commit comments

Comments
 (0)