diff --git a/sdk/ai/azure-ai-projects/azure/ai/projects/aio/operations/_operations.py b/sdk/ai/azure-ai-projects/azure/ai/projects/aio/operations/_operations.py index 45f5d3d15a03..abf4b81fa11b 100644 --- a/sdk/ai/azure-ai-projects/azure/ai/projects/aio/operations/_operations.py +++ b/sdk/ai/azure-ai-projects/azure/ai/projects/aio/operations/_operations.py @@ -2271,7 +2271,7 @@ async def create(self, red_team: Union[_models.RedTeam, JSON, IO[bytes]], **kwar if isinstance(red_team, (IOBase, bytes)): _content = red_team else: - _content = json.dumps(red_team, cls=SdkJSONEncoder, exclude_readonly=True) # type: ignore + _content = json.dumps(red_team, cls=SdkJSONEncoder, exclude_readonly=False) # type: ignore _request = build_red_teams_create_request( content_type=content_type, diff --git a/sdk/ai/azure-ai-projects/azure/ai/projects/operations/_operations.py b/sdk/ai/azure-ai-projects/azure/ai/projects/operations/_operations.py index 5bb2c2ef9a09..83cab04608b1 100644 --- a/sdk/ai/azure-ai-projects/azure/ai/projects/operations/_operations.py +++ b/sdk/ai/azure-ai-projects/azure/ai/projects/operations/_operations.py @@ -610,7 +610,7 @@ def build_red_teams_create_request(**kwargs: Any) -> HttpRequest: accept = _headers.pop("Accept", "application/json") # Construct URL - _url = "/redTeams/runs:run" + _url = "/redteams/runs:run" # Construct parameters _params["api-version"] = _SERIALIZER.query("api_version", api_version, "str") @@ -2823,7 +2823,7 @@ def create(self, red_team: Union[_models.RedTeam, JSON, IO[bytes]], **kwargs: An if isinstance(red_team, (IOBase, bytes)): _content = red_team else: - _content = json.dumps(red_team, cls=SdkJSONEncoder, exclude_readonly=True) # type: ignore + _content = json.dumps(red_team, cls=SdkJSONEncoder, exclude_readonly=False) # type: ignore _request = build_red_teams_create_request( content_type=content_type, diff --git a/sdk/ai/azure-ai-projects/samples/red_team/sample_red_team.py b/sdk/ai/azure-ai-projects/samples/red_team/sample_red_team.py new file mode 100644 index 000000000000..a7d7e1575329 --- /dev/null +++ b/sdk/ai/azure-ai-projects/samples/red_team/sample_red_team.py @@ -0,0 +1,82 @@ +# pylint: disable=line-too-long,useless-suppression +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +DESCRIPTION: + Given an AIProjectClient, this sample demonstrates how to use the synchronous + methods to create, get, list, and run Red Team scans. + +USAGE: + python sample_red_team.py + + Before running the sample: + + pip install azure-ai-projects azure-identity + + Set these environment variables with your own values: + 1) PROJECT_ENDPOINT - Required. The Azure AI Project endpoint, as found in the overview page of your + Azure AI Foundry project. + 2) MODEL_DEPLOYMENT_NAME - Required. Your model deployment name. + 3) MODEL_ENDPOINT - Required. The Azure AI Model endpoint, as found in the overview page of your + Azure AI Foundry project. + 4) MODEL_API_KEY - Required. The API key for your Azure AI Model. +""" +import os + +from azure.identity import DefaultAzureCredential +from azure.ai.projects import AIProjectClient +from azure.ai.projects.models import ( + RedTeam, + AzureOpenAIModelConfiguration, + AttackStrategy, + RiskCategory, +) + + +def sample_red_team() -> None: + """Demonstrates how to perform Red Team operations using the AIProjectClient.""" + + endpoint = os.environ["PROJECT_ENDPOINT"] # Sample : https://.services.ai.azure.com/api/projects/ + model_endpoint = os.environ["MODEL_ENDPOINT"] # Sample : https://.services.ai.azure.com + model_api_key= os.environ["MODEL_API_KEY"] + model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] # Sample : gpt-4o-mini + + with AIProjectClient( + endpoint=endpoint, + credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), + ) as project_client: + + # [START red_team_sample] + print("Creating a Red Team scan for direct model testing") + + # Create target configuration for testing an Azure OpenAI model + target_config = AzureOpenAIModelConfiguration(model_deployment_name=model_deployment_name) + + # Create the Red Team configuration + red_team = RedTeam( + attack_strategies=[AttackStrategy.BASE64], + risk_categories=[RiskCategory.VIOLENCE], + display_name="redteamtest1", # Use a simpler name + target=target_config, + ) + + # Create and run the Red Team scan + red_team_response = project_client.red_teams.create(red_team=red_team, headers={"model-endpoint": model_endpoint, "api-key": model_api_key,}) + print(f"Red Team scan created with scan name: {red_team_response.name}") + + print("Getting Red Team scan details") + # Use the name returned by the create operation for the get call + get_red_team_response = project_client.red_teams.get(name=red_team_response.name) + print(f"Red Team scan status: {get_red_team_response.status}") + + print("Listing all Red Team scans") + for scan in project_client.red_teams.list(): + print(f"Found scan: {scan.name}, Status: {scan.status}") + # [END red_team_sample] + + +if __name__ == "__main__": + sample_red_team() \ No newline at end of file diff --git a/sdk/ai/azure-ai-projects/samples/red_team/sample_red_team_async.py b/sdk/ai/azure-ai-projects/samples/red_team/sample_red_team_async.py new file mode 100644 index 000000000000..f455218a0334 --- /dev/null +++ b/sdk/ai/azure-ai-projects/samples/red_team/sample_red_team_async.py @@ -0,0 +1,83 @@ +# pylint: disable=line-too-long,useless-suppression +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +DESCRIPTION: + Given an AIProjectClient, this sample demonstrates how to use the asynchronous + methods to create, get, list, and run Red Team scans. + +USAGE: + python sample_red_team_async.py + + Before running the sample: + + pip install azure-ai-projects azure-identity + + Set these environment variables with your own values: + 1) PROJECT_ENDPOINT - Required. The Azure AI Project endpoint, as found in the overview page of your + Azure AI Foundry project. + 2) MODEL_DEPLOYMENT_NAME - Required. Your model deployment name. + 3) MODEL_ENDPOINT - Required. The Azure AI Model endpoint, as found in the overview page of your + Azure AI Foundry project. + 4) MODEL_API_KEY - Required. The API key for your Azure AI Model. +""" +import os +import asyncio + +from azure.identity.aio import DefaultAzureCredential +from azure.ai.projects.aio import AIProjectClient +from azure.ai.projects.models import ( + RedTeam, + AzureOpenAIModelConfiguration, + AttackStrategy, + RiskCategory, +) + + +async def sample_red_team_async() -> None: + """Demonstrates how to perform Red Team operations using the AIProjectClient.""" + + endpoint = os.environ["PROJECT_ENDPOINT"] # Sample : https://.services.ai.azure.com/api/projects/ + model_endpoint = os.environ["MODEL_ENDPOINT"] # Sample : https://.services.ai.azure.com + model_api_key= os.environ["MODEL_API_KEY"] + model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] # Sample : gpt-4o-mini + async with DefaultAzureCredential() as credential: + async with AIProjectClient( + endpoint=endpoint, + credential=credential, + ) as project_client: + + # [START red_team_sample] + print("Creating a Red Team scan for direct model testing") + + # Create target configuration for testing an Azure OpenAI model + target_config = AzureOpenAIModelConfiguration(model_deployment_name=model_deployment_name) + + # Create the Red Team configuration + red_team = RedTeam( + attack_strategies=[AttackStrategy.BASE64], + risk_categories=[RiskCategory.VIOLENCE], + display_name="redteamtest1", # Use a simpler name + target=target_config, + ) + + # Create and run the Red Team scan + red_team_response = await project_client.red_teams.create(red_team=red_team, headers={"model-endpoint": model_endpoint, "api-key": model_api_key,}) + print(f"Red Team scan created with scan name: {red_team_response.name}") + + print("Getting Red Team scan details") + # Use the name returned by the create operation for the get call + get_red_team_response = await project_client.red_teams.get(name=red_team_response.name) + print(f"Red Team scan status: {get_red_team_response.status}") + + print("Listing all Red Team scans") + async for scan in project_client.red_teams.list(): + print(f"Found scan: {scan.name}, Status: {scan.status}") + # [END red_team_sample] + + +if __name__ == "__main__": + asyncio.run(sample_red_team_async()) \ No newline at end of file