You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When deploying a RAG app to production, you should evaluate the safety of the answers generated by the RAG flow. This is important to ensure that the answers are appropriate and do not contain any harmful or sensitive content. This project includes scripts that use Azure AI services to simulate an adversarial user and evaluate the safety of the answers generated in response to those adversarial queries.
3
+
When deploying a RAG app to production, you should evaluate the safety of the answers generated by the RAG flow. This is important to ensure that the answers are appropriate and do not contain any harmful or sensitive content. This project includes scripts that use the [azure-ai-evaluation SDK](https://pypi.org/project/azure-ai-evaluation/#history)to perform an [automated safety scan with an AI Red Teaming agent](https://learn.microsoft.com/azure/ai-foundry/how-to/develop/run-scans-ai-red-teaming-agent).
4
4
5
5
*[Deploy an Azure AI project](#deploy-an-azure-ai-project)
6
6
*[Setup the evaluation environment](#setup-the-evaluation-environment)
7
-
*[Simulate and evaluate adversarial users](#simulate-and-evaluate-adversarial-users)
8
-
*[Review the safety evaluation results](#review-the-safety-evaluation-results)
7
+
*[Run red teaming agent](#run-red-teaming-agent)
8
+
*[Review the red teaming results](#review-the-red-teaming-results)
9
9
10
10
## Deploy an Azure AI project
11
11
12
-
In order to use the adversarial simulator and safety evaluators, you need an Azure AI project inside an Azure AI Hub.
12
+
In order to use the Red Teaming agent, you need an Azure AI project inside Azure AI Foundry.
13
13
14
-
1. Run this command to tell `azd` to provision an Azure AI project and hub:
14
+
1. Run this command to tell `azd` to provision an Azure AI project:
15
15
16
16
```shell
17
17
azd env set USE_AI_PROJECT true
@@ -45,63 +45,62 @@ In order to use the adversarial simulator and safety evaluators, you need an Azu
45
45
.evalenv\Scripts\activate
46
46
```
47
47
48
-
1. Install the dependencies for the safety evaluation script:
48
+
3. Install the dependencies for the safety evaluation script:
49
49
50
50
```bash
51
51
pip install uv
52
52
uv pip install -r evals/requirements.txt
53
53
```
54
54
55
-
## Simulate and evaluate adversarial users
55
+
## Run the red teaming agent
56
56
57
-
Run the following command to simulate adversarial queries and evaluate the safety of the answers generated in response to those queries:
57
+
To run the red teaming agent, you need to have the RAG app running at a deployed URL or local URL. Consult the main README for deployment instructions local server instructions.
58
+
59
+
In the same terminal where you activated `.evalenv`, run this command to perform a red teaming scan:
*`--target_url`: The target URL forthe callback. Default is `http://127.0.0.1:8000/chat`, so make sure that the RAG app is running locally. If you are running the RAG appin a different environment, set this to the correct URL.
64
-
*`--max_simulations`: The maximum number of simulated user queries. Default is `200`. The higher the number, the longer the evaluation will take. The default of `200` simulations will take about 25 minutes to run, which includes both the time to generate the simulated data and the time to evaluate it.
65
-
66
-
## Review the safety evaluation results
67
-
68
-
That command will generate a report in`safety_results.json`.
69
-
Results will look like this:
70
-
71
-
```json
72
-
{
73
-
"hate_unfairness": {
74
-
"low_count": 20,
75
-
"score_total": 8,
76
-
"mean_score": 0.4,
77
-
"low_rate": 1.0
78
-
},
79
-
"sexual": {
80
-
"low_count": 20,
81
-
"score_total": 9,
82
-
"mean_score": 0.45,
83
-
"low_rate": 1.0
84
-
},
85
-
"violence": {
86
-
"low_count": 20,
87
-
"score_total": 9,
88
-
"mean_score": 0.45,
89
-
"low_rate": 1.0
90
-
},
91
-
"self_harm": {
92
-
"low_count": 20,
93
-
"score_total": 10,
94
-
"mean_score": 0.5,
95
-
"low_rate": 1.0
96
-
}
97
-
}
98
-
```
65
+
*`--target_url`: The target URL for the callback. Default is `http://127.0.0.1:8000/chat`, which assumes the RAG app is running locally. If you want to scan a deployed app instead, set this to `https://DEPLOYEDURL/chat`.
66
+
*`--questions_per_category`: The number of questions per risk category to ask during the scan. There are four risk categories (`hate_unfairness`, `sexual`, `violence`, and `self_harm`). The default is 5 questions per category, but you can decrease this number to 1 fortest runs, or increase it for a more thorough evaluation. Note that the total questions asked will be higher, as the red teaming agent is configured to try [multiple attack strategies](https://learn.microsoft.com/azure/ai-foundry/how-to/develop/run-scans-ai-red-teaming-agent#supported-attack-strategies), where it transforms the original questionin different ways.
67
+
68
+
## Review the red teaming results
69
+
70
+
That command will save results both in Azure AI Foundry and in the local`evals/redteams` directory.
71
+
72
+
### Review results in Azure AI Foundry
73
+
74
+
Navigate from the URL in the terminal output to the results in Azure AI Foundry.
Note that the questions are intentionally adversarial, so you should mentally prepare for that before looking at the results. The goal is to see if your app can handle these adversarial queries and provide safe answers. Even if your scan results in a 0% attack success rate, you should still review the questions and answers to ensure that you're happy with the way your app responds to these adversarial queries.
84
+
85
+
Learn more in the [Red Teaming Agent documentation](https://learn.microsoft.com/azure/ai-foundry/how-to/develop/run-scans-ai-red-teaming-agent#viewing-your-results-in-azure-ai-foundry-project).
86
+
87
+
## Review results locally
88
+
89
+
In addition to the results in Azure AI Foundry, you can also review the results locally in the `evals/redteams` directory. The results are saved in JSON format.
90
+
91
+
Each file in this directory corresponds to a single red teaming scan, and contains the following fields:
92
+
93
+
*`scorecard`: A summary of the scan results, including the attack success rate and the number of questions asked.
94
+
*`parameters`: The parameters used for the scan, including the risk categories and attack strategies.
95
+
*`attack_details`: A list of the questions asked during the scan, along with the answers received from your app.
96
+
*`studio_url`: A link to the Azure AI Foundry studio where you can view the results in more detail.
99
97
100
-
The ideal score is `low_rate` of 1.0 and `mean_score` of 0.0. The `low_rate` indicates the fraction of answers that were reported as "Low" or "Very low" by an evaluator. The `mean_score` is the average score of all the answers, where 0 is a very safe answer and 7 is a very unsafe answer.
98
+
Learn more inthe [Red Teaming Agent documentation](https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/develop/run-scans-ai-red-teaming-agent#results-from-your-automated-scans).
101
99
102
100
## Resources
103
101
104
-
To learn more about the Azure AI services used in this project, look through the scriptand reference the following documentation:
102
+
To learn more about the red team scanning, look through the script, documentation, and videos below:
105
103
106
-
* [Generate simulated data for evaluation](https://learn.microsoft.com/azure/ai-studio/how-to/develop/simulator-interaction-data)
107
-
* [Evaluate with the Azure AI Evaluation SDK](https://learn.microsoft.com/azure/ai-studio/how-to/develop/evaluate-sdk)
0 commit comments