Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pyrit/prompt_normalizer/prompt_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -331,16 +331,14 @@ 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,
task_func=self.send_prompt_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,
Expand Down
14 changes: 8 additions & 6 deletions pyrit/prompt_target/batch_helper.py
Original file line number Diff line number Diff line change
@@ -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]:
"""
Expand Down Expand Up @@ -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.

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyrit/score/scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions pyrit/score/scorer_evaluation/scorer_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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:
Expand Down