YaqeenDocs
Back to Yaqeen

How-to guide

Mask personal data before a model call

Install the PII detector, use it as a library or a service, and add a name model.

The PII detector swaps phone numbers, IBANs, cards, emails and national IDs for placeholders before text reaches a language model, and puts the real values back in the reply. It is a Python package and an HTTP service. Yaqeen's own assistant already uses it on every model call.

Set it up step by step#

The package is not on PyPI yet. Ask your Yaqeen contact for the package file.

  1. Install. pip install <package file>. Once it is published, pip install yaqeen-pii.
  2. Try it on the command line.

    echo "Call +966500000001 or write someone@example.invalid" | yqn-pii redact

    The output has [PHONE_1] and [EMAIL_1] in place of the values.

  3. Use it in code, in front of any model call.

    python
    from yaqeen_pii import Redactor
    
    r = Redactor()  # one per reply
    safe = r.text(user_message)  # send this to the model
    reply = r.restore(model_reply)  # real values back, only here

    Redact tool results and retrieved documents the same way, not only the first prompt. For tool calls the model writes, use r.restore_data(arguments).

  4. Or run it as a service for code in another language:

    export YQN_PII_TOKEN=<a long random value>
    yqn-pii serve --port 8080
    curl -H "Authorization: Bearer $YQN_PII_TOKEN" -H "Content-Type: application/json" \
         -d '{"text": "Call +966500000001"}' http://127.0.0.1:8080/v1/redact

    Keep request_id from the answer, send the model's reply to /v1/restore with it within 300 seconds, and run one worker.

  5. Add the name model if names matter. Patterns cannot find a name typed in a sentence.

    pip install "<package file>[ner]"
    python
    from yaqeen_pii import Redactor
    from yaqeen_pii.ner import name_detector
    
    r = Redactor(detectors=(name_detector("<token classification model id from the Hub>"),))

    The first run downloads the model from Hugging Face, so it needs network access once and some memory. Read the model card for its label names and pass them with labels= if they do not contain NAME or PER. Run it on a few hundred of your own messages and count what it misses before you trust it.

  6. Inside Yaqeen the patterns are always on, and the name model is one setting. Every assistant model call passes through the detector. To add names to your assistant, give your Yaqeen contact the model id you tested.

    If the model cannot load, the assistant refuses the model call. It does not send the text on unmasked. Clearing the setting goes back to patterns only.

Limits#

  • A name inside a sentence is missed unless you add a name model. Masking lowers exposure. It does not make data anonymous.
  • Placeholders still tell a model that a phone number was there.
  • It does not decide whether sending data abroad is lawful. Under the Saudi PDPL, sending personal data to a provider outside the Kingdom can count as a transfer. Ask counsel what masking changes for you.

Up Down to moveEnter to openEsc to close