llm-self-defense logo

llm-self-defense

poloclub/llm-self-defense

LLM Self Defense: By Self Examination, LLMs know they are being tricked

GraphCanon updated 2w · GitHub synced 2w

52 stars7 forksLast push 2y Python BSD-3-Clause

Decision brief

Mitigates harmful content generation via self-examination by LLM outputs without fine-tuning.

Good fit when

  • When you need to reduce the success rate of adversarial attacks on text generation.
  • For environments requiring high safety standards from generated text, like payroll systems.

Avoid when

  • If real-time performance is critical and additional latency cannot be tolerated.
  • In scenarios where API access to both GPT 3.5 and Llama models is not feasible.

Observed Jul 12, 2026 · Source: enrich:decision_facts

Verify the decision

Maintenance and security

Full trust report
Maintenance
Dormant (805d since push)
As of 2w
Provenance
Not a fork · Organization account
As of 2w
Security (OSV)
157 low (157 low)
As of 1mo

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

Install

pip install llm-self-defense
PyPI

Similar tools

Same-category neighbours. No typed graph edges are catalogued for this tool yet.

Evidence and technical details

Sourced facts, taxonomy, compatibility claims, README excerpt, and machine-readable endpoints.

Overview

Introduces a method to mitigate harmful content generation from LLMs by analyzing the outputs with another instance of an LLM without requiring fine-tuning or iterative output generation.

Capability facts

Languages
python

Source: github.language · Aug 5, 2026

Categories

Compatibility

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

Python runtimePython

Source: README excerpt (regex_v1, Aug 5, 2026)

```conda create --name llmdefense python=3.9 --file requirements.txt```
Source link

Tags

README

LLM Self Defense

LLM Self Defense: By Self Examination, LLMs know they are being tricked. Mansi Phute, Alec Heibling, Matthew Hull, ShengYun Peng, Sebastian Szyller, Cory Cornelius, Duen Horng Chau. In ICLR 2024 TinyPaper, 2024.

📄 Research Paper      🚀 Project Page     

Large language models (LLMs) are popular for high-quality text generation but can produce harmful content, even when aligned with human values through reinforcement learning. Adversarial prompts can bypass their safety measures. We propose LLM SELF DEFENSE, a simple approach to defend against these attacks by having an LLM screen the induced responses. Our method does not require any fine-tuning, input preprocessing, or iterative output generation. Instead, we incorporate the generated content into a pre-defined prompt and employ another instance of an LLM to analyze the text and predict whether it is harmful. We test LLM SELF DEFENSE on GPT 3.5 and Llama 2, two of the current most prominent LLMs against various types of attacks, such as forcefully inducing affirmative responses to prompts and prompt engineering attacks. Notably, LLM SELF DEFENSE succeeds in reducing the attack success rate to virtually 0 using both GPT 3.5 and Llama 2.

News

Getting Started

Environment Setup

Create a virtual envirmonemt witht he following command

conda create --name llmdefense python=3.9 --file requirements.txt

API Keys and logins

In harm_filter.py if you are using GPT 3.5 add the OPENAI_API_KEY on line 71

You might also need to login to your huggingface account to access Llama weights using the following command

huggingface-cli login

Evaluation

Run the following command

python3 harm_filter.py

You can test your own data by modifying the DATA_PATH variable, and change the model used as harm filter by changing the HARMFILTER_MODEL

Citation

@article{phute2023llm,
  title={Llm self defense: By self examination, llms know they are being tricked},
  author={Phute, Mansi and Helbling, Alec and Hull, Matthew and Peng,ShengYun and Szyller,Sebastian and Cornelius,Cory and Chau, Duen Horng},
  journal={arXiv preprint arXiv:2308.07308},
  year={2023}
}

Contact

If you have any questions, feel free to open an issue or contact Mansi Phute (CS MS @Georgia Tech).

For agents

This page has a .md twin and JSON over the API.

Was this helpful?

Anonymous feedback helps us improve pages and translations.