forked from Azure/azure-sdk-for-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
qa uses base classes and is testable (Azure#38993)
* qa uses base classes and is testable * evaluator list input for base class
- Loading branch information
1 parent
c694e3f
commit db2c434
Showing
6 changed files
with
131 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...valuation/azure-ai-evaluation/azure/ai/evaluation/_evaluators/_common/_base_multi_eval.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# --------------------------------------------------------- | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# --------------------------------------------------------- | ||
from concurrent.futures import as_completed | ||
from typing import TypeVar, Dict, List | ||
|
||
from promptflow.tracing import ThreadPoolExecutorWithContext as ThreadPoolExecutor | ||
from typing_extensions import override | ||
|
||
from azure.ai.evaluation._evaluators._common import EvaluatorBase | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
class MultiEvaluatorBase(EvaluatorBase[T]): | ||
""" | ||
Base class for evaluators that contain and run multiple other evaluators to produce a | ||
suite of metrics. | ||
Child classes still need to implement the __call__ methods, but they shouldn't need a _do_eval. | ||
:param evaluators: The list of evaluators to run when this evaluator is called. | ||
:type evaluators: List[~azure.ai.evaluation._evaluators._common.EvaluatorBase] | ||
:param kwargs: Additional arguments to pass to the evaluator. | ||
:type kwargs: Any | ||
:return: An evaluator that runs multiple other evaluators and combines their results. | ||
""" | ||
|
||
def __init__(self, evaluators: List[EvaluatorBase[T]], **kwargs): | ||
super().__init__() | ||
self._parallel = kwargs.pop("_parallel", True) | ||
self._evaluators = evaluators | ||
|
||
@override | ||
async def _do_eval(self, eval_input: Dict) -> Dict[str, T]: | ||
"""Run each evaluator, possibly in parallel, and combine the results into | ||
a single large dictionary containing each evaluation. Inputs are passed | ||
directly to each evaluator without additional processing. | ||
:param eval_input: The input to the evaluation function. | ||
:type eval_input: Dict | ||
:return: The evaluation result. | ||
:rtype: Dict | ||
""" | ||
results: Dict[str, T] = {} | ||
if self._parallel: | ||
with ThreadPoolExecutor() as executor: | ||
# pylint: disable=no-value-for-parameter | ||
futures = {executor.submit(evaluator, **eval_input): evaluator for evaluator in self._evaluators} | ||
|
||
for future in as_completed(futures): | ||
results.update(future.result()) | ||
else: | ||
for evaluator in self._evaluators: | ||
result = evaluator(**eval_input) | ||
# Ignore is to avoid mypy getting upset over the amount of duck-typing | ||
# that's going on to shove evaluators around like this. | ||
results.update(result) # type: ignore[arg-type] | ||
|
||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters