|
1 | 1 | # Evaluating RAG answer safety
|
2 | 2 |
|
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). |
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) |
| 7 | +* [Run safety scan](#simulate-and-evaluate-adversarial-users) |
8 | 8 | * [Review the safety evaluation results](#review-the-safety-evaluation-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
|
@@ -57,51 +57,33 @@ In order to use the adversarial simulator and safety evaluators, you need an Azu
|
57 | 57 | Run the following command to simulate adversarial queries and evaluate the safety of the answers generated in response to those queries:
|
58 | 58 |
|
59 | 59 | ```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> |
61 | 61 | ```
|
62 | 62 |
|
63 | 63 | * `--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 | + |
| 76 | +
|
| 77 | +Then you can dig into the data and see the questions and answers received from your app: |
| 78 | +
|
| 79 | + |
99 | 80 |
|
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. |
101 | 82 |
|
102 | 83 | ## Resources
|
103 | 84 |
|
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: |
105 | 86 |
|
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) |
0 commit comments