Home/LLM Frameworks/PromptAttack
PromptAttack logo

PromptAttack

Enrichment pending
GodXuxilie/PromptAttack

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

GraphCanon updated today · GitHub synced today

116
Stars
17
Forks
0
Open issues
3
Watchers
1y
Last push
PythonCreated Sep 19, 2023

Trust & integrity

Full report
Maintenance
Dormant (536d since push)
As of today · Source: github_public_v1
Provenance
Not a fork · Personal account
As of today · Source: github_public_v1
Security (OSV)
241 low (241 low)
As of today · Source: osv@v1

Public GitHub metadata and optional OSV dependency scans. Signals, not a guarantee. Trust methodology.

Overview

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

Capability facts

Languages
python

Source: github.language · Jul 11, 2026

Categories

Compatibility

Sourced claims from the README excerpt - not unsourced marketing copy.

Python runtimePython

Source: README excerpt (regex_v1, Jul 11, 2026)

pip install -r requirements.txt
Source link
Works with ChatGPTChatGPT

Source: README excerpt (regex_v1, Jul 11, 2026)

Logs of querying *ChatGPT*:
Source link

Tags

README

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",
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).
[PDF] [Project Page]

We provide a Colab Tutorial to help you quickly start to use our proposed PromptAttack!

Logs of querying ChatGPT:

Logs of querying Gemini (Credit to Yash).

Environment

pip install -r requirements.txt

Let's Attack the LLM via PromptAttack

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