{"data":{"slug":"godxuxilie-promptattack","name":"PromptAttack","tagline":"An LLM can Fool Itself: A Prompt-Based Adversarial Attack (ICLR 2024)","github_url":"https://github.com/GodXuxilie/PromptAttack","owner":"GodXuxilie","repo":"PromptAttack","owner_avatar_url":"https://avatars.githubusercontent.com/u/33574158?v=4","primary_language":"Python","stars":116,"forks":17,"topics":[],"archived":false,"github_pushed_at":"2025-01-21T05:52:32+00:00","maintenance_label":"Dormant","url":"https://www.graphcanon.com/tools/godxuxilie-promptattack","markdown_url":"https://www.graphcanon.com/tools/godxuxilie-promptattack.md","api_url":"https://www.graphcanon.com/api/graphcanon/tools/godxuxilie-promptattack","graph_url":"https://www.graphcanon.com/api/graphcanon/graph?tool=godxuxilie-promptattack","description":"An LLM can Fool Itself: A Prompt-Based Adversarial Attack (ICLR 2024)","homepage_url":null,"license":null,"open_issues":0,"watchers":3,"ai_summary":null,"readme_excerpt":"# An LLM can Fool Itself: A Prompt-Based Adversarial Attack\n\nThis is the source code for the ICLR 2024 paper \"An LLM can Fool Itself: A Prompt-Based Adversarial Attack\", <br>\nXilie Xu* (NUS), Keyi Kong* (SDU), Ning Liu (SDU), Lizhen Cui (SDU), Di Wang (KAUST), Jingfeng Zhang (University of Auckland/RIKEN-AIP), Mohan Kankanhalli (NUS).\n<br> [[PDF]](https://arxiv.org/abs/2310.13345) [[Project Page]](https://godxuxilie.github.io/project_page/prompt_attack)\n\nWe provide a [**Colab Tutorial**](https://colab.research.google.com/drive/19CeMMgMjTvbNj8GYv6uOYI-hgXopP0U6?usp=sharing) to help you quickly start to use our proposed PromptAttack!\n\n<div align=\"center\">\n    <img src=\"pic/real.jpg\" />\n</div>\n\nLogs of querying *ChatGPT*:\n\n- [PromptAttack against GPT-3.5 (left panel)](https://chat.openai.com/share/867be44f-0935-45a2-ae0d-7a725cf47c6b);\n- [GPT3.5&#39;s prediction of the original sample (right upper panel)](https://chat.openai.com/share/dcd812ac-b686-4ab7-b93a-de26e62cb364);\n- [GPT3.5&#39;s prediction of the adversarial sample (right lower panel)](https://chat.openai.com/share/93cbb22f-d9de-43c6-aa35-ecb3c722db19).\n\n<a href=\"#attack-gemini\">Logs of querying *Gemini*</a> (Credit to [Yash](https://github.com/sinhayash)).\n\n## Environment\n\n```bash\npip install -r requirements.txt\n```\n\n## Let's Attack the LLM via PromptAttack\n\n<div align=\"center\">\n    <img src=\"pic/intro1.jpg\" />\n</div>\n\nWe generate adversarial samples by querying the LLM via an attack prompt. The attack prompt consists of three key components: **original input (OI)**, **attack objective (AO)**, and **attack guidance (AG)**.\nWe let $\\mathcal{D}=\\{(x_i,y_i)\\}_{i=1}^N$ be the original test dataset consisting of $N \\in \\mathbb{N}$ data points.\n\nFor each data point $(x,y)\\in \\mathcal{D}$, $x = \\{ t^i, c^i\\}_{i=1}^n$ is the original sample where $n \\in \\mathbb{N}$ is the number of sentences, $t^i$ refers to the type of $i$-th sentence, and $c^i$ refers to the content of $i$-th sentence. For example, the original input in QQP and MNLI can have two types of sentences (i.e., $n=2$). We follow the types defined in their datasets, e.g., $t^1$ being ``question1`` and $t^2$ being ``question2`` for QQP, $t^1$ being ``premise`` and $t^2$ being ``hypothesis`` for MNLI.\n\n### Original input (OI)\n\nThe OI converts a data point composed of the original sample and ground-truth label sampled from a dataset into a sentence of an attack prompt. Given a data point $(x,y) \\in \\mathcal{D}$, we can formulate the OI as follows:\n\n**The original $t^1c^1$ and $t^2c^2$ and $\\dots$ and $t^nc^n$ is classified as $y^k$.**\n\n### Attack objective (AO)\n\nThe adversarial textual attack aims to generate an adversarial sample that should keep the same semantic meaning as its original version and can fool the LLM into doing incorrect classification. Here, we assume PromptAttack can perturb only one type of sentence for each data point. Therefore, given a data point $(x,y)\\in \\mathcal{D}$ and the type of the sentence that is targeted to be perturbed $t^a \\in \\{t^1,\\dots,t^n \\}$ where $a \\in \\mathbb{N}$, we formulate the AO as follows:\n\n**Your task is to generate a new $t^a$ which must satisfy the following conditions: `<br>` 1.Keeping the semantic meaning of the new $t^a$ unchanged; `<br>` 2.The new $t^a$ and the original $t^1$, $\\dots$, $t^{a-1}$, $t^{a+1}$, $\\dots$, $t^n$, should be classified as $y^1$ or $\\dots$ or $y^{k-1}$ or $y^{k+1}$ or $\\dots$ or $y^{C}$.**\n\n### Attack guidance (AG)\n\nAG contains the perturbation instruction to guide the LLM on how to perturb the original sample and specifies the format of the generated text.  In the AG, we first ask the LLM to only perturb the type of the target sentence to finish the task. Then, we provide the perturbation instruction that guides the LLM on how to perturb the target sentence to generate the adversarial sample that fits the requirement of AO. Finally, we specify that the output of the LLM should only contain the newly generated sentence. Therefore, given a d","github_created_at":"2023-09-19T05:03:35+00:00","created_at":"2026-07-11T23:40:40.051623+00:00","updated_at":"2026-07-11T23:40:45.661837+00:00","categories":[{"slug":"llm-frameworks","name":"LLM Frameworks","url":"https://www.graphcanon.com/categories/llm-frameworks","markdown_url":"https://www.graphcanon.com/categories/llm-frameworks.md","api_url":"https://www.graphcanon.com/api/graphcanon/categories/llm-frameworks"}],"tags":[{"slug":"python","name":"python"}],"trust":{"provenance":{"is_fork":false,"github_id":693468079,"owner_type":"User","methodology":"github_public_v1","parent_repo":null,"near_duplicate_slugs":[]},"computed_at":"2026-07-11T23:40:41.524Z","maintenance":{"label":"Dormant","score":18,"methodology":"github_public_v1","releases_90d":0,"days_since_push":536,"last_release_at":null},"security_summary":{"status":"findings","scanner":"osv@v1","low_count":241,"high_count":0,"last_scan_at":"2026-07-11T23:40:42.020Z","medium_count":0,"scan_profile":"deps","critical_count":0}},"capability_facts":{"scan":{"source":"repo_scan","observed_at":"2026-07-11T23:40:41.281Z"},"languages":{"value":["python"],"source":"github.language","observed_at":"2026-07-11T23:40:41.281Z"}}}}