-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OPIK-101] sdk improve errors logs in evaluate method (#307)
* Draft implementation of catching missing score arguments situations * Fix lint errors * Refactor raise if score arguments are missing * Add unit test, fix lint errors * Add one more unit test for evaluate function * Remove unused fixture from the test * Update error message
- Loading branch information
1 parent
28399a4
commit 347fd21
Showing
6 changed files
with
132 additions
and
12 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
sdks/python/src/opik/evaluation/metrics/arguments_helpers.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,31 @@ | ||
from typing import List, Callable, Dict, Any | ||
import inspect | ||
from opik import exceptions | ||
|
||
|
||
def raise_if_score_arguments_are_missing( | ||
score_function: Callable, score_name: str, kwargs: Dict[str, Any] | ||
) -> None: | ||
signature = inspect.signature(score_function) | ||
|
||
parameters = signature.parameters | ||
|
||
missing_required_arguments: List[str] = [] | ||
|
||
for name, param in parameters.items(): | ||
if name == "self": | ||
continue | ||
|
||
if param.default == inspect.Parameter.empty and param.kind in ( | ||
inspect.Parameter.POSITIONAL_OR_KEYWORD, | ||
inspect.Parameter.KEYWORD_ONLY, | ||
): | ||
if name not in kwargs: | ||
missing_required_arguments.append(name) | ||
|
||
if len(missing_required_arguments) > 0: | ||
raise exceptions.ScoreMethodMissingArguments( | ||
f"The scoring object {score_name} is missing arguments: {missing_required_arguments}. " | ||
f"These keys were not present in the dictionary returned by the evaluation task. " | ||
f"Evaluation task dictionary keys found: {list(kwargs.keys())}." | ||
) |
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
35 changes: 35 additions & 0 deletions
35
sdks/python/tests/unit/evaluation/metrics/test_arguments_helpers.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,35 @@ | ||
from opik import exceptions | ||
from opik.evaluation.metrics import arguments_helpers, base_metric | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize( | ||
argnames="score_kwargs, should_raise", | ||
argvalues=[ | ||
({"a": 1, "b": 2}, False), | ||
({"a": 1, "b": 2, "c": 3}, False), | ||
({"a": 1, "c": 3}, True), | ||
({}, True), | ||
], | ||
) | ||
def test_raise_if_score_arguments_are_missing(score_kwargs, should_raise): | ||
class SomeMetric(base_metric.BaseMetric): | ||
def score(self, a, b, **ignored_kwargs): | ||
pass | ||
|
||
some_metric = SomeMetric(name="some-metric") | ||
|
||
if should_raise: | ||
with pytest.raises(exceptions.ScoreMethodMissingArguments): | ||
arguments_helpers.raise_if_score_arguments_are_missing( | ||
score_function=some_metric.score, | ||
score_name=some_metric.name, | ||
kwargs=score_kwargs, | ||
) | ||
else: | ||
arguments_helpers.raise_if_score_arguments_are_missing( | ||
score_function=some_metric.score, | ||
score_name=some_metric.name, | ||
kwargs=score_kwargs, | ||
) |
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