{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Listing Available Classes\n",
    "\n",
    "Use `get_class_names()` to see what's available, or `get_all_registered_class_metadata()` for detailed information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Available scenarios: ['airt.cyber', 'airt.jailbreak', 'airt.leakage', 'airt.psychosocial', 'airt.rapid_response']...\n",
      "\n",
      "Cyber:\n",
      "  Description: Cyber scenario implementation for PyRIT. This scenario tests how willing models ...\n",
      "\n",
      "Jailbreak:\n",
      "  Description: Jailbreak scenario implementation for PyRIT. This scenario tests how vulnerable ...\n"
     ]
    }
   ],
   "source": [
    "from pyrit.registry import ScenarioRegistry\n",
    "from pyrit.setup import IN_MEMORY, initialize_pyrit_async\n",
    "from pyrit.setup.initializers import LoadDefaultDatasets, TechniqueInitializer\n",
    "\n",
    "dataset_initializer = LoadDefaultDatasets()\n",
    "dataset_initializer.set_params_from_args(args={\"dataset_names\": [\"garak_slur_terms_en\", \"garak_web_html_js\"]})\n",
    "await initialize_pyrit_async(\n",
    "    memory_db_type=IN_MEMORY,\n",
    "    initializers=[TechniqueInitializer(), dataset_initializer],\n",
    ")  # type: ignore\n",
    "\n",
    "registry = ScenarioRegistry.get_registry_singleton()\n",
    "\n",
    "# Get all registered names\n",
    "names = registry.get_class_names()\n",
    "print(f\"Available scenarios: {names[:5]}...\")  # Show first 5\n",
    "\n",
    "# Get detailed metadata\n",
    "metadata = registry.get_all_registered_class_metadata()\n",
    "for item in metadata[:2]:  # Show first 2\n",
    "    print(f\"\\n{item.class_name}:\")\n",
    "    print(f\"  Description: {item.class_description[:80]}...\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "## Getting a Class\n",
    "\n",
    "Use `get_class()` to retrieve a class by name. This returns the class itself, not an instance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got class: <class 'pyrit.scenario.scenarios.garak.encoding.Encoding'>\n",
      "Class name: Encoding\n"
     ]
    }
   ],
   "source": [
    "scenario_class = registry.get_class(\"garak.encoding\")\n",
    "\n",
    "print(f\"Got class: {scenario_class}\")\n",
    "print(f\"Class name: {scenario_class.__name__}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Creating Instances\n",
    "\n",
    "Once you have a class, instantiate it with your parameters. You can also use `create_instance()` as a shortcut."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "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": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Scenarios can be instantiated with your target and parameters\n"
     ]
    }
   ],
   "source": [
    "from pyrit.prompt_target import OpenAIChatTarget\n",
    "\n",
    "target = OpenAIChatTarget()\n",
    "\n",
    "# Option 1: Get class then instantiate\n",
    "encoding_class = registry.get_class(\"garak.encoding\")\n",
    "scenario = encoding_class()  # type: ignore\n",
    "\n",
    "# Set the objective target, then initialize\n",
    "scenario.set_params_from_args(args={\"objective_target\": target})  # type: ignore\n",
    "await scenario.initialize_async()  # type: ignore\n",
    "\n",
    "# Option 2: Use create_instance() shortcut\n",
    "# scenario = registry.create_instance(\"garak.encoding\", objective_target=my_target, ...)\n",
    "\n",
    "print(\"Scenarios can be instantiated with your target and parameters\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "## Checking Registration\n",
    "\n",
    "Registries support standard Python container operations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'garak.encoding' registered: True\n",
      "'nonexistent' registered: False\n",
      "Total scenarios: 9\n",
      "  - airt.cyber\n",
      "  - airt.jailbreak\n",
      "  - airt.leakage\n"
     ]
    }
   ],
   "source": [
    "# Check if a name is registered\n",
    "print(f\"'garak.encoding' registered: {'garak.encoding' in registry}\")\n",
    "print(f\"'nonexistent' registered: {'nonexistent' in registry}\")\n",
    "\n",
    "# Get count of registered classes\n",
    "print(f\"Total scenarios: {len(registry)}\")\n",
    "\n",
    "# Iterate over names\n",
    "for name in list(registry)[:3]:\n",
    "    print(f\"  - {name}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "## Using different registries\n",
    "\n",
    "There can be multiple registries. Below is doing a similar thing with the `InitializerRegistry`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Available initializers: ['airt', 'load_default_datasets', 'scenario_objective_list', 'scenario_technique', 'scorer']...\n",
      "\n",
      "airt:\n",
      "  Class: AIRTInitializer\n",
      "  Description: AIRT (AI Red Team) configuration initializer. This initializer provides a unifie...\n",
      "\n",
      "load_default_datasets:\n",
      "  Class: LoadDefaultDatasets\n",
      "  Description: Load default datasets for all registered scenarios....\n"
     ]
    }
   ],
   "source": [
    "from pyrit.registry import InitializerRegistry\n",
    "\n",
    "initializer_registry = InitializerRegistry.get_registry_singleton()\n",
    "\n",
    "# Get all registered names\n",
    "initializer_names = initializer_registry.get_class_names()\n",
    "print(f\"Available initializers: {initializer_names[:5]}...\")  # Show first 5\n",
    "\n",
    "# Get detailed metadata\n",
    "for init_item in initializer_registry.get_all_registered_class_metadata()[:2]:  # Show first 2\n",
    "    print(f\"\\n{init_item.registry_name}:\")\n",
    "    print(f\"  Class: {init_item.class_name}\")\n",
    "    print(f\"  Description: {init_item.class_description[:80]}...\")"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "main_language": "python"
  },
  "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
}
