Zum Inhalt springen

Lektion 21 · Serving and Safety

Safety: Llama Guard and Prompt Guard

Add layered safety around Llama with Llama Guard content classification, Prompt Guard injection detection, output checks, permissions and human review.

A chat model can be pushed into unsafe or wrong behavior. No single filter solves this. A safer system uses layers: checks on input, a well-designed prompt, checks on output, limited permissions and people for the high-risk cases.

Two Meta safety models

Llama Guard is a safety classifier. Llama Guard 4 is a 12B model that accepts text and images, and can check user prompts as well as model replies. According to its model card, it answers with a text verdict: safe, or unsafe plus the codes of the violated categories. The categories follow the MLCommons hazard taxonomy, with codes S1 to S14, for example violent crimes (S1), specialized advice (S6), privacy (S7), indiscriminate weapons (S9) and hate (S10). S14, code interpreter abuse, is meant for text-only tool-use cases.

Prompt Guard 2 looks for prompt injections and jailbreaks. Its model card says it labels text benign or malicious, and flags explicit attempts to override instructions, even when the request would otherwise be harmless. It has a window of 512 tokens, and the card advises splitting longer input into segments. It comes in 86M and 22M sizes, and the card says the 22M model is weaker on languages other than English.

Both models have their own licenses and access conditions. Llama Guard 4 is a large model with its own memory needs. Read the model cards before use.

Where the layers go

flowchart LR
  U["User input"] --> PG["Injection check"]
  PG --> G1["Content check"]
  G1 --> M["Llama chat model"]
  M --> G2["Output check"]
  G2 --> A["Answer or block"]

Each layer can block, flag for review or let the request pass.

Retrieved documents and tool results should go through injection checks as well, because attackers can hide instructions in them.

Handling a verdict

Parse the classifier reply strictly, and decide what happens when it is unclear. If a safety check fails or returns something odd, a high-risk system should usually block or escalate. This is called failing closed.

Python Stand-in: parse a guard-style verdict

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The example policy is yours to design. It is not part of any model. Check the exact output format on the model card for the version you use.

Scanning long input with a short window

A detector with a short window misses text beyond its limit. Overlapping segments help to catch phrases that cross a boundary.

Python Stand-in: scan long text in overlapping windows

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Checking only the first window misses the attack. (Real windows are counted in tokens, not words.) A keyword detector is easy to evade, which is why trained classifiers exist. Trained ones have limits too, and the Prompt Guard card warns about adaptive attacks.

Measure your filters

Build a test set with harmful, harmless and borderline examples from your domain and languages. Count:

  • False negatives: unsafe content that passed.
  • False positives: safe content that was blocked.

Choose thresholds and policies from these counts. A filter that blocks every medical question may fail your users as badly as a weak one.

Other defenses that matter

  • Least privilege: the model can only do what its tools allow.
  • Human approval for high-impact actions.
  • Output handling: escape HTML, and never run generated code outside a sandbox.
  • Data protection: do not give the model secrets it does not need.
  • User notice: tell people they are talking to an AI, and give a way to report problems.

See prompt injection attacks.

A safety classifier reduces risk; it does not guarantee safety. Never connect powerful tools to untrusted content just because a filter said "safe".

Designing the policy around a classifier

A classifier gives a label. Your policy decides what happens next. For each category, decide whether to block, to show a safer reply, to ask for human review or to log and allow. Also decide what the user sees. A short, respectful message that explains that the request cannot be handled, and where to get help, is better than a silent failure. For sensitive topics such as self-harm, work with qualified people on the response.

Your application may need rules that no general model covers, such as "never discuss other customers" or "never promise a refund". Enforce important business rules in code or tool permissions, not only in a prompt or a classifier.

Keep testing after launch

Attackers change their wording, and your product changes too. Review blocked and allowed samples regularly, within your privacy rules. Add new attacks and false alarms to your test set. When you update a guard model, a chat model or a threshold, run the full set again and compare the counts.

Where classifiers can fail

Expect mistakes in predictable places. Short or ambiguous messages lack context. Quotations, fiction and news can mention harmful things without asking for help with them. Mixed languages, unusual spelling and encoded text may slip through. A long conversation can become unsafe step by step even when each message looks mild, so consider checking recent context together and not only the latest line.

Also plan for the failure of the check itself. The classifier service can be slow or down. Decide in advance whether your application waits, refuses or continues with extra limits, and make that choice according to the risk of each feature. Log how often the checks fail, so a silent outage cannot remove your protection without anyone noticing.

Be honest about protection. In your documentation, describe what each layer checks and what it does not.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What is Llama Guard mainly for?

    It is a safety classifier placed around a model.
  2. 2. Why split long text for Prompt Guard 2?

    Text beyond the window would not be checked.
  3. 3. A guard model returns unexpected text in a high-risk flow. What is a cautious choice?

    Failing closed avoids treating errors as approval.

Summary

  • Use layers: input checks, output checks, permissions and human review.
  • Llama Guard classifies content; Prompt Guard flags injections and jailbreaks.
  • Parse verdicts strictly and measure false positives and negatives.
  • No filter is a guarantee.

Next: Project: A Local Llama Chat Assistant

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.