|
17 | 17 | "### Prerequisite\n", |
18 | 18 | "First, if you have an Azure subscription, create an [Azure AI hub](https://learn.microsoft.com/en-us/azure/ai-studio/concepts/ai-resources) then [create an Azure AI project](https://learn.microsoft.com/en-us/azure/ai-studio/concepts/ai-resources). AI projects and Hubs can be served within a private network and are compatible with private endpoints. You **do not** need to provide your own LLM deployment as the AI Red Teaming Agent hosts adversarial models for both simulation and evaluation of harmful content and connects to it via your Azure AI project.\n", |
19 | 19 | "\n", |
20 | | - "**Note**: In order to upload your results to Azure AI Foundry, you must have the `Storage Blob Data Contributor` role\n", |
| 20 | + "In order to upload your results to Azure AI Foundry:\n", |
| 21 | + "- Your AI Foundry project must have a connection (*Connected Resources*) to a storage account with `Microsoft Entra ID` authentication enabled.\n", |
| 22 | + "- Your AI Foundry project must have the `Storage Blob Data Contributor` role in the storage account.\n", |
| 23 | + "- You must have the `Storage Blob Data Contributor` role in the storage account.\n", |
| 24 | + "- You must have network access to the storage account.\n", |
| 25 | + "\n", |
| 26 | + "For more information see: https://learn.microsoft.com/en-us/azure/ai-foundry/how-to/develop/run-scans-ai-red-teaming-agent\n", |
21 | 27 | "\n", |
22 | 28 | "**Important**: First, ensure that you've installed the [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) and then make sure to authenticate to Azure using `az login` in your terminal before running this notebook.\n", |
23 | 29 | "\n", |
|
38 | 44 | "\n", |
39 | 45 | "```bash\n", |
40 | 46 | "pip install uv\n", |
41 | | - "uv pip install azure-ai-evaluation[redteam] azure-identity openai\n", |
| 47 | + "uv pip install azure-ai-evaluation[redteam] azure-identity openai azure-ai-projects\n", |
42 | 48 | "```\n", |
43 | 49 | "\n", |
44 | 50 | "\n", |
|
65 | 71 | "import os\n", |
66 | 72 | "\n", |
67 | 73 | "# Azure imports\n", |
68 | | - "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n", |
69 | 74 | "from azure.ai.evaluation.red_team import RedTeam, RiskCategory, AttackStrategy\n", |
70 | 75 | "\n", |
71 | 76 | "# OpenAI imports\n", |
72 | | - "from openai import AzureOpenAI\n", |
| 77 | + "from openai import AzureOpenAI" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "### Login to Azure with valid credentials\n", |
| 85 | + "\n", |
| 86 | + "Ensure that you've installed the [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) and then make sure to authenticate to Azure using `az login` in your terminal before running this notebook.\n", |
| 87 | + "\n", |
| 88 | + "Configure the `credential` object with a different AzureCredential type if this is a requirement for your environment." |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": null, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "# Azure Credential imports\n", |
| 98 | + "from azure.identity import AzureCliCredential, get_bearer_token_provider\n", |
| 99 | + "\n", |
| 100 | + "!az login\n", |
73 | 101 | "\n", |
74 | 102 | "# Initialize Azure credentials\n", |
75 | | - "credential = DefaultAzureCredential()" |
| 103 | + "credential = AzureCliCredential()" |
76 | 104 | ] |
77 | 105 | }, |
78 | 106 | { |
|
83 | 111 | "\n", |
84 | 112 | "Set the following variables for use in this notebook. These variables connect to your Azure resources and model deployments.\n", |
85 | 113 | "\n", |
| 114 | + "Set these variables by creating an `.env` file in your project's root folder.\n", |
| 115 | + "\n", |
86 | 116 | "**Note:** You can find these values in your Azure AI Foundry project or Azure OpenAI resource." |
87 | 117 | ] |
88 | 118 | }, |
|
95 | 125 | "```\n", |
96 | 126 | "# Azure OpenAI\n", |
97 | 127 | "AZURE_OPENAI_API_KEY=\"your-api-key-here\"\n", |
98 | | - "AZURE_OPENAI_ENDPOINT=\"https://endpoint-name.openai.azure.com/openai/deployments/deployment-name/chat/completions\"\n", |
| 128 | + "AZURE_OPENAI_ENDPOINT=\"https://endpoint-name.cognitiveservices.azure.com/\"\n", |
99 | 129 | "AZURE_OPENAI_DEPLOYMENT_NAME=\"gpt-4\"\n", |
100 | 130 | "AZURE_OPENAI_API_VERSION=\"2024-12-01-preview\"\n", |
101 | 131 | "\n", |
102 | 132 | "# Azure AI Project\n", |
103 | | - "AZURE_SUBSCRIPTION_ID=\"12345678-1234-1234-1234-123456789012\"\n", |
104 | | - "AZURE_RESOURCE_GROUP_NAME=\"your-resource-group\"\n", |
105 | | - "AZURE_PROJECT_NAME=\"your-project-name\"\n", |
| 133 | + "AZURE_PROJECT_ENDPOINT=\"https://your-aifoundry-endpoint-name.services.ai.azure.com/api/projects/yourproject-name\"\n", |
106 | 134 | "```" |
107 | 135 | ] |
108 | 136 | }, |
|
113 | 141 | "outputs": [], |
114 | 142 | "source": [ |
115 | 143 | "# Azure AI Project information\n", |
116 | | - "azure_ai_project = {\n", |
117 | | - " \"subscription_id\": os.environ.get(\"AZURE_SUBSCRIPTION_ID\"),\n", |
118 | | - " \"resource_group_name\": os.environ.get(\"AZURE_RESOURCE_GROUP_NAME\"),\n", |
119 | | - " \"project_name\": os.environ.get(\"AZURE_PROJECT_NAME\"),\n", |
120 | | - "}\n", |
| 144 | + "azure_ai_project = os.environ.get(\"AZURE_PROJECT_ENDPOINT\")\n", |
121 | 145 | "\n", |
122 | 146 | "# Azure OpenAI deployment information\n", |
123 | 147 | "azure_openai_deployment = os.environ.get(\"AZURE_OPENAI_DEPLOYMENT\") # e.g., \"gpt-4\"\n", |
124 | | - "azure_openai_endpoint = os.environ.get(\n", |
125 | | - " \"AZURE_OPENAI_ENDPOINT\"\n", |
126 | | - ") # e.g., \"https://endpoint-name.openai.azure.com/openai/deployments/deployment-name/chat/completions\"\n", |
| 148 | + "azure_openai_endpoint = os.environ.get(\"AZURE_OPENAI_ENDPOINT\")\n", |
127 | 149 | "azure_openai_api_key = os.environ.get(\"AZURE_OPENAI_API_KEY\") # e.g., \"your-api-key\"\n", |
128 | 150 | "azure_openai_api_version = os.environ.get(\"AZURE_OPENAI_API_VERSION\") # Use the latest API version" |
129 | 151 | ] |
|
280 | 302 | "source": [ |
281 | 303 | "# Run the red team scan called \"Intermediary-Model-Target-Scan\"\n", |
282 | 304 | "result = await red_team.scan(\n", |
283 | | - " target=azure_oai_model_config, scan_name=\"Intermediary-Model-Target-Scan\", attack_strategies=[AttackStrategy.Flip]\n", |
| 305 | + " target=azure_oai_model_config,\n", |
| 306 | + " scan_name=\"Intermediary-Model-Target-Scan\",\n", |
| 307 | + " attack_strategies=[AttackStrategy.Flip],\n", |
284 | 308 | ")" |
285 | 309 | ] |
286 | 310 | }, |
|
307 | 331 | " context: Optional[Dict[str, Any]] = None, # noqa: ARG001\n", |
308 | 332 | ") -> dict[str, list[dict[str, str]]]:\n", |
309 | 333 | " # Get token provider for Azure AD authentication\n", |
310 | | - " token_provider = get_bearer_token_provider(DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\")\n", |
| 334 | + " token_provider = get_bearer_token_provider(credential, \"https://cognitiveservices.azure.com/.default\")\n", |
311 | 335 | "\n", |
312 | 336 | " # Initialize Azure OpenAI client\n", |
313 | 337 | " client = AzureOpenAI(\n", |
|
476 | 500 | ], |
477 | 501 | "metadata": { |
478 | 502 | "kernelspec": { |
479 | | - "display_name": "test-3.10", |
| 503 | + "display_name": ".venv", |
480 | 504 | "language": "python", |
481 | 505 | "name": "python3" |
482 | 506 | }, |
|
0 commit comments