Overview
Concrete tool for providing a unified KServe model interface that routes explanation requests to the appropriate Alibi algorithm provided by the KServe sample code.
Description
This module defines two classes: ExplainerMethod (an Enum with values anchor_tabular, anchor_images, and anchor_text) and AlibiExplainer (which extends kserve.Model). The AlibiExplainer constructor accepts a model name, predictor host, explainer method, configuration mapping, and an optional pre-built explainer object. Based on the selected ExplainerMethod, it instantiates the corresponding wrapper class (AnchorTabular, AnchorImages, or AnchorText) with an internal _predict_fn that calls the predictor service. The explain() method delegates to the selected wrapper, converts the Alibi Explanation result to JSON, and returns it as a dictionary. The _predict_fn uses nest_asyncio to bridge synchronous Alibi calls with the async KServe prediction pipeline.
Usage
Use this class as the main explainer model server when deploying an Alibi-based explanation service alongside a KServe predictor, supporting tabular, image, or text anchor explanations.
Code Reference
Source Location
Signature
class ExplainerMethod(Enum):
anchor_tabular = "AnchorTabular"
anchor_images = "AnchorImages"
anchor_text = "AnchorText"
class AlibiExplainer(kserve.Model):
def __init__(
self,
name: str,
predictor_host: str,
method: ExplainerMethod,
config: Mapping,
explainer: object = None,
):
...
def _predict_fn(self, arr: Union[np.ndarray, List]) -> np.ndarray:
...
def explain(self, payload: Dict, headers: Dict[str, str] = None) -> Any:
...
Import
from alibiexplainer.explainer import AlibiExplainer, ExplainerMethod
I/O Contract
Inputs
Constructor
| Name |
Type |
Required |
Description
|
| name |
str |
Yes |
Model name for KServe routing
|
| predictor_host |
str |
Yes |
Hostname of the predictor service to send prediction requests to
|
| method |
ExplainerMethod |
Yes |
The Alibi explanation algorithm to use (anchor_tabular, anchor_images, or anchor_text)
|
| config |
Mapping |
Yes |
Configuration parameters passed to the selected explanation wrapper
|
| explainer |
object |
No |
Optional pre-built Alibi explainer object (default: None)
|
explain()
| Name |
Type |
Required |
Description
|
| payload |
Dict |
Yes |
Dictionary with "instances" key containing the data to explain
|
| headers |
Dict[str, str] |
No |
Optional HTTP headers
|
Outputs
explain()
| Name |
Type |
Description
|
| explanation |
Dict |
JSON-serializable dictionary containing the Alibi explanation result
|
Usage Examples
Basic Usage
from alibiexplainer.explainer import AlibiExplainer, ExplainerMethod
# Create an Alibi explainer for tabular data
explainer = AlibiExplainer(
name="income-explainer",
predictor_host="income-predictor.default.svc.cluster.local",
method=ExplainerMethod.anchor_tabular,
config={"threshold": 0.95},
explainer=pretrained_anchor_tabular,
)
# Handle an explain request
payload = {"instances": [[25, 50000, 0.8]]}
result = explainer.explain(payload)
print(result) # {"meta": {...}, "data": {"anchor": ["income > 40000"], ...}}
Related Pages