Skip to content

Commit 75d0645

Browse files
committed
Update documentation
1 parent 2cbf19e commit 75d0645

File tree

5 files changed

+27
-45
lines changed

5 files changed

+27
-45
lines changed

docs/images/redteam_dashboard.png

34.3 KB
Loading

docs/images/redteam_logs.png

80.4 KB
Loading

docs/safety_evaluation.md

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Evaluating RAG answer safety
22

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 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).
44

55
* [Deploy an Azure AI project](#deploy-an-azure-ai-project)
66
* [Setup the evaluation environment](#setup-the-evaluation-environment)
7-
* [Simulate and evaluate adversarial users](#simulate-and-evaluate-adversarial-users)
7+
* [Run safety scan](#simulate-and-evaluate-adversarial-users)
88
* [Review the safety evaluation results](#review-the-safety-evaluation-results)
99

1010
## Deploy an Azure AI project
1111

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.
1313

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:
1515

1616
```shell
1717
azd env set USE_AI_PROJECT true
@@ -57,51 +57,33 @@ In order to use the adversarial simulator and safety evaluators, you need an Azu
5757
Run the following command to simulate adversarial queries and evaluate the safety of the answers generated in response to those queries:
5858

5959
```shell
60-
python evals/safety_evaluation.py --target_url <TARGET_URL> --max_simulations <MAX_RESULTS>
60+
python evals/safety_evaluation.py --target_url <TARGET_URL> --questions_per_category <NUM_QUESTIONS>
6161
```
6262

6363
* `--target_url`: The target URL for the 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 app in 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-
```
64+
* `--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 for test 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 question in different ways.
65+
66+
## Review the red team results
67+
68+
That command will save results both in Azure AI Foundry and in the local `evals/redteams` directory.
69+
70+
### Review results in Azure AI Foundry
71+
72+
Navigate from the URL in the terminal output to the results in Azure AI Foundry.
73+
First you'll see overall attack metrics:
74+
75+
![Red Teaming Agent Results](images/redteam_dashboard.png)
76+
77+
Then you can dig into the data and see the questions and answers received from your app:
78+
79+
![Red Teaming Agent Results](images/redteam_logs.png)
9980
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.
81+
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.
10182

10283
## Resources
10384

104-
To learn more about the Azure AI services used in this project, look through the script and reference the following documentation:
85+
To learn more about the Azure AI services used in this project, look through the script, documentation, and videos below:
10586

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)
87+
* [safety_evaluation.py](evals/safety_evaluation.py)
88+
* [Run automated safety scans with AI Red Teaming Agent](https://learn.microsoft.com/azure/ai-foundry/how-to/develop/run-scans-ai-red-teaming-agent)
89+
* [Build 2025: Red-teaming Demo](https://www.youtube.com/watch?v=sZzcSX7BFVA)

evals/redteams/.gitkeep

Whitespace-only changes.

evals/safety_evaluation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async def run_redteaming(target_url: str, questions_per_category: int = 1, scan_
9292
parser.add_argument(
9393
"--questions_per_category",
9494
type=int,
95-
default=1,
95+
default=5,
9696
help="Number of questions per risk category to ask during the scan.",
9797
)
9898
parser.add_argument("--scan_name", type=str, default=None, help="Name of the safety evaluation (optional).")

0 commit comments

Comments
 (0)