Skip to content

Commit 1db8f14

Browse files
authored
Merge pull request #5143 from lgayhardt/evalairedteaming0525
AI red teaming updates + cloud
2 parents e334bb9 + b7e06a6 commit 1db8f14

File tree

4 files changed

+306
-57
lines changed

4 files changed

+306
-57
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
title: Run AI Red Teaming Agent in the cloud (Azure AI Foundry SDK)
3+
titleSuffix: Azure AI Foundry
4+
description: This article provides instructions on how to use the AI Red Teaming Agent to run an automated scan in the cloud of a Generative AI application with the Azure AI Foundry SDK.
5+
manager: scottpolly
6+
ms.service: azure-ai-foundry
7+
ms.custom:
8+
- references_regions
9+
ms.topic: how-to
10+
ms.date: 06/03/2025
11+
ms.reviewer: minthigpen
12+
ms.author: lagayhar
13+
author: lgayhardt
14+
---
15+
16+
# Run AI Red Teaming Agent in the cloud (preview)
17+
18+
[!INCLUDE [feature-preview](../../includes/feature-preview.md)]
19+
20+
Though the AI Red Teaming Agent (preview) can be run [locally](run-scans-ai-red-teaming-agent.md) during prototyping and development to help identify safety risks, running them in the cloud allows for pre-deployment AI red teaming runs on larger combinations of attack strategies and risk categories for a fuller analysis.
21+
22+
## Prerequisites
23+
24+
[!INCLUDE [uses-fdp-only](../../includes/uses-fdp-only.md)]
25+
26+
If this is your first time running evaluations or AI red teaming runs on your Azure AI Foundry project, you might need to do a few additional setup steps.
27+
28+
1. [Create and connect your storage account](https://github.com/azure-ai-foundry/foundry-samples/blob/main/samples/microsoft/infrastructure-setup/01-connections/connection-storage-account.bicep) to your Azure AI Foundry project at the resource level. This bicep template provisions and connects a storage account to your Foundry project with key authentication.
29+
2. Make sure the connected storage account has access to all projects.
30+
3. If you connected your storage account with Microsoft Entra ID, make sure to give MSI (Microsoft Identity) permissions for Storage Blob Data Owner to both your account and Foundry project resource in Azure portal.
31+
32+
## Getting started
33+
34+
First, install Azure AI Foundry SDK's project client which runs the AI Red Teaming Agent in the cloud
35+
36+
```python
37+
uv install azure-ai-projects azure-identity
38+
```
39+
40+
> [!NOTE]
41+
> For more detailed information, see the [REST API Reference Documentation](/rest/api/aifoundry/aiprojects/red-teams).
42+
43+
Then, set your environment variables for your Azure AI Foundry resources
44+
45+
```python
46+
import os
47+
48+
endpoint = os.environ["PROJECT_ENDPOINT"] # Sample : https://<account_name>.services.ai.azure.com/api/projects/<project_name>
49+
model_endpoint = os.environ["MODEL_ENDPOINT"] # Sample : https://<account_name>.services.ai.azure.com
50+
model_api_key= os.environ["MODEL_API_KEY"]
51+
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] # Sample : gpt-4o-mini
52+
```
53+
54+
## Supported targets
55+
56+
Running the AI Red Teaming Agent in the cloud currently only supports Azure OpenAI model deployments in your Azure AI Foundry project as a target.
57+
58+
## Create an AI red teaming run
59+
60+
# [Python](#tab/python)
61+
62+
```python
63+
from azure.identity import DefaultAzureCredential
64+
from azure.ai.projects import AIProjectClient
65+
from azure.ai.projects.models import (
66+
RedTeam,
67+
AzureOpenAIModelConfiguration,
68+
AttackStrategy,
69+
RiskCategory,
70+
)
71+
72+
with AIProjectClient(
73+
endpoint=endpoint,
74+
credential=DefaultAzureCredential(exclude_interactive_browser_credential=False),
75+
) as project_client:
76+
77+
# Create target configuration for testing an Azure OpenAI model
78+
target_config = AzureOpenAIModelConfiguration(model_deployment_name=model_deployment_name)
79+
80+
# Instantiate the AI Red Teaming Agent
81+
red_team_agent = RedTeam(
82+
attack_strategies=[AttackStrategy.BASE64],
83+
risk_categories=[RiskCategory.VIOLENCE],
84+
display_name="red-team-cloud-run",
85+
target=target_config,
86+
)
87+
88+
# Create and run the red teaming scan
89+
red_team_response = project_client.red_teams.create(red_team=red_team_agent, headers={"model-endpoint": model_endpoint, "api-key": model_api_key,})
90+
```
91+
92+
# [cURL](#tab/curl)
93+
94+
```bash
95+
curl --request POST \ --url https://{{account}}.services.ai.azure.com/api/projects/{{project}}/redteams/runs:run \ --header 'content-type: application/json' \ --header 'authorization: Bearer {{ai_token}}' --data '{ "scanName": "sample_scan_magic_1", "riskCategories": [ "Violence" ], "attackStrategy": [ "Flip" ], "numTurns": 1, "target": { "type": "AzureOpenAIModel", "modelDeploymentName": "{{connectionName}}/{{deploymentName}}" }}'
96+
```
97+
98+
- Replace `{{account}}`, `{{project}}` with Foundry Project account name and project name.
99+
- Replace `{{ai_token}}` with Bearer token with audience "<https://ai.azure.com>"
100+
- Replace `"{{connectionName}}"` with the Azure OpenAI model connection name connected to the Foundry project account.
101+
- Replace `"{{deploymentName}}"` with the Azure OpenAI deployment name of the AOAI connection account.
102+
103+
---
104+
105+
## Get an AI red teaming run
106+
107+
# [Python](#tab/python)
108+
109+
```python
110+
# Use the name returned by the create operation for the get call
111+
get_red_team_response = project_client.red_teams.get(name=red_team_response.name)
112+
print(f"Red Team scan status: {get_red_team_response.status}")
113+
```
114+
115+
# [cURL](#tab/curl)
116+
117+
```bash
118+
curl --request GET \ --header 'authorization: Bearer {{ai_token}}' --url https://{{account}}.services.ai.azure.com/api/projects/{{project}}/redteams/runs/{{scan_id}}
119+
```
120+
121+
- Replace `"{{scan_id}"` with the ID returned by the POST API.
122+
123+
---
124+
125+
## List all AI red teaming runs
126+
127+
# [Python](#tab/python)
128+
129+
```python
130+
for scan in project_client.red_teams.list():
131+
print(f"Found scan: {scan.name}, Status: {scan.status}")
132+
```
133+
134+
# [cURL](#tab/curl)
135+
136+
```bash
137+
curl --request GET \ --header 'authorization: Bearer {{ai_token}}' --url https://{{account}}.services.ai.azure.com/api/projects/{{project}}/redteams/runs
138+
```
139+
140+
---
141+
142+
Once your AI red teaming run is finished running, you can [view your results](../view-ai-red-teaming-results.md) in your Azure AI Foundry project.
143+
144+
## Related content
145+
146+
Try out an [example workflow](https://aka.ms/airedteamingagent-sample) in our GitHub samples.

0 commit comments

Comments
 (0)