Models reference

The Model class is a core component of Giskard Open Source that wraps different types of LLM models and AI systems for testing and evaluation.

To scan, test and debug your model, you need to wrap it into a Giskard Model. Your model can use any ML library (sklearn, catboost, pytorch, tensorflow, huggingface and langchain) and can be any Python function that respects the right signature.

You can wrap your model in two different ways:

  1. Wrap a prediction function that contains all your data pre-processing steps. Prediction function is any Python function that takes input as raw pandas dataframe and returns the probabilities for each classification labels (classification) or predictions (regression or text_generation).

    Make sure that:

    • prediction_function encapsulates all the data pre-processing steps (categorical encoding, numerical scaling, etc.).

    • prediction_function(df[feature_names]) does not return an error message.

  2. Wrap a model object in addition to a data pre-processing function. Providing the model object to Model allows us to automatically infer the ML library of your model object and provide a suitable serialization method (provided by save_model and load_model methods).

    This requires:

    • Mandatory: Overriding the model_predict method which should take the input as raw pandas dataframe and return

    the probabilities for each classification labels (classification) or predictions (regression or text_generation). - Optional: Our pre-defined serialization and prediction methods cover the sklearn, catboost, pytorch, tensorflow, huggingface and langchain libraries. If none of these libraries are detected, cloudpickle is used as the default for serialization. If this fails, we will ask you to also override the save_model and load_model methods where you provide your own serialization of the model object.

Integrations

class giskard.models.langchain.LangchainModel(model, model_type: SupportedModelTypes, name: str | None = None, description: str | None = None, data_preprocessing_function: Callable[[DataFrame], Any] | None = None, model_postprocessing_function: Callable[[Any], Any] | None = None, feature_names: Iterable | None = None, classification_threshold: float | None = 0.5, classification_labels: Iterable | None = None, **kwargs)[source]
classmethod load_model(local_dir, model_py_ver: Tuple[str, str, str] | None = None, *args, **kwargs)[source]

Loads the wrapped model object.

Parameters:
  • path (Union[str, Path]) – Path from which the model should be loaded.

  • model_py_ver (Optional[Tuple[str, str, str]]) – Python version used to save the model, to validate if model loading failed.

model_predict(df)[source]

Performs the model inference/forward pass.

Parameters:

data (Any) – The input data for making predictions. If you did not specify a data_preprocessing_function, this will be a pd.DataFrame, otherwise it will be whatever the data_preprocessing_function returns.

Returns:

If the model is classification, it should return an array of probabilities of shape (num_entries, num_classes). If the model is regression or text_generation, it should return an array of num_entries predictions.

Return type:

numpy.ndarray

save_model(local_path: str | Path, *args, **kwargs) None[source]

Saves the wrapped model object.

Parameters:

path (Union[str, Path]) – Path to which the model should be saved.

The giskard.Model class

class giskard.Model(model: Any, model_type: SupportedModelTypes | Literal['classification', 'regression', 'text_generation'], data_preprocessing_function: Callable[[DataFrame], Any] | None = None, model_postprocessing_function: Callable[[Any], Any] | None = None, name: str | None = None, description: str | None = None, feature_names: Iterable | None = None, classification_threshold: float | None = 0.5, classification_labels: Iterable | None = None, **kwargs)[source]
Parameters:
  • model (Any) – Could be any function or ML model. The standard model output required for Giskard is: * if classification: an array (nxm) of probabilities corresponding to n data entries (rows of pandas.DataFrame) and m classification_labels. In the case of binary classification, an array of (nx1) probabilities is also accepted. Make sure that the probability provided is for the second label provided in classification_labels. * if regression or text_generation: an array of predictions corresponding to data entries (rows of pandas.DataFrame) and outputs.

  • name (Optional[str]) – Name of the model.

  • description (Optional[str]) – Description of the model’s task. Mandatory for non-langchain text_generation models.

  • model_type (ModelType) – The type of the model: regression, classification or text_generation.

  • data_preprocessing_function (Optional[Callable[[pd.DataFrame]) – A function that takes a pandas.DataFrame as raw input, applies preprocessing and returns any object that could be directly fed to clf. You can also choose to include your preprocessing inside clf, in which case no need to provide this argument.

  • model_postprocessing_function (Optional[Callable[[Any]) – A function that takes a clf output as input, applies postprocessing and returns an object of the same type and shape as the clf output.

  • feature_names (Optional[Iterable[str]]) – list of feature names matching the column names in the data that correspond to the features which the model trained on. By default, feature_names are all the Dataset columns except from target.

  • classification_threshold (float) – represents the classification model threshold, for binary classification models.

  • classification_labels (Optional[Iterable[str]]) – that represents the classification labels, if model_type is classification. Make sure the labels have the same order as the column output of clf.

  • **kwargs – Additional keyword arguments.

static __new__(cls, model: Any, model_type: SupportedModelTypes | Literal['classification', 'regression', 'text_generation'], data_preprocessing_function: Callable[[DataFrame], Any] | None = None, model_postprocessing_function: Callable[[Any], Any] | None = None, name: str | None = None, description: str | None = None, feature_names: Iterable | None = None, classification_threshold: float | None = 0.5, classification_labels: Iterable | None = None, **kwargs)[source]

Used for dynamical inheritance and returns one of the following class instances: PredictionFunctionModel, SKLearnModel, CatboostModel, HuggingFaceModel, PyTorchModel, TensorFlowModel or LangchainModel, depending on the ML library detected in the model object. If the model object provided does not belong to one of these libraries, an instance of CloudpickleSerializableModel is returned in which case:

  1. the default serialization method used will be cloudpickle

  2. you will be asked to provide your own model_predict method.

save_model(local_path: str | Path, *args, **kwargs) None[source]

Saves the wrapped model object.

Parameters:

path (Union[str, Path]) – Path to which the model should be saved.

abstract model_predict(data)[source]

Performs the model inference/forward pass.

Parameters:

data (Any) – The input data for making predictions. If you did not specify a data_preprocessing_function, this will be a pd.DataFrame, otherwise it will be whatever the data_preprocessing_function returns.

Returns:

If the model is classification, it should return an array of probabilities of shape (num_entries, num_classes). If the model is regression or text_generation, it should return an array of num_entries predictions.

Return type:

numpy.ndarray

classmethod load_model(local_dir, model_py_ver: Tuple[str, str, str] | None = None, *args, **kwargs)[source]

Loads the wrapped model object.

Parameters:
  • path (Union[str, Path]) – Path from which the model should be loaded.

  • model_py_ver (Optional[Tuple[str, str, str]]) – Python version used to save the model, to validate if model loading failed.