liveBook · Manning

Safety and compliance filter

As explained in Grokking AI Algorithms, Second Edition

A safety and compliance filter is a data-filtering process that removes harmful, offensive, or legally restricted content before model training.

A safety and compliance filter removes harmful, offensive, or legally restricted content before model training. It targets profanity, slurs, toxicity, and personally identifiable information such as phone numbers and credit card numbers. section 11.7

Filtering protects training quality and users by reducing:

  • Obscene or offensive language that could create reputational risk or a poor user experience. section 11.7
  • Toxic content, including highly opinionated views that may be harmful. section 11.7
  • Personally identifiable information that a model might reproduce and that may create privacy or legal risks. section 11.7
Table 11.2 A summary of areas important for data quality

Metric

Why it matters

Goals

Duplicate rate

It prevents memorization and reduces dataset size.

< 5% exact-paragraph matches

Average sentence length

Very short sentences lack context, and very long sentences overflow the model window.

5–40 tokens per segmented sentence

Toxicity rate

It keeps generation safe and inoffensive.

< 0.3% words flagged for toxicity

Profanity ratio

It provides a more precise count than toxicity rate.

Depends on the use case; can be < 0.2% for general models but can be higher for “edgy” domains

Language purity

Mixed languages confuse the model and add unnecessary vocabulary.

> 98% of the content in the desired language

PII leaks

It reduces privacy and legal risks.

0 emails, 0 phone numbers, 0 ID numbers

Non-UTF8 character rate

Binary junk characters can cause the tokenizer (explored next) to crash.

0 control characters (realistically, < 0.01% is good enough)

Forbidden phrases

It prevents generation of phrases you don’t want to see.

0 occurrences of forbidden phrases

Markup density

Markup like HTML and markdown noise wastes the context window and makes tokenizing less accurate.

< 2% lines containing code characters such as <, [, ^, >, ], +, > and links

Readability score

It makes the LLM generate text that’s easy to read (written for a certain reading level).

The Flesch-Kincaid Grade-Level readability score (F-K score) is a formula to determine ease of readability. A score of 6 means 11-year-olds can read it easily; a score of 12 means 17-year-olds can read it easily.

Domain balance

If you curate multiple domains (medical, fiction, geography, and so on), you want to represent them as intended.

No more than 10% variance per domain. If you planned 20% medical content, for example, your actual data shouldn’t be less than 10% or more than 30%.

Use the data-quality summary to compare toxicity, profanity, and PII-leak metrics with their stated goals. section 11.7

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Defines target content and explains practical risks, but does not detail detection algorithms or filtering implementation.
Examples
Uses profanity, slurs, toxic views, phone numbers, credit card numbers, and other personally identifiable information as filtering targets.
What you can do after
Apply it by removing harmful language and personal data before training a language model.
How it connects
Connects safety filtering with data preprocessing, data quality, toxicity, profanity, and personally identifiable information.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.