Skip to content

Commit 6622714

Browse files
authored
Changes to run as AzureCliCredential from notebook in VSC (#272)
* Changes to run as AzureCliCredential from notebook in VSC * pre-commit corrections * AI Red Team example against Copilot Studio agent
1 parent 005d470 commit 6622714

File tree

4 files changed

+693
-20
lines changed

4 files changed

+693
-20
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ deployment.json
1414
*result.jsonl
1515
*results.json
1616
*results.jsonl
17+
*.scan_*
18+
*-Scan.json
1719

1820
## Python Gitignore
1921
## From: https://github.com/github/gitignore/blob/main/Python.gitignore
@@ -181,3 +183,5 @@ cython_debug/
181183

182184
# macOS Finder
183185
.DS_Store
186+
187+
token_cache.bin

scenarios/evaluate/AI_RedTeaming/AI_RedTeaming.ipynb

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
"### Prerequisite\n",
1818
"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",
1919
"\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",
2127
"\n",
2228
"**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",
2329
"\n",
@@ -38,7 +44,7 @@
3844
"\n",
3945
"```bash\n",
4046
"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",
4248
"```\n",
4349
"\n",
4450
"\n",
@@ -65,14 +71,36 @@
6571
"import os\n",
6672
"\n",
6773
"# Azure imports\n",
68-
"from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n",
6974
"from azure.ai.evaluation.red_team import RedTeam, RiskCategory, AttackStrategy\n",
7075
"\n",
7176
"# 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",
73101
"\n",
74102
"# Initialize Azure credentials\n",
75-
"credential = DefaultAzureCredential()"
103+
"credential = AzureCliCredential()"
76104
]
77105
},
78106
{
@@ -83,6 +111,8 @@
83111
"\n",
84112
"Set the following variables for use in this notebook. These variables connect to your Azure resources and model deployments.\n",
85113
"\n",
114+
"Set these variables by creating an `.env` file in your project's root folder.\n",
115+
"\n",
86116
"**Note:** You can find these values in your Azure AI Foundry project or Azure OpenAI resource."
87117
]
88118
},
@@ -95,14 +125,12 @@
95125
"```\n",
96126
"# Azure OpenAI\n",
97127
"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",
99129
"AZURE_OPENAI_DEPLOYMENT_NAME=\"gpt-4\"\n",
100130
"AZURE_OPENAI_API_VERSION=\"2024-12-01-preview\"\n",
101131
"\n",
102132
"# 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",
106134
"```"
107135
]
108136
},
@@ -113,17 +141,11 @@
113141
"outputs": [],
114142
"source": [
115143
"# 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",
121145
"\n",
122146
"# Azure OpenAI deployment information\n",
123147
"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",
127149
"azure_openai_api_key = os.environ.get(\"AZURE_OPENAI_API_KEY\") # e.g., \"your-api-key\"\n",
128150
"azure_openai_api_version = os.environ.get(\"AZURE_OPENAI_API_VERSION\") # Use the latest API version"
129151
]
@@ -280,7 +302,9 @@
280302
"source": [
281303
"# Run the red team scan called \"Intermediary-Model-Target-Scan\"\n",
282304
"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",
284308
")"
285309
]
286310
},
@@ -307,7 +331,7 @@
307331
" context: Optional[Dict[str, Any]] = None, # noqa: ARG001\n",
308332
") -> dict[str, list[dict[str, str]]]:\n",
309333
" # 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",
311335
"\n",
312336
" # Initialize Azure OpenAI client\n",
313337
" client = AzureOpenAI(\n",
@@ -476,7 +500,7 @@
476500
],
477501
"metadata": {
478502
"kernelspec": {
479-
"display_name": "test-3.10",
503+
"display_name": ".venv",
480504
"language": "python",
481505
"name": "python3"
482506
},

0 commit comments

Comments
 (0)