pyrit.score.AzureContentFilterScorer#
- class AzureContentFilterScorer(*, endpoint: str | None = None, api_key: str | Callable[[], str | Awaitable[str]] | None = None, harm_categories: list[TextCategory] | None = None, validator: ScorerPromptValidator | None = None)[source]#
Bases:
FloatScaleScorerA scorer that uses Azure Content Safety API to evaluate text and images for harmful content.
This scorer analyzes content across multiple harm categories (hate, self-harm, sexual, violence) and returns a score for each category in the range [0, 1], where higher scores indicate more severe content. Supports both text and image inputs.
- __init__(*, endpoint: str | None = None, api_key: str | Callable[[], str | Awaitable[str]] | None = None, harm_categories: list[TextCategory] | None = None, validator: ScorerPromptValidator | None = None) None[source]#
Initialize an Azure Content Filter Scorer.
- Parameters:
endpoint (Optional[str | None]) – The endpoint URL for the Azure Content Safety service. Defaults to the ENDPOINT_URI_ENVIRONMENT_VARIABLE environment variable.
api_key (Optional[str | Callable[[], str | Awaitable[str]] | None]) – The API key for accessing the Azure Content Safety service, or a callable that returns an access token. For Azure endpoints with Entra authentication, pass a token provider from pyrit.auth (e.g., get_azure_token_provider(’https://cognitiveservices.azure.com/.default’)). Defaults to the API_KEY_ENVIRONMENT_VARIABLE environment variable.
harm_categories (Optional[list[TextCategory]]) – The harm categories you want to query for as defined in azure.ai.contentsafety.models.TextCategory. If not provided, defaults to all categories.
validator (Optional[ScorerPromptValidator]) – Custom validator for the scorer. Defaults to None.
- Raises:
ValueError – If neither API key nor endpoint is provided, or if both are missing.
Methods
__init__(*[, endpoint, api_key, ...])Initialize an Azure Content Filter Scorer.
evaluate_async([file_mapping, ...])Evaluate this scorer against human-labeled datasets.
get_identifier()Get an identifier dictionary for the scorer for database storage.
get_scorer_metrics()Get evaluation metrics for this scorer from the configured evaluation result file.
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 that the returned scores are within the valid range [0, 1].
Attributes
evaluation_file_mappingscorer_identifierGet the scorer identifier.
scorer_typeGet the scorer type based on class hierarchy.
- MAX_TEXT_LENGTH = 10000#
- async evaluate_async(file_mapping: ScorerEvalDatasetFiles | None = None, *, num_scorer_trials: int = 3, update_registry_behavior: RegistryUpdateBehavior = None, max_concurrency: int = 10) ScorerMetrics | None[source]#
Evaluate this scorer against human-labeled datasets.
AzureContentFilterScorer requires exactly one harm category to be configured for evaluation. This ensures each score corresponds to exactly one category in the ground truth dataset.
- Parameters:
file_mapping – Optional ScorerEvalDatasetFiles configuration. If not provided, uses the mapping based on the configured harm category.
num_scorer_trials – Number of times to score each response. Defaults to 3.
update_registry_behavior – Controls how existing registry entries are handled. - SKIP_IF_EXISTS (default): Check registry for existing results. If found, return cached metrics. - ALWAYS_UPDATE: Always run evaluation and overwrite any existing registry entry. - NEVER_UPDATE: Always run evaluation but never write to registry (for debugging). Defaults to RegistryUpdateBehavior.SKIP_IF_EXISTS.
max_concurrency – Maximum concurrent scoring requests. Defaults to 10.
- Returns:
The evaluation metrics, or None if no datasets found.
- Return type:
- Raises:
ValueError – If more than one harm category is configured.