In this tutorial, you’ll learn how to create a robust PII (Personally Identifiable Information) data extraction and scrubbing system using BAML and GPT-4. By the end, you’ll have a working system that can identify, extract, and scrub various types of PII from text documents.
First, let’s define what our PII data structure should look like. Create a new file called pii_extractor.baml and add the following schema:
This schema defines:
PIIData: A class representing a single piece of PII with its type and valuePIIExtraction: A container class that holds an array of PII data items and a sensitive data flagNext, let’s create the function that uses GPT-4 to extract PII. Add this to your pii_extractor.baml file:
Let’s break down what this function does:
document input as a stringgpt-5-mini modelPIIExtraction object containing all found PII dataTo ensure our PII extractor works correctly, let’s add some test cases:
This is what it looks like in BAML playground after running the test:
You can try playing with the functions and tests online at https://www.promptfiddle.com/Pii-data-O4PmJ
Now you can use the PII extractor to both identify and scrub sensitive information from your documents:
This implementation provides several key features:
Example output:
Now that you have a working PII extractor, you can:
For organizations handling sensitive data, using cloud-based LLMs like OpenAI’s GPT models might not be suitable due to data privacy concerns. BAML supports using local models, which keeps all PII processing within your infrastructure.
In this example, we’re going to use a Ollama model. For more details on how to use Ollama with BAML, check out this page.
pii_extractor.baml: