-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #238 from whylabs/validation-library
Add validation library
- Loading branch information
Showing
8 changed files
with
380 additions
and
48 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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
from functools import partial | ||
from typing import Any, Callable, List, Optional, Sequence, Set, Union | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from langkit.core.validation import ValidationFailure, ValidationResult, Validator | ||
|
||
|
||
def _enforce_upper_threshold(target_metric: str, upper_threshold: Union[int, float], value: Any, id: str) -> Sequence[ValidationFailure]: | ||
if not isinstance(value, (float, int)): | ||
return [] | ||
|
||
if value > upper_threshold: | ||
return [ | ||
ValidationFailure( | ||
id=id, | ||
metric=target_metric, | ||
details=f"Value {value} is above threshold {upper_threshold}", | ||
value=value, | ||
upper_threshold=upper_threshold, | ||
) | ||
] | ||
|
||
return [] | ||
|
||
|
||
def _enforce_lower_threshold(target_metric: str, lower_threshold: Union[int, float], value: Any, id: str) -> Sequence[ValidationFailure]: | ||
if not isinstance(value, (float, int)): | ||
return [] | ||
|
||
if value < lower_threshold: | ||
return [ | ||
ValidationFailure( | ||
id=id, | ||
metric=target_metric, | ||
details=f"Value {value} is below threshold {lower_threshold}", | ||
value=value, | ||
lower_threshold=lower_threshold, | ||
) | ||
] | ||
|
||
return [] | ||
|
||
|
||
def _enforce_upper_threshold_inclusive( | ||
target_metric: str, upper_threshold_inclusive: Union[int, float], value: Any, id: str | ||
) -> Sequence[ValidationFailure]: | ||
if not isinstance(value, (float, int)): | ||
return [] | ||
|
||
if value >= upper_threshold_inclusive: | ||
return [ | ||
ValidationFailure( | ||
id=id, | ||
metric=target_metric, | ||
details=f"Value {value} is above or equal to threshold {upper_threshold_inclusive}", | ||
value=value, | ||
upper_threshold=upper_threshold_inclusive, | ||
) | ||
] | ||
|
||
return [] | ||
|
||
|
||
def _enforce_lower_threshold_inclusive( | ||
target_metric: str, lower_threshold_inclusive: Union[int, float], value: Any, id: str | ||
) -> Sequence[ValidationFailure]: | ||
if not isinstance(value, (float, int)): | ||
return [] | ||
|
||
if value <= lower_threshold_inclusive: | ||
return [ | ||
ValidationFailure( | ||
id=id, | ||
metric=target_metric, | ||
details=f"Value {value} is below or equal to threshold {lower_threshold_inclusive}", | ||
value=value, | ||
lower_threshold=lower_threshold_inclusive, | ||
) | ||
] | ||
|
||
return [] | ||
|
||
|
||
def _enforce_one_of(target_metric: str, one_of: Set[Union[str, float, int]], value: Any, id: str) -> Sequence[ValidationFailure]: | ||
if value not in one_of: | ||
return [ | ||
ValidationFailure( | ||
id=id, | ||
metric=target_metric, | ||
details=f"Value {value} is not in allowed values {one_of}", | ||
value=value, | ||
allowed_values=list(one_of), | ||
) | ||
] | ||
return [] | ||
|
||
|
||
def _enforce_none_of(target_metric: str, none_of: Set[Union[str, float, int]], value: Any, id: str) -> Sequence[ValidationFailure]: | ||
if value in none_of: | ||
return [ | ||
ValidationFailure( | ||
id=id, | ||
metric=target_metric, | ||
details=f"Value {value} is in disallowed values {none_of}", | ||
value=value, | ||
disallowed_values=list(none_of), | ||
) | ||
] | ||
return [] | ||
|
||
|
||
def _enforce_must_be_none(target_metric: str, value: Any, id: str) -> Sequence[ValidationFailure]: | ||
if value is not None: | ||
return [ | ||
ValidationFailure( | ||
id=id, | ||
metric=target_metric, | ||
details=f"Value {value} is not None", | ||
value=value, | ||
) | ||
] | ||
return [] | ||
|
||
|
||
def _enforce_must_be_non_none(target_metric: str, value: Any, id: str) -> Sequence[ValidationFailure]: | ||
if value is None: | ||
return [ | ||
ValidationFailure( | ||
id=id, | ||
metric=target_metric, | ||
details="Value is None", | ||
value=value, | ||
) | ||
] | ||
return [] | ||
|
||
|
||
class ConstraintValidator(Validator): | ||
def __init__( | ||
self, | ||
target_metric: str, | ||
upper_threshold: Optional[Union[float, int]] = None, | ||
upper_threshold_inclusive: Optional[Union[float, int]] = None, | ||
lower_threshold: Optional[Union[float, int]] = None, | ||
lower_threshold_inclusive: Optional[Union[float, int]] = None, | ||
one_of: Optional[Sequence[Union[str, float, int]]] = None, | ||
none_of: Optional[Sequence[Union[str, float, int]]] = None, | ||
must_be_non_none: Optional[bool] = None, | ||
must_be_none: Optional[bool] = None, | ||
): | ||
validation_functions: List[Callable[[Any, str], Sequence[ValidationFailure]]] = [] | ||
|
||
if upper_threshold is not None: | ||
validation_functions.append(partial(_enforce_upper_threshold, target_metric, upper_threshold)) | ||
if lower_threshold is not None: | ||
validation_functions.append(partial(_enforce_lower_threshold, target_metric, lower_threshold)) | ||
if upper_threshold_inclusive is not None: | ||
validation_functions.append(partial(_enforce_upper_threshold_inclusive, target_metric, upper_threshold_inclusive)) | ||
if lower_threshold_inclusive is not None: | ||
validation_functions.append(partial(_enforce_lower_threshold_inclusive, target_metric, lower_threshold_inclusive)) | ||
if one_of is not None: | ||
validation_functions.append(partial(_enforce_one_of, target_metric, set(one_of))) | ||
if none_of is not None: | ||
validation_functions.append(partial(_enforce_none_of, target_metric, set(none_of))) | ||
if must_be_non_none is not None: | ||
validation_functions.append(partial(_enforce_must_be_non_none, target_metric)) | ||
if must_be_none is not None: | ||
validation_functions.append(partial(_enforce_must_be_none, target_metric)) | ||
|
||
self._target_metric = target_metric | ||
self._validation_functions = validation_functions | ||
|
||
if len(validation_functions) == 0: | ||
raise ValueError("At least one constraint must be provided") | ||
|
||
def get_target_metric_names(self) -> List[str]: | ||
return [self._target_metric] | ||
|
||
def validate_result(self, df: pd.DataFrame) -> Optional[ValidationResult]: | ||
failures: List[ValidationFailure] = [] | ||
for _index, row in df.iterrows(): # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType] | ||
id = str(row["id"]) # pyright: ignore[reportUnknownArgumentType] | ||
value: Any = row[self._target_metric] | ||
if isinstance(value, pd.Series) and value.size == 1: | ||
value = value.item() | ||
elif isinstance(value, np.ndarray) and value.size == 1: | ||
value = value.item() | ||
|
||
for validation_function in self._validation_functions: | ||
failures.extend(validation_function(value, id)) | ||
|
||
if len(failures) == 0: | ||
return None | ||
|
||
return ValidationResult(failures) |
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,30 @@ | ||
from typing import Optional, Sequence, Union | ||
|
||
from langkit.core.validation import Validator | ||
from langkit.validators.comparison import ConstraintValidator | ||
|
||
|
||
class lib: | ||
@staticmethod | ||
def constraint( | ||
target_metric: str, | ||
upper_threshold: Optional[float] = None, | ||
upper_threshold_inclusive: Optional[float] = None, | ||
lower_threshold: Optional[float] = None, | ||
lower_threshold_inclusive: Optional[float] = None, | ||
one_of: Optional[Sequence[Union[str, float, int]]] = None, | ||
none_of: Optional[Sequence[Union[str, float, int]]] = None, | ||
must_be_non_none: Optional[bool] = None, | ||
must_be_none: Optional[bool] = None, | ||
) -> Validator: | ||
return ConstraintValidator( | ||
target_metric=target_metric, | ||
upper_threshold=upper_threshold, | ||
upper_threshold_inclusive=upper_threshold_inclusive, | ||
lower_threshold=lower_threshold, | ||
lower_threshold_inclusive=lower_threshold_inclusive, | ||
one_of=one_of, | ||
none_of=none_of, | ||
must_be_non_none=must_be_non_none, | ||
must_be_none=must_be_none, | ||
) |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "langkit" | ||
version = "0.0.69" | ||
version = "0.0.70" | ||
description = "A language toolkit for monitoring LLM interactions" | ||
authors = ["WhyLabs.ai <[email protected]>"] | ||
homepage = "https://docs.whylabs.ai/docs/large-language-model-monitoring" | ||
|
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
Oops, something went wrong.