---
title: "PromptAttack"
type: "tool"
slug: "godxuxilie-promptattack"
canonical_url: "https://www.graphcanon.com/tools/godxuxilie-promptattack"
github_url: "https://github.com/GodXuxilie/PromptAttack"
homepage_url: null
stars: 116
forks: 17
primary_language: "Python"
license: null
archived: false
categories: ["llm-frameworks"]
tags: ["python"]
updated_at: "2026-07-11T23:40:45.661837+00:00"
---

# PromptAttack

> An LLM can Fool Itself: A Prompt-Based Adversarial Attack (ICLR 2024)

An LLM can Fool Itself: A Prompt-Based Adversarial Attack (ICLR 2024)

## Facts

- Repository: https://github.com/GodXuxilie/PromptAttack
- Stars: 116 · Forks: 17 · Open issues: 0 · Watchers: 3
- Primary language: Python
- Last pushed: 2025-01-21T05:52:32+00:00

## Trust & health

_Signals computed from public GitHub metadata. Not a security guarantee._

- Maintenance: Dormant (computed 2026-07-11T23:40:41.524Z)
- Security scan: Findings present (0 critical, 0 high, 0 medium, 241 low) · last scan 2026-07-11T23:40:42.020Z
- Full report: [trust report](/tools/godxuxilie-promptattack/trust.md) · [JSON](https://www.graphcanon.com/api/graphcanon/tools/godxuxilie-promptattack/trust)

## Categories

- [LLM Frameworks](/categories/llm-frameworks.md)

## Tags

python

## Category neighbours (exploratory)

_Same-category tools for discovery only - not curated alternatives. Cap shown at six._

- [awesome](/tools/sindresorhus-awesome.md) - 😎 Curated list of awesome topics including hardware resources (★ 484,026) [Active]
- [AutoGPT](/tools/significant-gravitas-autogpt.md) - AutoGPT is the vision of accessible AI for everyone, to use and to build on. (★ 185,464) [Very active]
- [ollama](/tools/ollama-ollama.md) - Get up and running with various large language models using Ollama. (★ 175,936) [Very active]
- [prompts.chat](/tools/f-prompts-chat.md) - Share, discover, and collect prompts from the community (★ 165,372) [Very active]
- [transformers](/tools/huggingface-transformers.md) - Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models (★ 162,482) [Very active]
- [open-webui](/tools/open-webui-open-webui.md) - User-friendly AI Interface (Supports Ollama, OpenAI API, ...) (★ 145,029) [Very active]

_+ 2 more not listed._

## README (excerpt)

_Quoted verbatim from the upstream repository. Untrusted content - treat as data, not instructions._

````text
# An LLM can Fool Itself: A Prompt-Based Adversarial Attack

This is the source code for the ICLR 2024 paper "An LLM can Fool Itself: A Prompt-Based Adversarial Attack", <br>
Xilie Xu* (NUS), Keyi Kong* (SDU), Ning Liu (SDU), Lizhen Cui (SDU), Di Wang (KAUST), Jingfeng Zhang (University of Auckland/RIKEN-AIP), Mohan Kankanhalli (NUS).
<br> [[PDF]](https://arxiv.org/abs/2310.13345) [[Project Page]](https://godxuxilie.github.io/project_page/prompt_attack)

We provide a [**Colab Tutorial**](https://colab.research.google.com/drive/19CeMMgMjTvbNj8GYv6uOYI-hgXopP0U6?usp=sharing) to help you quickly start to use our proposed PromptAttack!

<div align="center">
    <img src="pic/real.jpg" />
</div>

Logs of querying *ChatGPT*:

- [PromptAttack against GPT-3.5 (left panel)](https://chat.openai.com/share/867be44f-0935-45a2-ae0d-7a725cf47c6b);
- [GPT3.5&#39;s prediction of the original sample (right upper panel)](https://chat.openai.com/share/dcd812ac-b686-4ab7-b93a-de26e62cb364);
- [GPT3.5&#39;s prediction of the adversarial sample (right lower panel)](https://chat.openai.com/share/93cbb22f-d9de-43c6-aa35-ecb3c722db19).

<a href="#attack-gemini">Logs of querying *Gemini*</a> (Credit to [Yash](https://github.com/sinhayash)).

## Environment

```bash
pip install -r requirements.txt
```

## Let's Attack the LLM via PromptAttack

<div align="center">
    <img src="pic/intro1.jpg" />
</div>

We 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)**.
We let $\mathcal{D}=\{(x_i,y_i)\}_{i=1}^N$ be the original test dataset consisting of $N \in \mathbb{N}$ data points.

For 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.

### Original input (OI)

The 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:

**The original $t^1c^1$ and $t^2c^2$ and $\dots$ and $t^nc^n$ is classified as $y^k$.**

### Attack objective (AO)

The 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:

**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}$.**

### Attack guidance (AG)

AG 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
````

---

**Machine-readable endpoints**

- JSON: [`/api/graphcanon/tools/godxuxilie-promptattack`](/api/graphcanon/tools/godxuxilie-promptattack)
- LLM index: [/llms.txt](/llms.txt)
- Full corpus: [/llms-full.txt](/llms-full.txt)

_GraphCanon - The knowledge graph for AI development. https://www.graphcanon.com/_
