The MetricBase class provides an interface to define custom metrics. Four additional default classes can help you create custom metrics: ModelMetricBase, DataMetricBase, LLMMetricBase, and SklearnMetric.
LLMMetricBase is the base class for LLM metrics that require scoring data and predictions for metric calculation, otherwise known as prompts (the user input) and completions (the LLM response).
To accelerate the implementation of custom metrics, you can use ready-made, proven metrics from Sklearn. Provide the name of a metric, using the SklearnMetric class as the base class, to create a custom metric. For example:
fromdmm.metric.sklearn_metricimportSklearnMetricclassMedianAbsoluteError(SklearnMetric):""" Metric that calculates the median absolute error of the difference between predictions and actuals """def__init__(self):super().__init__(metric="median_absolute_error",)
The PromptSimilarityMetricBase class compares the LLM prompt and context vectors. This class is generally used with Text Generation models where the prompt and context vectors are populated as described below:
The base class pulls the vectors from the scoring_data, and iterates over each entry:
The prompt vector is pulled from the prompt_column (which defaults to _LLM_PROMPT_VECTOR) of the scoring_data.
The context vectors are pulled from the context_column (which defaults to _LLM_CONTEXT) of the scoring_data. The context column contains a list of context dictionaries, and each context needs to have a vector element.
Note
Both the prompt_column and context_column are expected to be JSON-encoded data.
A derived class must implement calculate_distance(). For this class, score() is already implemented.
The calculate_distance function returns a single floating point value based on a single prompt_vector and a list of context_vectors.
For an example using the PromptSimilarityMetricBase, review the code below calculating the minimum Euclidean distance:
fromdmm.metricimportPromptSimilarityMetricBaseclassEuclideanMinMetric(PromptSimilarityMetricBase):"""Calculate the minimum Euclidean distance between a prompt vector and a list of context vectors"""defcalculate_distance(self,prompt_vector:np.ndarray,context_vectors:List[np.ndarray])->float:distances=[np.linalg.norm(prompt_vector-context_vector)forcontext_vectorincontext_vectors]returnmin(distances)# Instantiation could look like thisscorer=EuclideanMinMetric(name=custom_metric.name,description="Euclidean minimum distance between prompt and context vectors")
The metrics described above provide the source of the custom metric definitions. Use the CustomMetric interface to retrieve the metadata of an existing custom metric in DataRobot and to report data to that custom metric. Initialize the metric by providing the parameters explicitly (metric_id, deployment_id, model_id, DataRobotClient()):
The report method submits custom metric values to a custom metric defined in DataRobot. To use this method, report a DataFrame in the shape of the output from the metric evaluator.
The dry_run parameter determines if the custom metric values transfer is a dry run (the values aren't saved in the database) or if it is a production data transfer.
This parameter is False by default (the values are saved).