Skip to content
Open
2 changes: 2 additions & 0 deletions pyrit/memory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pyrit.memory.azure_sql_memory import AzureSQLMemory
from pyrit.memory.central_memory import CentralMemory
from pyrit.memory.identifier_filters import IdentifierFilter
from pyrit.memory.memory_embedding import MemoryEmbedding
from pyrit.memory.memory_exporter import MemoryExporter
from pyrit.memory.memory_interface import MemoryInterface
Expand All @@ -26,4 +27,5 @@
"MemoryExporter",
"PromptMemoryEntry",
"SeedEntry",
"IdentifierFilter",
]
315 changes: 168 additions & 147 deletions pyrit/memory/azure_sql_memory.py

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions pyrit/memory/identifier_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

from dataclasses import dataclass
from enum import Enum


class IdentifierType(Enum):
"""Enumeration of supported identifier types for filtering."""

ATTACK = "attack"
TARGET = "target"
SCORER = "scorer"
CONVERTER = "converter"


@dataclass(frozen=True)
class IdentifierFilter:
"""
Immutable filter definition for matching JSON-backed identifier properties.

Attributes:
identifier_type: The type of identifier column to filter on.
property_path: The JSON path for the property to match.
sub_path: An optional JSON path that indicates the property at property_path is an array
and the condition should resolve if any element in that array matches the value.
Cannot be used with partial_match.
value_to_match: The string value that must match the extracted JSON property value.
partial_match: Whether to perform a substring match. Cannot be used with sub_path.
case_sensitive: Whether the match should be case-sensitive. Defaults to False.
"""

identifier_type: IdentifierType
property_path: str
sub_path: str | None
value_to_match: str
partial_match: bool = False
case_sensitive: bool = False

def __post_init__(self) -> None:
"""
Validate the filter configuration.

Raises:
ValueError: If the filter configuration is not valid.
"""
if self.sub_path and (self.partial_match or self.case_sensitive):
raise ValueError("Cannot use sub_path with partial_match or case_sensitive")
Loading
Loading