AI Hints
Home›Fine-Tuning LLMs Tutorial›Data: Balance, Safety and Practice
Fine-Tuning LLMs · Lesson 80 of 120

Bias and Safety

What the model should not learn.

⏱ 2 min read▶ Video at 2:01:39🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

A model learns everything in its data. The good patterns, and the bad ones. If the data contains private details, or unfair patterns, the model can learn those too.

  • It learns everything
  • Good and bad patterns
  • Private details, unfair patterns

Three risks in data

There are three risks to watch. Private data, like email addresses, or card numbers, which a model can memorise, and repeat. Bias, where the data treats some groups of people unfairly. And harmful content, like rude replies, which the model will copy.

Private dataemails, card numbers
Biasunfair to some groups
Harmful contentrude or unsafe replies

Scanning for private data

Here's a simple privacy scan. A regular expression is a pattern for matching text. One pattern matches email addresses. The other matches long runs of digits, like card numbers. First we scan our training set. Then a new batch of messages, before we add them. Run it. Our training set is clean. But two new messages contain private details. We'd remove those details, before training.

app.pypython
import re
from datasets import load_dataset

EMAIL = re.compile(r"\S+@\S+")
NUMBER = re.compile(r"\d{8,}")              # card or phone numbers
def private(text):
    return bool(EMAIL.search(text) or NUMBER.search(text))

data = load_dataset("json", data_files="tickets_train.jsonl")["train"]
print("Training set flagged:", len(data.filter(lambda r: private(r["message"]))))

new_batch = ["Where is my order? Email me at sam.jones@example.com",
             "Is Dune in stock?",
             "Refund to card 4929123456781234 please"]
for message in new_batch:
    print("FLAG" if private(message) else " ok ", message)
Output
Training set flagged: 0
FLAG Where is my order? Email me at sam.jones@example.com
 ok  Is Dune in stock?
FLAG Refund to card 4929123456781234 please

Good habits

A few good habits. Remove private details before training, or replace them with placeholders. Read a random sample of your data, with your own eyes. Check that every group of customers gets the same quality of answer. And keep a short note, describing what the data contains.

keyvalue
Privateremove or replace
Reada random sample
Fairnesssame quality for all
Documentwhat the data contains

Summary

Let's recap. Models learn the bad patterns too. Watch for private data, bias, and harmful content. Scan with simple patterns. And always read a sample yourself. Next, data practice.

Key takeaways
  • Private data
  • Bias
  • Harmful content
Next up: Data Practice — check a dataset yourself