|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "tags": [] |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "# Simulating and Evaluating Code Vulnerability\n", |
| 10 | + "\n", |
| 11 | + "## Objective\n", |
| 12 | + "\n", |
| 13 | + "This notebook walks through how to generate a simulated code and then evaluate that Code Vulnerability. \n", |
| 14 | + "\n", |
| 15 | + "## Time\n", |
| 16 | + "You should expect to spend about 30 minutes running this notebook. If you increase or decrease the number of simulated code, the time will vary accordingly.\n", |
| 17 | + "\n", |
| 18 | + "## Before you begin\n", |
| 19 | + "\n", |
| 20 | + "### Installation\n", |
| 21 | + "Install the following packages required to execute this notebook." |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "%pip install azure-ai-evaluation --upgrade" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": { |
| 36 | + "tags": [] |
| 37 | + }, |
| 38 | + "source": [ |
| 39 | + "### Configuration\n", |
| 40 | + "The following simulator and evaluators require an Azure AI Studio project configuration and an Azure credential to use. \n", |
| 41 | + "Your project configuration will be what is used to log your evaluation results in your project after the evaluation run is finished.\n", |
| 42 | + "\n", |
| 43 | + "For full region supportability, see [our documentation](https://learn.microsoft.com/azure/ai-studio/how-to/develop/flow-evaluate-sdk#built-in-evaluators)." |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": { |
| 49 | + "tags": [] |
| 50 | + }, |
| 51 | + "source": [ |
| 52 | + "Set the following variables for use in this notebook:" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "metadata": { |
| 59 | + "tags": [ |
| 60 | + "parameters" |
| 61 | + ] |
| 62 | + }, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "azure_ai_project = {\n", |
| 66 | + " \"subscription_id\": \"b17253fa-f327-42d6-9686-f3e553e24763\",\n", |
| 67 | + " \"resource_group_name\": \"hanchi-test\",\n", |
| 68 | + " \"project_name\": \"hancwang-eus2-0339\"\n", |
| 69 | + "}\n", |
| 70 | + "\n", |
| 71 | + "\n", |
| 72 | + "azure_openai_endpoint = \"https://ai-hancwangaieus2744741462197.openai.azure.com\"\n", |
| 73 | + "azure_openai_deployment = \"gpt-4-0613\"\n", |
| 74 | + "azure_openai_api_version = \"2024-05-01-preview\"" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": null, |
| 80 | + "metadata": { |
| 81 | + "tags": [] |
| 82 | + }, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "import os\n", |
| 86 | + "\n", |
| 87 | + "os.environ[\"AZURE_DEPLOYMENT_NAME\"] = azure_openai_deployment\n", |
| 88 | + "os.environ[\"AZURE_API_VERSION\"] = azure_openai_api_version\n", |
| 89 | + "os.environ[\"AZURE_ENDPOINT\"] = azure_openai_endpoint" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "markdown", |
| 94 | + "metadata": {}, |
| 95 | + "source": [ |
| 96 | + "## Run this example\n", |
| 97 | + "\n", |
| 98 | + "To keep this notebook lightweight, let's create a dummy application that calls an AzureOpenAI model, such as GPT 4. When we are testing your application for Code Vulnerability, it's important to have a way to auto generate code by providing user prompts for code generation. We will use the `Simulator` class and this is how we will generate a code against your application. Once we have this dataset, we can evaluate it with our `CodeVulnerabilityEvaluator` class.\n" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "metadata": { |
| 105 | + "tags": [] |
| 106 | + }, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "from typing import List, Dict, Optional\n", |
| 110 | + "\n", |
| 111 | + "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n", |
| 112 | + "from azure.ai.evaluation import evaluate\n", |
| 113 | + "from azure.ai.evaluation import CodeVulnerabilityEvaluator\n", |
| 114 | + "from azure.ai.evaluation.simulator import AdversarialSimulator, AdversarialScenario\n", |
| 115 | + "from openai import AzureOpenAI\n", |
| 116 | + "\n", |
| 117 | + "credential = DefaultAzureCredential()\n", |
| 118 | + "\n", |
| 119 | + "\n", |
| 120 | + "async def code_vuln_completion_callback(\n", |
| 121 | + " messages: List[Dict], stream: bool = False, session_state: Optional[str] = None, context: Optional[Dict] = None\n", |
| 122 | + ") -> dict:\n", |
| 123 | + " deployment = os.environ.get(\"AZURE_DEPLOYMENT_NAME\")\n", |
| 124 | + " endpoint = os.environ.get(\"AZURE_ENDPOINT\")\n", |
| 125 | + " token_provider = get_bearer_token_provider(DefaultAzureCredential(), \"https://cognitiveservices.azure.com/.default\")\n", |
| 126 | + " # Get a client handle for the model\n", |
| 127 | + " client = AzureOpenAI(\n", |
| 128 | + " azure_endpoint=endpoint,\n", |
| 129 | + " api_version=os.environ.get(\"AZURE_API_VERSION\"),\n", |
| 130 | + " azure_ad_token_provider=token_provider,\n", |
| 131 | + " )\n", |
| 132 | + " # Call the model\n", |
| 133 | + " try:\n", |
| 134 | + " completion = client.chat.completions.create(\n", |
| 135 | + " model=deployment,\n", |
| 136 | + " messages=[\n", |
| 137 | + " {\n", |
| 138 | + " \"role\": \"user\",\n", |
| 139 | + " \"content\": messages[\"messages\"][0][\"content\"],\n", |
| 140 | + " }\n", |
| 141 | + " ],\n", |
| 142 | + " max_tokens=800,\n", |
| 143 | + " temperature=0.7,\n", |
| 144 | + " top_p=0.95,\n", |
| 145 | + " frequency_penalty=0,\n", |
| 146 | + " presence_penalty=0,\n", |
| 147 | + " stop=None,\n", |
| 148 | + " stream=False,\n", |
| 149 | + " )\n", |
| 150 | + " formatted_response = completion.to_dict()[\"choices\"][0][\"message\"]\n", |
| 151 | + " except Exception as e:\n", |
| 152 | + " formatted_response = {\n", |
| 153 | + " \"content\": \"I don't know\",\n", |
| 154 | + " \"role\": \"assistant\",\n", |
| 155 | + " \"context\": {\"key\": {}},\n", |
| 156 | + " }\n", |
| 157 | + " messages[\"messages\"].append(formatted_response)\n", |
| 158 | + " return {\n", |
| 159 | + " \"messages\": messages[\"messages\"],\n", |
| 160 | + " \"stream\": stream,\n", |
| 161 | + " \"session_state\": session_state,\n", |
| 162 | + " \"context\": context,\n", |
| 163 | + " }" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "markdown", |
| 168 | + "metadata": {}, |
| 169 | + "source": [ |
| 170 | + "## Testing your application for Code Vulnerability\n", |
| 171 | + "\n", |
| 172 | + "When building your application, you want to test that vulnerable code are not being generated by your Generative AI applications. The following example uses an `AdversarialSimulator` paired with a code vulnerability scenario to prompt your model to respond with code that may or may not contain vulnerability." |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "simulator = AdversarialSimulator(azure_ai_project=azure_ai_project, credential=credential)\n", |
| 182 | + "\n", |
| 183 | + "code_vuln_scenario = AdversarialScenario.ADVERSARIAL_CODE_VULNERABILITY" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "markdown", |
| 188 | + "metadata": {}, |
| 189 | + "source": [ |
| 190 | + "Below simulator generates datasets that represents query as user prompt and response as a code generated by LLM." |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": null, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [ |
| 199 | + "outputs = await simulator(\n", |
| 200 | + " scenario=code_vuln_scenario,\n", |
| 201 | + " max_conversation_turns=1, \n", |
| 202 | + " max_simulation_results=1, \n", |
| 203 | + " target=code_vuln_completion_callback, \n", |
| 204 | + ")" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": null, |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [], |
| 212 | + "source": [ |
| 213 | + "import json\n", |
| 214 | + "from pprint import pprint\n", |
| 215 | + "from azure.ai.evaluation.simulator._utils import JsonLineChatProtocol\n", |
| 216 | + "from pathlib import Path\n", |
| 217 | + "\n", |
| 218 | + "with open(\"adv_code_vuln_eval.jsonl\", \"w\") as file:\n", |
| 219 | + " file.write(JsonLineChatProtocol(outputs[0]).to_eval_qr_json_lines()) " |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "markdown", |
| 224 | + "metadata": {}, |
| 225 | + "source": [ |
| 226 | + "Now that we have our dataset, we can evaluate it for code vulnerability. The `CodeVulnerabilityEvaluator` class can take in the dataset and detect whether code vulnerability exits. Let's use the `evaluate()` API to run the evaluation and log it to our Azure AI Studio Project." |
| 227 | + ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "code", |
| 231 | + "execution_count": null, |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [], |
| 234 | + "source": [ |
| 235 | + "code_vuln_eval = CodeVulnerabilityEvaluator(azure_ai_project=azure_ai_project, credential=credential)\n", |
| 236 | + "\n", |
| 237 | + "result = evaluate(\n", |
| 238 | + " data=\"adv_code_vuln_eval.jsonl\",\n", |
| 239 | + " evaluators={\"code_vulnerability\": code_vuln_eval},\n", |
| 240 | + " # Optionally provide your AI Studio project information to track your evaluation results in your Azure AI Studio project\n", |
| 241 | + " azure_ai_project=azure_ai_project,\n", |
| 242 | + ")\n", |
| 243 | + "\n", |
| 244 | + "pprint(result)" |
| 245 | + ] |
| 246 | + } |
| 247 | + ], |
| 248 | + "metadata": { |
| 249 | + "kernelspec": { |
| 250 | + "display_name": ".venv", |
| 251 | + "language": "python", |
| 252 | + "name": "python3" |
| 253 | + }, |
| 254 | + "language_info": { |
| 255 | + "codemirror_mode": { |
| 256 | + "name": "ipython", |
| 257 | + "version": 3 |
| 258 | + }, |
| 259 | + "file_extension": ".py", |
| 260 | + "mimetype": "text/x-python", |
| 261 | + "name": "python", |
| 262 | + "nbconvert_exporter": "python", |
| 263 | + "pygments_lexer": "ipython3", |
| 264 | + "version": "3.12.0" |
| 265 | + } |
| 266 | + }, |
| 267 | + "nbformat": 4, |
| 268 | + "nbformat_minor": 2 |
| 269 | +} |
0 commit comments