|
10 | 10 | from typing import Any, Dict, NamedTuple, Optional, Union, cast
|
11 | 11 | import uuid
|
12 | 12 | import base64
|
| 13 | +import math |
13 | 14 |
|
14 | 15 | import pandas as pd
|
15 | 16 | from azure.ai.evaluation._legacy._adapters.entities import Run
|
@@ -173,9 +174,9 @@ def _log_metrics_and_instance_results_onedp(
|
173 | 174 | EvaluationRunProperties.RUN_TYPE: "eval_run",
|
174 | 175 | EvaluationRunProperties.EVALUATION_RUN: "promptflow.BatchRun",
|
175 | 176 | EvaluationRunProperties.EVALUATION_SDK: f"azure-ai-evaluation:{VERSION}",
|
176 |
| - EvaluationRunProperties.NAME_MAP: json.dumps(name_map), |
177 | 177 | "_azureml.evaluate_artifacts": json.dumps([{"path": artifact_name, "type": "table"}]),
|
178 |
| - } |
| 178 | + } |
| 179 | + properties.update(_convert_name_map_into_property_entries(name_map)) |
179 | 180 |
|
180 | 181 | create_evaluation_result_response = client.create_evaluation_result(
|
181 | 182 | name=uuid.uuid4(),
|
@@ -264,15 +265,14 @@ def _log_metrics_and_instance_results(
|
264 | 265 | # adding these properties to avoid showing traces if a dummy run is created.
|
265 | 266 | # We are doing that only for the pure evaluation runs.
|
266 | 267 | if run is None:
|
267 |
| - ev_run.write_properties_to_run_history( |
268 |
| - properties={ |
| 268 | + properties = { |
269 | 269 | EvaluationRunProperties.RUN_TYPE: "eval_run",
|
270 | 270 | EvaluationRunProperties.EVALUATION_RUN: "promptflow.BatchRun",
|
271 | 271 | EvaluationRunProperties.EVALUATION_SDK: f"azure-ai-evaluation:{VERSION}",
|
272 |
| - EvaluationRunProperties.NAME_MAP: json.dumps(name_map), |
273 | 272 | "_azureml.evaluate_artifacts": json.dumps([{"path": artifact_name, "type": "table"}]),
|
274 | 273 | }
|
275 |
| - ) |
| 274 | + properties.update(_convert_name_map_into_property_entries(name_map)) |
| 275 | + ev_run.write_properties_to_run_history(properties=properties) |
276 | 276 | else:
|
277 | 277 | ev_run.write_properties_to_run_history(
|
278 | 278 | properties={
|
@@ -407,6 +407,41 @@ def set_event_loop_policy() -> None:
|
407 | 407 | # On Windows seems to be a problem with EventLoopPolicy, use this snippet to work around it
|
408 | 408 | asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore[attr-defined]
|
409 | 409 |
|
| 410 | +# textwrap.wrap tries to do fancy nonsense that we don't want |
| 411 | +def _wrap(s, w): |
| 412 | + return [s[i:i + w] for i in range(0, len(s), w)] |
| 413 | + |
| 414 | +def _convert_name_map_into_property_entries( |
| 415 | + name_map: Dict[str, str], segment_length: int = 950, max_segments: int = 10 |
| 416 | +) -> Dict[str, Any]: |
| 417 | + """ |
| 418 | + Convert the name map into property entries. |
| 419 | +
|
| 420 | + :param name_map: The name map to be converted. |
| 421 | + :type name_map: Dict[str, str] |
| 422 | + :param segment_length: The max length of each individual segment, |
| 423 | + which will each have their own dictionary entry |
| 424 | + :type segment_length: str |
| 425 | + :param max_segments: The max number of segments we can have. If the stringified |
| 426 | + name map is too long, we just return a length entry with a value |
| 427 | + of -1 to indicate that the map was too long. |
| 428 | + :type max_segments: str |
| 429 | + :return: The converted name map. |
| 430 | + :rtype: Dict[str, Any] |
| 431 | + """ |
| 432 | + name_map_string = json.dumps(name_map) |
| 433 | + num_segments = math.ceil(len(name_map_string) / segment_length) |
| 434 | + # Property map is somehow still too long to encode within the space |
| 435 | + # we allow, so give up, but make sure the service knows we gave up |
| 436 | + if (num_segments > max_segments): |
| 437 | + return {EvaluationRunProperties.NAME_MAP_LENGTH: -1} |
| 438 | + |
| 439 | + result: Dict[str, Any] = {EvaluationRunProperties.NAME_MAP_LENGTH: num_segments} |
| 440 | + segments_list = _wrap(name_map_string, segment_length) |
| 441 | + for i in range(0, num_segments): |
| 442 | + segment_key = f"{EvaluationRunProperties.NAME_MAP}_{i}" |
| 443 | + result[segment_key] = segments_list[i] |
| 444 | + return result |
410 | 445 |
|
411 | 446 | class JSONLDataFileLoader:
|
412 | 447 | def __init__(self, filename: Union[os.PathLike, str]):
|
|
0 commit comments