diff --git a/pyrit/prompt_normalizer/prompt_normalizer.py b/pyrit/prompt_normalizer/prompt_normalizer.py index 6bb364b47c..a722f8298b 100644 --- a/pyrit/prompt_normalizer/prompt_normalizer.py +++ b/pyrit/prompt_normalizer/prompt_normalizer.py @@ -14,7 +14,7 @@ from contextvars import copy_context from functools import partial from pathlib import Path -from typing import Any, TypeVar, cast +from typing import Any, TypeVar from uuid import uuid4 from pyrit.common.attack_result_scope import get_current_attack_result_id @@ -331,7 +331,7 @@ async def send_prompt_batch_to_target_async( "send_context", ] - responses = await batch_task_async( + return await batch_task_async( prompt_target=target, batch_size=batch_size, items_to_batch=batch_items, @@ -339,8 +339,6 @@ async def send_prompt_batch_to_target_async( task_arguments=batch_item_keys, target=target, ) - # Each batch result is the Message returned by send_prompt_async. - return cast("list[Message]", responses) async def convert_values_async( self, diff --git a/pyrit/prompt_target/batch_helper.py b/pyrit/prompt_target/batch_helper.py index b7a3716a92..53cd3ad3d8 100644 --- a/pyrit/prompt_target/batch_helper.py +++ b/pyrit/prompt_target/batch_helper.py @@ -1,12 +1,14 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT license. -from collections.abc import Callable, Generator, Sequence -from typing import Any +from collections.abc import Awaitable, Callable, Generator, Sequence +from typing import Any, TypeVar from pyrit.common.task_utils import gather_with_cleanup_async from pyrit.prompt_target.common.prompt_target import PromptTarget +_BatchResult = TypeVar("_BatchResult") + def _get_chunks(*args: Sequence[Any], batch_size: int) -> Generator[list[Sequence[Any]], None, None]: """ @@ -70,10 +72,10 @@ async def batch_task_async( prompt_target: PromptTarget | None = None, batch_size: int, items_to_batch: Sequence[Sequence[Any]], - task_func: Callable[..., Any], + task_func: Callable[..., Awaitable[_BatchResult]], task_arguments: list[str], **task_kwargs: Any, -) -> list[Any]: +) -> list[_BatchResult]: """ Perform provided task in batches and validate parameters using helpers. @@ -89,14 +91,14 @@ async def batch_task_async( **task_kwargs: Any other keyword arguments that task needs Returns: - responses(list): List of results from the batched function + list[_BatchResult]: One ``task_func`` result per batched item, in input order. Raises: ValueError: When the batch size is not an integer of at least 1. ValueError: When no items to batch are provided. ValueError: When number of lists of items to batch does not match number of task arguments. """ - responses = [] + responses: list[_BatchResult] = [] _validate_batch_size(batch_size) _validate_rate_limit_parameters(prompt_target=prompt_target, batch_size=batch_size) diff --git a/pyrit/score/scorer.py b/pyrit/score/scorer.py index 66bb484e89..0a674180e8 100644 --- a/pyrit/score/scorer.py +++ b/pyrit/score/scorer.py @@ -1642,7 +1642,7 @@ async def _score_batch_with_task_async( **task_kwargs, ) - # results is a list[list[Score]] and needs to be flattened + # Flatten the per-scorable score lists into one list return [score for sublist in results for score in sublist] async def score_image_batch_async( diff --git a/pyrit/score/scorer_evaluation/scorer_evaluator.py b/pyrit/score/scorer_evaluation/scorer_evaluator.py index 63ce975dcd..9bf431d2e7 100644 --- a/pyrit/score/scorer_evaluation/scorer_evaluator.py +++ b/pyrit/score/scorer_evaluation/scorer_evaluator.py @@ -518,7 +518,7 @@ async def _score_responses_grouped_async( Returns: list[list[Score]]: One score list for each response. """ - results = await batch_task_async( + return await batch_task_async( task_func=self.scorer.score_async, task_arguments=["scorable", "expectation"], prompt_target=self.scorer.get_chat_target(), @@ -528,7 +528,6 @@ async def _score_responses_grouped_async( [ScoringExpectation(objective=objective) for objective in objectives], ], ) - return cast("list[list[Score]]", results) @staticmethod def _score_matches_harm_category(*, score: Score, harm_category: str) -> bool: