# PurpleLlama

> Meta's open trust-and-safety tools and benchmarks for evaluating and guarding LLMs

- Category: Evaluation & Production Ops / Evaluation & Red-Teaming
- Language: Python
- Licence: MIT
- Repository: https://github.com/meta-llama/PurpleLlama
- Website: https://meta-llama.github.io/PurpleLlama
- Canonical page: https://ai-tldr.dev/tools/purplellama/

## In short

PurpleLlama is an umbrella project from Meta that gathers tools and evaluations for building responsibly with open generative AI models. The name comes from the cybersecurity idea of "purple teaming" — combining red-team (attack) and blue-team (defense) work — applied to LLM safety. It sits in the Evaluation & Red-Teaming category of the Evaluation & Production Ops tools and is open-source, written in Python and licensed MIT.

## Overview

PurpleLlama is an umbrella project from Meta that gathers tools and evaluations for building responsibly with open generative AI models. The name comes from the cybersecurity idea of "purple teaming" — combining red-team (attack) and blue-team (defense) work — applied to LLM safety. Its first releases focus on cybersecurity benchmarks and input/output safeguards, with more planned over time.

It is aimed at developers and ML teams who run LLMs in production and need to filter inputs and outputs, score a model's security risk, or catch insecure generated code. The project bundles several components: Llama Guard for content moderation, Prompt Guard for detecting prompt injection and jailbreaks, Code Shield for filtering insecure code, and CyberSecEval as a cybersecurity benchmark suite.

As an eval-and-guardrail framework, PurpleLlama sits between your application and the model. The safeguard models classify or block risky content at inference time, while the benchmarks let you measure how a given model behaves before you ship it.

## What it does

- Llama Guard: input and output moderation models, fine-tuned from Llama 3.1/3.2, that detect content matching the MLCommons hazards taxonomy
- Prompt Guard: a model that flags prompt injection and jailbreak attempts before they reach your LLM
- Code Shield: inference-time filtering of insecure code generated by LLMs, with block/warn treatment recommendations
- CyberSecEval: cybersecurity benchmarks for LLMs based on industry standards like CWE and MITRE ATT&CK
- Permissive licensing: evals and benchmarks under MIT; safeguard models under the corresponding Llama Community License
- Covers both attack and defense ("purple teaming") so you can evaluate risk and add guardrails from one project

## Getting started

The components ship separately. Code Shield is available as a pip package; the Llama Guard and Prompt Guard models are downloaded from Hugging Face. Here is a minimal Code Shield example.

1. Install Code Shield

   Install the codeshield package from PyPI.

```bash
pip3 install codeshield
```

2. Scan LLM-generated code

   Call CodeShield.scan_code on model output and act on the result's is_insecure flag and recommended_treatment.

```python
from codeshield.cs import CodeShield

async def scan_llm_output(llm_output_code):
    result = await CodeShield.scan_code(llm_output_code)
    if result.is_insecure:
        if result.recommended_treatment == "block":
            llm_output_code = "*** Code Security issues found, blocking the code ***"
        if result.recommended_treatment == "warn":
            llm_output_code = (
                llm_output_code
                + "*** Warning: The generated snippit contains insecure code ***"
            )
    summary = "Security issue detected" if result.is_insecure else "No issues found"
```

3. Add the safeguard models

   For moderation and prompt-attack detection, download the Llama Guard and Prompt Guard models from Hugging Face and run them around your LLM calls. See the project's Hugging Face page and the CodeShield usage notebook for full examples.

## When to use it

- Moderate user inputs and model outputs against a hazards taxonomy with Llama Guard before content reaches end users
- Detect prompt injection and jailbreak attempts with Prompt Guard in an LLM-powered application
- Filter insecure code suggestions from a coding assistant or code interpreter using Code Shield
- Benchmark an LLM's cybersecurity risk with CyberSecEval before deploying it
