Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing private parameter from __call__ of AdversarialSimulator #37709

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
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 @@ -92,7 +94,7 @@ def _ensure_service_dependencies(self):
blame=ErrorBlame.USER_ERROR,
)

# @monitor_adversarial_scenario
# pylint: disable=too-many-locals
async def __call__(
self,
*,
Expand All @@ -106,10 +108,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 +218,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
@@ -1,54 +1,25 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: disable=C0301,C0114,R0913,R0903
# noqa: E501
import functools
import logging
from random import randint
from typing import Callable, Optional

from promptflow._sdk._telemetry import ActivityType, monitor_operation

from azure.ai.evaluation._exceptions import ErrorBlame, ErrorCategory, ErrorTarget, EvaluationException
from azure.ai.evaluation._model_configurations import AzureAIProject
from azure.ai.evaluation.simulator import AdversarialScenario
from azure.identity import DefaultAzureCredential

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 +81,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
@@ -1,53 +1,23 @@
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
# pylint: disable=C0301,C0114,R0913,R0903
# noqa: E501
import functools
import logging
from typing import Callable

from promptflow._sdk._telemetry import ActivityType, monitor_operation

from azure.ai.evaluation._exceptions import ErrorBlame, ErrorCategory, ErrorTarget, EvaluationException
from azure.ai.evaluation._model_configurations import AzureAIProject
from azure.ai.evaluation.simulator import AdversarialScenario
from azure.identity import DefaultAzureCredential

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 +77,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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# flake8: noqa
# pylint: disable=W0102,W0613,R0914,C0301,E0401,E0611
# pylint: disable=W0102,W0613,R0914,C0301,E0401,E0611,C0114,R0913,E0702,R0903
# ---------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# ---------------------------------------------------------
Expand Down
Loading