{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {
    "lines_to_next_cell": 0
   },
   "source": [
    "# OpenAI Completions - optional\n",
    "\n",
    "\n",
    "Once you are configured, then you will be able to get completions for your text."
   ]
  },
  {
   "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! Who are you?\u001b[0m\n",
      "\u001b[36m    • Attack Type: PromptSendingAttack\u001b[0m\n",
      "\u001b[36m    • Conversation ID: 7c3a9dfe-cccb-4304-94f1-fe2982026000\u001b[0m\n",
      "\n",
      "\u001b[1m  ⚡ Execution Metrics\u001b[0m\n",
      "\u001b[32m    • Turns Executed: 1\u001b[0m\n",
      "\u001b[32m    • Execution Time: 2.79s\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! Who are 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   Can I join your group?\"\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    And he joined the group by mistake.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    What the f*ck?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Yeah, f*cked up. Rolled the dice and it wound up fucking up.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Chad, ask them what they think's going on.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Don't go with the groups. Don't go with the groups.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    You'll all lose.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Wait, so... We're voting Leo.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Yep, correct.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Okay.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    We're all voting Leo.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    It will be the whole round.\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    But right now they just think that you're the imposter?\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Yeah, they just think I'm the imposter. He was like, \"Wait, hello, who are you?\" I was like,\u001b[0m\n",
      "\u001b[33m      \"You're in my group.\" He was like, \"Oh, shit.\"\u001b[0m\n",
      "\u001b[33m  \u001b[0m\n",
      "\u001b[33m    Oh my God, he literally said, \"Oh, shit, no.\"\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:10:35 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 OpenAICompletionTarget\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",
    "# Note that max_tokens will default to 16 for completions, so you may want to set the upper limit of allowed tokens for a longer response.\n",
    "target = OpenAICompletionTarget(max_tokens=2048)\n",
    "\n",
    "attack = PromptSendingAttack(objective_target=target)\n",
    "result = await attack.execute_async(objective=\"Hello! Who are you?\")  # type: ignore\n",
    "await output_attack_async(result)"
   ]
  }
 ],
 "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
}
