The Patronus AI Experimentation Framework lets you to write your own evaluators that can run locally, and use remote Patronus-hosted Evaluators. This tutorial will show you how to write your own evaluators and how to use them with Patronus Evaluators.
See Evals; for an in-depth walk-through on Evaluators.
Let's define a simple evaluator that will compare the model output to the gold answer. (This evaluator is case insensitive and ignores leading and trailing whitespaces.)
To define a function-based evaluator, you need to wrap your evaluator function with the @evaluator decorator. The function can accept any of the parameters described in the Evaluator Definition section. The framework will automatically inject the appropriate values based on the parameter names you specify.
In its simplest form, as shown in the example above, an evaluator can return a boolean value. The framework will automatically convert this into an EvaluationResult object. For more complex evaluations, you can return any of the supported return types described in the Evaluator Definition section.
For a more complex example, we'll use BERTScore to measure embedding similarity. BERTScore measures the cosine similarity between two BERT embeddings, which can be used to compare string similarity. In our case, we want to compare the model's output to the gold answer. The output doesn't need to be an exact match, but it should be close. Additionally, we want to be able to set a threshold to determine whether the evaluation passes or not.
Before we can start we need to install the Transformers and PyTorch dependencies.
Now we can write our class-based evaluator.
A class-based evaluator needs to inherit from the Evaluator base class and must implement the evaluate() method. Similar to the function-based evaluator, the evaluate() method only accepts predefined parameter names.
The return type of the evaluate() method can be a bool or an EvaluationResult object, as shown in this example. The EvaluationResult object provides a more detailed assessment by including:
score_raw: The calculated score reflecting the similarity between the evaluated output and the gold answer. While the score is often normalized between 0 and 1, with 1 representing an exact match, it doesn’t have to be normalized.
pass_: A boolean indicating whether the score meets the pass threshold.
tags: A dictionary that can store additional metadata as key-value pairs, such as the threshold used during the evaluation.
If the return type is a boolean, that is equivalent to returning an EvaluationResult object with only the pass_ value set.
The Patronus Experimentation Framework supports both synchronous and asynchronous evaluators. Defining an asynchronous evaluator is as simple as using Python's async functions.
Patronus Evaluators are remotely hosted evaluators developed and maintained by the Patronus AI team. The following section describes how to use these evaluators in your evaluation workflow, and combine them with local evaluators.
Patronus remote evaluators expect your dataset to use the standard field names (evaluated_model_input, evaluated_model_output, etc.). If your dataset uses different field names, or you wish to use it with chaining, you can use the RemoteEvaluator.wrap decorator to map your fields to the expected names.
Using Patronus Evaluators is straightforward. For Evaluators that do not require Profiles, you can reference them by simply providing an ID or an alias:
Below is an example of how to use the pii evaluator to detect personally identifiable information (PII) in model outputs. The evaluator is referenced by its alias:
For evaluators that require Profiles, such as the Judge Evaluator, you must provide the profile_name along with the evaluator's ID or alias.
In this example, the evaluator "judge-large" is referenced with a specific system profile managed by the Patronus AI team, which is available in your account out of the box.
If you need to create a profile from code, which is often required for a Judge Evaluator, you can specify its profile configuration directly in your code:
If you attempt to tweak the profile configuration for an existing profile, it will throw an error. This safeguard is in place because modifying a profile can be risky. Changes may impact other users in your account who rely on the same profile or could disrupt a production workload that is relying on that profile.
To update an evaluator profile, you must explicitly pass the allow_update=True argument:.
The RemoteEvaluator.wrap decorator allows you to customize how remote evaluators interact with your data. This is particularly useful when you need to:
Map custom field names to standard Patronus fields
Conditionally skip evaluations based on your data
Preprocess data before evaluation
Work with chained evaluation results
Here's an example that demonstrates these capabilities:
Remote evaluators can be used directly without running a full experiment. This is useful for real-time evaluations, testing, or when you need to integrate evaluations into your own workflows.
There are two ways to use remote evaluators:
Using the Client.remote_evaluator() method (recommended):
The remote_evaluator method instantiates a RemoteEvaluator class that provides additional features like creating and updating evaluation criteria in code and automatic retries. Note that the first call using a remote evaluator may be slower as it needs to verify its configuration.
Using the lower-level API directly:
This direct API approach is more lightweight but doesn't provide the additional features available through the RemoteEvaluator class.
When creating an evaluator, whether function-based or class-based, you can access various parameters that provide context about the evaluation. These parameters must be named exactly as specified below:
row (patronus.Row): The complete row from the dataset, extending pandas.Series. It provides helper properties for well-defined fields like evaluated_model_input, evaluated_model_output, etc., making data access more convenient.
task_result (patronus.TaskResult): The result object returned by the task execution.
evaluated_model_system_prompt
evaluated_model_retrieved_context
evaluated_model_input
evaluated_model_output
evaluated_model_gold_answer
parent: Reference to results from previous chain links. This parameter is only available when using evaluation chaining in your experiment.
📘 It is recommended to use the evaluated_model_* fields names, as these fields are properly tracked and reported to the Patronus platform. While the raw data is available through the row parameter, using the dedicated fields ensures better visibility and reporting of your evaluation process.
Evaluators can return different types of values, which are automatically coerced into an EvaluationResult object:
bool: A simple pass/fail evaluation
float, int: A raw score value
EvaluationResult: A complete evaluation result object
The EvaluationResult class is defined as follows:
When returning a boolean, float, or int, the framework automatically converts it to an EvaluationResult:
A boolean return value sets the pass_ field
A float return value sets the score_raw field
For more complex evaluations, you can return an EvaluationResult object directly, allowing you to provide additional context through metadata and tags. Note that the metadata field is only available locally and is not reported to the Patronus platform at this moment. You can access this data by exporting evaluation results to CSV after the evaluation is complete. If you need to report structured data to the platform, use the tags field instead.
Here's an example that demonstrates using various evaluator parameters: