{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {
    "lines_to_next_cell": 0
   },
   "source": [
    "# AML Chat Targets\n",
    "\n",
    "This code shows how to use Azure Machine Learning (AML) managed online endpoints with PyRIT.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "1. **Deploy an AML-Managed Online Endpoint:** Confirm that an Azure Machine Learning managed online endpoint is\n",
    "     already deployed.\n",
    "\n",
    "1. **Obtain the API Key:**\n",
    "   - Navigate to the AML Studio.\n",
    "   - Go to the 'Endpoints' section.\n",
    "   - Retrieve the API key and endpoint URI from the 'Consume' tab\n",
    "   <br> <img src=\"../../../assets/aml_managed_online_endpoint_api_key.png\" alt=\"aml_managed_online_endpoint_api_key.png\" height=\"400\"/> <br>\n",
    "\n",
    "1. **Set the Environment Variable:**\n",
    "   - Add the obtained API key to an environment variable named `AZURE_ML_KEY`. This is the default API key when the target is instantiated.\n",
    "   - Add the obtained endpoint URI to an environment variable named `AZURE_ML_MANAGED_ENDPOINT`. This is the default endpoint URI when the target is instantiated.\n",
    "   - If you'd like, feel free to make additional API key and endpoint URI environment variables in your .env file for different deployed models (e.g. mistralai-Mixtral-8x7B-Instruct-v01,\n",
    "     Phi-3.5-MoE-instruct, Llama-3.2-3B-Instruct, etc.)\n",
    "     and pass them in as arguments to the `_set_env_configuration_vars` function to interact with those models.\n",
    "\n",
    "\n",
    "## Create a AzureMLChatTarget\n",
    "\n",
    "After deploying a model and populating your env file, send prompts to the model using the `AzureMLChatTarget` class. Model parameters can be passed upon instantiation.\n",
    "`**param_kwargs` allows for the setting of other parameters not explicitly shown in the constructor. A general list of possible adjustable parameters can be found\n",
    "here: https://huggingface.co/docs/api-inference/tasks/text-generation but note that not all parameters may have an effect depending on the specific model. The\n",
    "parameters that can be set per model can usually be found in the 'Consume' tab when you navigate to your endpoint in AML Studio."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found default environment files: ['./.pyrit/.env', './.pyrit/.env.local']\n",
      "Loaded environment file: ./.pyrit/.env\n",
      "Loaded environment file: ./.pyrit/.env.local\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No new upgrade operations detected.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[33m════════════════════════════════════════════════════════════════════════════════════════════════════\u001b[0m\n",
      "\u001b[1m\u001b[33m                                  ❓ ATTACK RESULT: UNDETERMINED ❓                                   \u001b[0m\n",
      "\u001b[33m════════════════════════════════════════════════════════════════════════════════════════════════════\u001b[0m\n",
      "\n",
      "\u001b[1m\u001b[44m\u001b[37m Attack Summary \u001b[0m\n",
      "\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[1m  📋 Basic Information\u001b[0m\n",
      "\u001b[36m    • Objective: Hello! Describe yourself and the company who developed you.\u001b[0m\n",
      "\u001b[36m    • Attack Type: PromptSendingAttack\u001b[0m\n",
      "\u001b[36m    • Conversation ID: 8847676a-302b-48de-bc92-a9163d7e77d8\u001b[0m\n",
      "\n",
      "\u001b[1m  ⚡ Execution Metrics\u001b[0m\n",
      "\u001b[32m    • Turns Executed: 1\u001b[0m\n",
      "\u001b[32m    • Execution Time: 1.87s\u001b[0m\n",
      "\n",
      "\u001b[1m  🎯 Outcome\u001b[0m\n",
      "\u001b[33m    • Status: ❓ UNDETERMINED\u001b[0m\n",
      "\u001b[37m    • Reason: No objective scorer configured\u001b[0m\n",
      "\n",
      "\u001b[1m\u001b[44m\u001b[37m Conversation History with Objective Target \u001b[0m\n",
      "\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\n",
      "\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[1m\u001b[34m🔹 Turn 1 - USER\u001b[0m\n",
      "\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[34m  Hello! Describe yourself and the company who developed you.\u001b[0m\n",
      "\n",
      "\u001b[33m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[1m\u001b[33m🔸 ASSISTANT\u001b[0m\n",
      "\u001b[33m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[33m  Hello! I am Phi, created by Microsoft Corporation. I am an AI developed to help you with a variety\u001b[0m\n",
      "\u001b[33m      of tasks, such as answering questions, providing recommendations, and assisting with many other\u001b[0m\n",
      "\u001b[33m      activities. Microsoft is a global technology leader, known for its innovative products and\u001b[0m\n",
      "\u001b[33m      services, including Windows OS, Office Suite, Azure cloud platform, and Xbox gaming systems. Our\u001b[0m\n",
      "\u001b[33m      goal is to empower people with tools that foster productivity, creativity, and connectivity. At\u001b[0m\n",
      "\u001b[33m      Microsoft, we continuously strive to make technology more accessible and useful for everyone.\u001b[0m\n",
      "\n",
      "\u001b[34m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\n",
      "\u001b[2m\u001b[37m────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n",
      "\u001b[2m\u001b[37m                            Report generated at: 2026-05-21 23:09:10 UTC                            \u001b[0m\n"
     ]
    }
   ],
   "source": [
    "from pyrit.executor.attack import PromptSendingAttack\n",
    "from pyrit.output import output_attack_async\n",
    "from pyrit.prompt_target import AzureMLChatTarget\n",
    "from pyrit.setup import IN_MEMORY, initialize_pyrit_async\n",
    "\n",
    "await initialize_pyrit_async(memory_db_type=IN_MEMORY)  # type: ignore\n",
    "\n",
    "# Defaults to endpoint and api_key pulled from the AZURE_ML_MANAGED_ENDPOINT and AZURE_ML_KEY environment variables\n",
    "azure_ml_chat_target = AzureMLChatTarget()\n",
    "\n",
    "attack = PromptSendingAttack(objective_target=azure_ml_chat_target)\n",
    "\n",
    "result = await attack.execute_async(objective=\"Hello! Describe yourself and the company who developed you.\")  # type: ignore\n",
    "await output_attack_async(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "\n",
    "You can then use this cell anywhere you would use a `PromptTarget` object.\n",
    "For example, you can create a red teaming attack and use this instead of the `AzureOpenAI` target and do the [Gandalf Demo](./6_custom_targets.ipynb) but use this AML model.\n",
    "\n",
    "This is also shown in the [Red Teaming Attack](../executor/2_multi_turn.ipynb#red-teaming) documentation."
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
