Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
TokenScope,
)
from ._utils import JsonLineList
from ._helpers import experimental

logger = logging.getLogger(__name__)


@experimental
class AdversarialSimulator:
"""
Initializes the adversarial simulator with a project scope.
Expand Down Expand Up @@ -106,10 +107,10 @@ async def __call__(
api_call_retry_sleep_sec: int = 1,
api_call_delay_sec: int = 0,
concurrent_async_task: int = 3,
_jailbreak_type: Optional[str] = None,
language: SupportedLanguages = SupportedLanguages.English,
randomize_order: bool = True,
randomization_seed: Optional[int] = None,
**kwargs,
):
"""
Executes the adversarial simulation against a specified target function asynchronously.
Expand Down Expand Up @@ -216,6 +217,7 @@ async def __call__(
total_tasks,
)
total_tasks = min(total_tasks, max_simulation_results)
_jailbreak_type = kwargs.get("_jailbreak_type", None)
if _jailbreak_type:
jailbreak_dataset = await self.rai_client.get_jailbreaks_dataset(type=_jailbreak_type)
progress_bar = tqdm(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,11 @@

from ._adversarial_simulator import AdversarialSimulator
from ._model_tools import AdversarialTemplateHandler, ManagedIdentityAPITokenManager, RAIClient, TokenScope

from ._helpers import experimental
logger = logging.getLogger(__name__)


def monitor_adversarial_scenario(func) -> Callable:
"""Decorator to monitor adversarial scenario.

:param func: The function to be decorated.
:type func: Callable
:return: The decorated function.
:rtype: Callable
"""

@functools.wraps(func)
def wrapper(*args, **kwargs):
scenario = str(kwargs.get("scenario", None))
max_conversation_turns = kwargs.get("max_conversation_turns", None)
max_simulation_results = kwargs.get("max_simulation_results", None)
decorated_func = monitor_operation(
activity_name="jailbreak.adversarial.simulator.call",
activity_type=ActivityType.PUBLICAPI,
custom_dimensions={
"scenario": scenario,
"max_conversation_turns": max_conversation_turns,
"max_simulation_results": max_simulation_results,
},
)(func)

return decorated_func(*args, **kwargs)

return wrapper


@experimental
class DirectAttackSimulator:
"""
Initialize a UPIA (user prompt injected attack) jailbreak adversarial simulator with a project scope.
Expand Down Expand Up @@ -110,7 +82,6 @@ def _ensure_service_dependencies(self):
blame=ErrorBlame.USER_ERROR,
)

# @monitor_adversarial_scenario
async def __call__(
self,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,12 @@

from ._adversarial_simulator import AdversarialSimulator
from ._model_tools import AdversarialTemplateHandler, ManagedIdentityAPITokenManager, RAIClient, TokenScope
from ._helpers import experimental

logger = logging.getLogger(__name__)


def monitor_adversarial_scenario(func) -> Callable:
"""Decorator to monitor adversarial scenario.

:param func: The function to be decorated.
:type func: Callable
:return: The decorated function.
:rtype: Callable
"""

@functools.wraps(func)
def wrapper(*args, **kwargs):
scenario = str(kwargs.get("scenario", None))
max_conversation_turns = kwargs.get("max_conversation_turns", None)
max_simulation_results = kwargs.get("max_simulation_results", None)
decorated_func = monitor_operation(
activity_name="xpia.adversarial.simulator.call",
activity_type=ActivityType.PUBLICAPI,
custom_dimensions={
"scenario": scenario,
"max_conversation_turns": max_conversation_turns,
"max_simulation_results": max_simulation_results,
},
)(func)

return decorated_func(*args, **kwargs)

return wrapper


@experimental
class IndirectAttackSimulator:
"""
Initializes the XPIA (cross domain prompt injected attack) jailbreak adversarial simulator with a project scope.
Expand Down Expand Up @@ -107,7 +80,6 @@ def _ensure_service_dependencies(self):
blame=ErrorBlame.USER_ERROR,
)

# @monitor_adversarial_scenario
async def __call__(
self,
*,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ model:
configuration:
type: azure_openai
azure_deployment: ${env:AZURE_DEPLOYMENT}
api_key: ${env:AZURE_OPENAI_API_KEY}
azure_endpoint: ${env:AZURE_OPENAI_ENDPOINT}
parameters:
temperature: 0.0
Expand Down