Skip to content
Merged
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: 3 additions & 3 deletions pyrit/models/seeds/attack_technique_seed_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Literal
from typing import TYPE_CHECKING, Literal, Self

from pydantic import Field

Expand Down Expand Up @@ -48,7 +48,7 @@ class AttackTechniqueSeedGroup(SeedGroup):
)

@classmethod
def from_system_prompt(cls, system_prompt: str, *, insertion_index: int | None = None) -> AttackTechniqueSeedGroup:
def from_system_prompt(cls, system_prompt: str, *, insertion_index: int | None = None) -> Self:
"""
Build a technique group carrying a single system-role instruction.

Expand Down Expand Up @@ -83,7 +83,7 @@ def from_messages(
starting_sequence: int = 0,
insertion_index: int | None = None,
prompt_placement: Literal["preserve", "prepend"] = "prepend",
) -> AttackTechniqueSeedGroup:
) -> Self:
"""
Build a technique group from conversation messages.

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/models/test_attack_technique_seed_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

"""Tests for is_general_technique property and AttackTechniqueSeedGroup class."""

from typing import assert_type

import pytest

from pyrit.models import Message
Expand Down Expand Up @@ -216,9 +218,21 @@ def test_insertion_index_zero(self):
assert group.insertion_index == 0


class _CustomAttackTechniqueSeedGroup(AttackTechniqueSeedGroup):
"""A subclass used to check classmethod factory return types."""


class TestAttackTechniqueSeedGroupFromSystemPrompt:
"""Tests for the from_system_prompt classmethod."""

def test_returns_calling_subclass(self):
"""Test that from_system_prompt preserves the calling subclass."""
group = _CustomAttackTechniqueSeedGroup.from_system_prompt("Follow these rules.")

assert_type(group, _CustomAttackTechniqueSeedGroup)
assert type(group) is _CustomAttackTechniqueSeedGroup
assert group.seeds[0].value == "Follow these rules."

def test_builds_single_system_general_technique_seed(self):
"""Test that from_system_prompt yields one system-role general-technique seed."""
group = AttackTechniqueSeedGroup.from_system_prompt("Follow these rules.")
Expand Down Expand Up @@ -256,6 +270,16 @@ def test_prompt_placement_survives_serialization_round_trip(self):
class TestAttackTechniqueSeedGroupFromMessages:
"""Tests for the from_messages classmethod."""

def test_returns_calling_subclass(self):
"""Test that from_messages preserves the calling subclass."""
group = _CustomAttackTechniqueSeedGroup.from_messages(
messages=[Message.from_prompt(prompt="encoded practice", role="user")],
)

assert_type(group, _CustomAttackTechniqueSeedGroup)
assert type(group) is _CustomAttackTechniqueSeedGroup
assert group.seeds[0].value == "encoded practice"

def test_builds_general_technique_seeds_from_messages(self):
"""Test that from_messages preserves roles and marks prompts as a technique."""
messages = [
Expand Down
Loading