pyrit.score.Scorer#

class Scorer(*, validator: ScorerPromptValidator)[source]#

Bases: ABC

Abstract base class for scorers.

__init__(*, validator: ScorerPromptValidator)[source]#

Initialize the Scorer.

Parameters:

validator (ScorerPromptValidator) – Validator for message pieces and scorer configuration.

Methods

__init__(*, validator)

Initialize the Scorer.

get_identifier()

Get an identifier dictionary for the scorer.

get_scorer_metrics(dataset_name[, metrics_type])

Get evaluation statistics for the scorer using the dataset_name of the human labeled dataset.

scale_value_float(value, min_value, max_value)

Scales a value from 0 to 1 based on the given min and max values.

score_async(message, *[, objective, ...])

Score the message, add the results to the database, and return a list of Score objects.

score_image_async(image_path, *[, objective])

Score the given image using the chat target.

score_image_batch_async(*, image_paths[, ...])

Score a batch of images asynchronously.

score_prompts_batch_async(*, messages[, ...])

Score multiple prompts in batches using the provided objectives.

score_response_async(*, response[, ...])

Score a response using an objective scorer and optional auxiliary scorers.

score_response_multiple_scorers_async(*, ...)

Score a response using multiple scorers in parallel.

score_text_async(text, *[, objective])

Scores the given text based on the task using the chat target.

validate_return_scores(scores)

Validate the scores returned by the scorer.

Attributes

get_identifier()[source]#

Get an identifier dictionary for the scorer.

Returns:

The identifier dictionary containing class type, module, and sub-identifiers.

Return type:

dict

get_scorer_metrics(dataset_name: str, metrics_type: MetricsType | None = None)[source]#

Get evaluation statistics for the scorer using the dataset_name of the human labeled dataset.

This scorer was run against. If you did not evaluate the scorer against your own human labeled dataset, you can use this method to retrieve metrics based on a pre-existing dataset name, which is often a ‘harm_category’ or abbreviated version of the ‘objective’. For example, to retrieve metrics for the ‘hate_speech’ harm, you would pass ‘hate_speech’ as the dataset_name.

The existing metrics can be found in the ‘dataset/score/scorer_evals’ directory within either the ‘harm’ or ‘objective’ subdirectory.

Parameters:
  • dataset_name (str) – The name of the dataset on which the scorer evaluation was run. This is used to inform the name of the metrics file to read in the scorer_evals directory.

  • metrics_type (MetricsType, optional) – The type of metrics to retrieve, either HARM or OBJECTIVE. If not provided, it will default to OBJECTIVE for true/false scorers and HARM for all other scorers.

Returns:

A ScorerMetrics object containing the saved evaluation statistics for the scorer.

Return type:

ScorerMetrics

scale_value_float(value: float, min_value: float, max_value: float) float[source]#

Scales a value from 0 to 1 based on the given min and max values. E.g. 3 stars out of 5 stars would be .5.

Parameters:
  • value (float) – The value to be scaled.

  • min_value (float) – The minimum value of the range.

  • max_value (float) – The maximum value of the range.

Returns:

The scaled value.

Return type:

float

async score_async(message: Message, *, objective: str | None = None, role_filter: Literal['system', 'user', 'assistant', 'tool', 'developer'] | None = None, skip_on_error_result: bool = False, infer_objective_from_request: bool = False) list[Score][source]#

Score the message, add the results to the database, and return a list of Score objects.

Parameters:
  • message (Message) – The message to be scored.

  • objective (Optional[str]) – The task or objective based on which the message should be scored. Defaults to None.

  • role_filter (Optional[ChatMessageRole]) – Only score messages with this role. Defaults to None.

  • skip_on_error_result (bool) – If True, skip scoring if the message contains an error. Defaults to False.

  • infer_objective_from_request (bool) – If True, infer the objective from the message’s previous request when objective is not provided. Defaults to False.

Returns:

A list of Score objects representing the results.

Return type:

list[Score]

Raises:
  • PyritException – If scoring raises a PyRIT exception (re-raised with enhanced context).

  • RuntimeError – If scoring raises a non-PyRIT exception (wrapped with scorer context).

async score_image_async(image_path: str, *, objective: str | None = None) list[Score][source]#

Score the given image using the chat target.

Parameters:
  • image_path (str) – The path to the image file to be scored.

  • objective (Optional[str]) – The objective based on which the image should be scored. Defaults to None.

Returns:

A list of Score objects representing the results.

Return type:

list[Score]

async score_image_batch_async(*, image_paths: Sequence[str], objectives: Sequence[str] | None = None, batch_size: int = 10) list[Score][source]#

Score a batch of images asynchronously.

Parameters:
  • image_paths (Sequence[str]) – Sequence of paths to image files to be scored.

  • objectives (Optional[Sequence[str]]) – Optional sequence of objectives corresponding to each image. If provided, must match the length of image_paths. Defaults to None.

  • batch_size (int) – Maximum number of images to score concurrently. Defaults to 10.

Returns:

A list of Score objects representing the scoring results for all images.

Return type:

list[Score]

Raises:

ValueError – If the number of objectives does not match the number of image_paths.

async score_prompts_batch_async(*, messages: Sequence[Message], objectives: Sequence[str] | None = None, batch_size: int = 10, role_filter: Literal['system', 'user', 'assistant', 'tool', 'developer'] | None = None, skip_on_error_result: bool = False, infer_objective_from_request: bool = False) list[Score][source]#

Score multiple prompts in batches using the provided objectives.

Parameters:
  • messages (Sequence[Message]) – The messages to be scored.

  • objectives (Sequence[str]) – The objectives/tasks based on which the prompts should be scored. Must have the same length as messages.

  • batch_size (int) – The maximum batch size for processing prompts. Defaults to 10.

  • role_filter (Optional[ChatMessageRole]) – If provided, only score pieces with this role. Defaults to None (no filtering).

  • skip_on_error_result (bool) – If True, skip scoring pieces that have errors. Defaults to False.

  • infer_objective_from_request (bool) – If True and objective is empty, attempt to infer the objective from the request. Defaults to False.

Returns:

A flattened list of Score objects from all scored prompts.

Return type:

list[Score]

Raises:

ValueError – If objectives is empty or if the number of objectives doesn’t match the number of messages.

async static score_response_async(*, response: Message, objective_scorer: Scorer | None = None, auxiliary_scorers: List[Scorer] | None = None, role_filter: Literal['system', 'user', 'assistant', 'tool', 'developer'] = 'assistant', objective: str | None = None, skip_on_error_result: bool = True) Dict[str, List[Score]][source]#

Score a response using an objective scorer and optional auxiliary scorers.

Parameters:
  • response (Message) – Response containing pieces to score.

  • objective_scorer (Optional[Scorer]) – The main scorer to determine success. Defaults to None.

  • auxiliary_scorers (Optional[List[Scorer]]) – List of auxiliary scorers to apply. Defaults to None.

  • role_filter (ChatMessageRole) – Only score pieces with this role. Defaults to “assistant”.

  • objective (Optional[str]) – Task/objective for scoring context. Defaults to None.

  • skip_on_error_result (bool) – If True, skip scoring pieces that have errors. Defaults to True.

Returns:

Dictionary with keys auxiliary_scores and objective_scores

containing lists of scores from each type of scorer.

Return type:

Dict[str,List[Score]]

Raises:

ValueError – If response is not provided.

async static score_response_multiple_scorers_async(*, response: Message, scorers: List[Scorer], role_filter: Literal['system', 'user', 'assistant', 'tool', 'developer'] = 'assistant', objective: str | None = None, skip_on_error_result: bool = True) List[Score][source]#

Score a response using multiple scorers in parallel.

This method applies each scorer to the first scorable response piece (filtered by role and error), and returns all scores. This is typically used for auxiliary scoring where all results are needed.

Parameters:
  • response (Message) – The response containing pieces to score.

  • scorers (List[Scorer]) – List of scorers to apply.

  • role_filter (ChatMessageRole) – Only score pieces with this role (default: “assistant”).

  • objective (Optional[str]) – Optional objective description for scoring context.

  • skip_on_error_result (bool) – If True, skip scoring pieces that have errors (default: True).

Returns:

All scores from all scorers

Return type:

List[Score]

async score_text_async(text: str, *, objective: str | None = None) list[Score][source]#

Scores the given text based on the task using the chat target.

Parameters:
  • text (str) – The text to be scored.

  • objective (str) – The task based on which the text should be scored

Returns:

A list of Score objects representing the results.

Return type:

list[Score]

scorer_type: Literal['true_false', 'float_scale']#
abstract validate_return_scores(scores: list[Score])[source]#

Validate the scores returned by the scorer. Because some scorers may require specific Score types or values.

Parameters:

scores (list[Score]) – The scores to be validated.