⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content
Merged
12 changes: 9 additions & 3 deletions pyrit/scenario/scenarios/airt/content_harms.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ContentHarms(Scenario):
with respect to certain harm categories.
"""

version: int = 1
VERSION: int = 1

@classmethod
def get_strategy_class(cls) -> Type[ScenarioStrategy]:
Expand Down Expand Up @@ -179,7 +179,7 @@ def __init__(

super().__init__(
name="Content Harms",
version=self.version,
version=self.VERSION,
objective_scorer=self._objective_scorer,
strategy_class=ContentHarmsStrategy,
scenario_result_id=scenario_result_id,
Expand Down Expand Up @@ -239,9 +239,15 @@ def _get_strategy_attacks(

Returns:
List[AtomicAttack]: The constructed AtomicAttack instances for each attack type.

Raises:
ValueError: If scenario is not properly initialized.
"""
# objective_target is guaranteed to be non-None by parent class validation
assert self._objective_target is not None
if self._objective_target is None:
raise ValueError(
"Scenario not properly initialized. Call await scenario.initialize_async() before running."
)

prompt_sending_attack = PromptSendingAttack(
objective_target=self._objective_target,
Expand Down
14 changes: 9 additions & 5 deletions pyrit/scenario/scenarios/airt/cyber.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Cyber(Scenario):
techniques.
"""

version: int = 1
VERSION: int = 1

@classmethod
def get_strategy_class(cls) -> type[ScenarioStrategy]:
Expand Down Expand Up @@ -141,7 +141,7 @@ def __init__(

super().__init__(
name="Cyber",
version=self.version,
version=self.VERSION,
strategy_class=CyberStrategy,
objective_scorer=objective_scorer,
include_default_baseline=include_baseline,
Expand Down Expand Up @@ -241,10 +241,13 @@ def _get_atomic_attack_from_strategy(self, strategy: str) -> AtomicAttack:
AtomicAttack: configured for the specified strategy.
Raises:
ValueError: if an unknown CyberStrategy is passed.
ValueError: If scenario is not properly initialized or an unknown CyberStrategy is passed.
"""
# objective_target is guaranteed to be non-None by parent class validation
assert self._objective_target is not None
if self._objective_target is None:
raise ValueError(
"Scenario not properly initialized. Call await scenario.initialize_async() before running."
)
attack_strategy: Optional[AttackStrategy[Any, Any]] = None
if strategy == "single_turn":
attack_strategy = PromptSendingAttack(
Expand All @@ -261,7 +264,8 @@ def _get_atomic_attack_from_strategy(self, strategy: str) -> AtomicAttack:
raise ValueError(f"Unknown CyberStrategy: {strategy}")

# _seed_groups is guaranteed to be set by _get_atomic_attacks_async before this method is called
assert self._seed_groups is not None, "_seed_groups must be resolved before creating atomic attacks"
if self._seed_groups is None:
raise ValueError("_seed_groups must be resolved before creating atomic attacks")

return AtomicAttack(
atomic_attack_name=f"cyber_{strategy}",
Expand Down
12 changes: 9 additions & 3 deletions pyrit/scenario/scenarios/airt/jailbreak.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Jailbreak(Scenario):
scored to determine if the jailbreak was successful.
"""

version: int = 1
VERSION: int = 1

@classmethod
def get_strategy_class(cls) -> type[ScenarioStrategy]:
Expand Down Expand Up @@ -114,7 +114,7 @@ def __init__(

super().__init__(
name="Jailbreak",
version=self.version,
version=self.VERSION,
strategy_class=JailbreakStrategy,
objective_scorer=objective_scorer,
include_default_baseline=include_baseline,
Expand Down Expand Up @@ -182,9 +182,15 @@ async def _get_atomic_attack_from_jailbreak_async(self, *, jailbreak_template_na

Returns:
AtomicAttack: An atomic attack using the specified jailbreak template.

Raises:
ValueError: If scenario is not properly initialized.
"""
# objective_target is guaranteed to be non-None by parent class validation
assert self._objective_target is not None
if self._objective_target is None:
raise ValueError(
"Scenario not properly initialized. Call await scenario.initialize_async() before running."
)

# Create the jailbreak converter
jailbreak_converter = TextJailbreakConverter(
Expand Down
11 changes: 7 additions & 4 deletions pyrit/scenario/scenarios/airt/leakage_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class LeakageScenario(Scenario):
attack variations designed to extract sensitive information from models.
"""

version: int = 1
VERSION: int = 1

@classmethod
def get_strategy_class(cls) -> type[ScenarioStrategy]:
Expand Down Expand Up @@ -161,7 +161,7 @@ def __init__(

super().__init__(
name="Leakage Scenario",
version=self.version,
version=self.VERSION,
strategy_class=LeakageStrategy,
objective_scorer=objective_scorer,
include_default_baseline=include_baseline,
Expand Down Expand Up @@ -261,10 +261,13 @@ async def _get_atomic_attack_from_strategy_async(self, strategy: str) -> AtomicA
AtomicAttack: Configured for the specified strategy.

Raises:
ValueError: If an unknown LeakageStrategy is passed.
ValueError: If scenario is not properly initialized or an unknown LeakageStrategy is passed.
"""
# objective_target is guaranteed to be non-None by parent class validation
assert self._objective_target is not None
if self._objective_target is None:
raise ValueError(
"Scenario not properly initialized. Call await scenario.initialize_async() before running."
)

strategy_factories = {
"first_letter": self._create_first_letter_attack,
Expand Down
11 changes: 7 additions & 4 deletions pyrit/scenario/scenarios/airt/scam.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Scam(Scenario):
(e.g., phishing emails, fraudulent messages) with primarily persuasion-oriented techniques.
"""

version: int = 1
VERSION: int = 1

@classmethod
def get_strategy_class(cls) -> type[ScenarioStrategy]:
Expand Down Expand Up @@ -167,7 +167,7 @@ def __init__(

super().__init__(
name="Scam",
version=self.version,
version=self.VERSION,
strategy_class=ScamStrategy,
objective_scorer=objective_scorer,
include_default_baseline=include_baseline,
Expand Down Expand Up @@ -266,10 +266,13 @@ def _get_atomic_attack_from_strategy(self, strategy: str) -> AtomicAttack:
AtomicAttack: Configured for the specified strategy.

Raises:
ValueError: If an unknown ScamStrategy is provided.
ValueError: If scenario is not properly initialized or an unknown ScamStrategy is provided.
"""
# objective_target is guaranteed to be non-None by parent class validation
assert self._objective_target is not None
if self._objective_target is None:
raise ValueError(
"Scenario not properly initialized. Call await scenario.initialize_async() before running."
)
attack_strategy: Optional[AttackStrategy[Any, Any]] = None

if strategy == "persuasive_rta":
Expand Down
4 changes: 2 additions & 2 deletions pyrit/scenario/scenarios/foundry/red_team_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class RedTeamAgent(Scenario):
providing a consistent PyRIT contract for their integration.
"""

version: int = 1
VERSION: int = 1

@classmethod
def get_strategy_class(cls) -> Type[ScenarioStrategy]:
Expand Down Expand Up @@ -297,7 +297,7 @@ def __init__(
# Call super().__init__() first to initialize self._memory
super().__init__(
name="RedTeamAgent",
version=self.version,
version=self.VERSION,
strategy_class=FoundryStrategy,
objective_scorer=objective_scorer,
include_default_baseline=include_baseline,
Expand Down
12 changes: 9 additions & 3 deletions pyrit/scenario/scenarios/garak/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Encoding(Scenario):
By default, this uses the same dataset as Garak: slur terms and web XSS payloads.
"""

version: int = 1
VERSION: int = 1

@classmethod
def get_strategy_class(cls) -> type[ScenarioStrategy]:
Expand Down Expand Up @@ -209,7 +209,7 @@ def __init__(

super().__init__(
name="Encoding",
version=self.version,
version=self.VERSION,
strategy_class=EncodingStrategy,
objective_scorer=objective_scorer,
include_default_baseline=include_baseline,
Expand Down Expand Up @@ -332,6 +332,9 @@ def _get_prompt_attacks(self, *, converters: list[PromptConverter], encoding_nam

Returns:
list[AtomicAttack]: List of atomic attacks for this encoding scheme.

Raises:
ValueError: If scenario is not properly initialized.
"""
converter_configs = [
AttackConverterConfig(
Expand All @@ -351,7 +354,10 @@ def _get_prompt_attacks(self, *, converters: list[PromptConverter], encoding_nam
atomic_attacks = []
for attack_converter_config in converter_configs:
# objective_target is guaranteed to be non-None by parent class validation
assert self._objective_target is not None
if self._objective_target is None:
raise ValueError(
"Scenario not properly initialized. Call await scenario.initialize_async() before running."
)
attack = PromptSendingAttack(
objective_target=self._objective_target,
attack_converter_config=attack_converter_config,
Expand Down
4 changes: 2 additions & 2 deletions pyrit/score/conversation_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ConversationScorer(Scorer, ABC):
Note: This class cannot be instantiated directly. Use create_conversation_scorer() factory instead.
"""

_default_validator: ScorerPromptValidator = ScorerPromptValidator(
_DEFAULT_VALIDATOR: ScorerPromptValidator = ScorerPromptValidator(
supported_data_types=["text"],
enforce_all_pieces_valid=True,
)
Expand Down Expand Up @@ -189,7 +189,7 @@ class DynamicConversationScorer(ConversationScorer, scorer_base_class): # type:

def __init__(self) -> None:
# Initialize with the validator and wrapped scorer
Scorer.__init__(self, validator=validator or ConversationScorer._default_validator)
Scorer.__init__(self, validator=validator or ConversationScorer._DEFAULT_VALIDATOR)
self._wrapped_scorer = scorer

def _get_wrapped_scorer(self) -> Scorer:
Expand Down
4 changes: 2 additions & 2 deletions pyrit/score/float_scale/azure_content_filter_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AzureContentFilterScorer(FloatScaleScorer):

MAX_TEXT_LENGTH = 10000 # Azure Content Safety API limit

_default_validator: ScorerPromptValidator = ScorerPromptValidator(
_DEFAULT_VALIDATOR: ScorerPromptValidator = ScorerPromptValidator(
supported_data_types=["text", "image_path"],
)

Expand Down Expand Up @@ -140,7 +140,7 @@ def __init__(
else:
raise ValueError("Please provide the Azure Content Safety endpoint and api_key")

super().__init__(validator=validator or self._default_validator)
super().__init__(validator=validator or self._DEFAULT_VALIDATOR)

@property
def _category_values(self) -> list[str]:
Expand Down
4 changes: 2 additions & 2 deletions pyrit/score/float_scale/insecure_code_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class InsecureCodeScorer(FloatScaleScorer):
Configuration is loaded from a YAML file for dynamic prompts and instructions.
"""

_default_validator: ScorerPromptValidator = ScorerPromptValidator(supported_data_types=["text"])
_DEFAULT_VALIDATOR: ScorerPromptValidator = ScorerPromptValidator(supported_data_types=["text"])

def __init__(
self,
Expand All @@ -38,7 +38,7 @@ def __init__(
Defaults to the default insecure code scoring prompt if not provided.
validator (Optional[ScorerPromptValidator]): Custom validator for the scorer. Defaults to None.
"""
super().__init__(validator=validator or self._default_validator)
super().__init__(validator=validator or self._DEFAULT_VALIDATOR)

self._prompt_target = chat_target

Expand Down
4 changes: 2 additions & 2 deletions pyrit/score/float_scale/plagiarism_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PlagiarismScorer(FloatScaleScorer):
3. Word-level n-gram Jaccard similarity
"""

_default_validator: ScorerPromptValidator = ScorerPromptValidator(supported_data_types=["text"])
_DEFAULT_VALIDATOR: ScorerPromptValidator = ScorerPromptValidator(supported_data_types=["text"])

def __init__(
self,
Expand All @@ -50,7 +50,7 @@ def __init__(
n (int): The n-gram size for n-gram similarity. Defaults to 5.
validator (Optional[ScorerPromptValidator]): Custom validator for the scorer. Defaults to None.
"""
super().__init__(validator=validator or self._default_validator)
super().__init__(validator=validator or self._DEFAULT_VALIDATOR)

self.reference_text = reference_text
self.metric = metric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SelfAskGeneralFloatScaleScorer(FloatScaleScorer):
system prompt and prompt format. The final score is normalized to [0, 1].
"""

_default_validator: ScorerPromptValidator = ScorerPromptValidator(
_DEFAULT_VALIDATOR: ScorerPromptValidator = ScorerPromptValidator(
supported_data_types=["text"],
is_objective_required=True,
)
Expand Down Expand Up @@ -69,7 +69,7 @@ def __init__(
ValueError: If system_prompt_format_string is not provided or empty.
ValueError: If min_value is greater than max_value.
"""
super().__init__(validator=validator or self._default_validator)
super().__init__(validator=validator or self._DEFAULT_VALIDATOR)
self._prompt_target = chat_target
if not system_prompt_format_string:
raise ValueError("system_prompt_format_string must be provided and non-empty.")
Expand Down
4 changes: 2 additions & 2 deletions pyrit/score/float_scale/self_ask_likert_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class SelfAskLikertScorer(FloatScaleScorer):
A class that represents a "self-ask" score for text scoring for a likert scale.
"""

_default_validator: ScorerPromptValidator = ScorerPromptValidator(supported_data_types=["text"])
_DEFAULT_VALIDATOR: ScorerPromptValidator = ScorerPromptValidator(supported_data_types=["text"])

def __init__(
self,
Expand All @@ -170,7 +170,7 @@ def __init__(
likert_scale (LikertScalePaths): The Likert scale configuration to use for scoring.
validator (Optional[ScorerPromptValidator]): Custom validator for the scorer. Defaults to None.
"""
super().__init__(validator=validator or self._default_validator)
super().__init__(validator=validator or self._DEFAULT_VALIDATOR)

self._prompt_target = chat_target
self._likert_scale = likert_scale
Expand Down
4 changes: 2 additions & 2 deletions pyrit/score/float_scale/self_ask_scale_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SystemPaths(enum.Enum):
RED_TEAMER_SYSTEM_PROMPT = Path(SCORER_SCALES_PATH, "red_teamer_system_prompt.yaml").resolve()
CRITERIA_SYSTEM_PROMPT = Path(SCORER_SCALES_PATH, "criteria_system_prompt.yaml").resolve()

_default_validator: ScorerPromptValidator = ScorerPromptValidator(
_DEFAULT_VALIDATOR: ScorerPromptValidator = ScorerPromptValidator(
supported_data_types=["text"],
is_objective_required=True,
)
Expand All @@ -59,7 +59,7 @@ def __init__(
Defaults to GENERAL_SYSTEM_PROMPT if not provided.
validator (Optional[ScorerPromptValidator]): Custom validator for the scorer. Defaults to None.
"""
super().__init__(validator=validator or self._default_validator)
super().__init__(validator=validator or self._DEFAULT_VALIDATOR)

self._prompt_target = chat_target

Expand Down
4 changes: 2 additions & 2 deletions pyrit/score/float_scale/video_float_scale_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class VideoFloatScaleScorer(
the audio is extracted, transcribed, and scored. The audio scores are included in the aggregation.
"""

_default_validator: ScorerPromptValidator = ScorerPromptValidator(supported_data_types=["video_path"])
_DEFAULT_VALIDATOR: ScorerPromptValidator = ScorerPromptValidator(supported_data_types=["video_path"])

def __init__(
self,
Expand Down Expand Up @@ -78,7 +78,7 @@ def __init__(
Raises:
ValueError: If audio_scorer is provided and does not support audio_path data type.
"""
FloatScaleScorer.__init__(self, validator=validator or self._default_validator)
FloatScaleScorer.__init__(self, validator=validator or self._DEFAULT_VALIDATOR)

_BaseVideoScorer.__init__(
self,
Expand Down
Loading