Skip to main content

preempt

Prϵϵmpt is a security framework designed to protect personally identifiable information (PII) in text by applying encryption or other privacy-preserving techniques before that data is sent to third-party large language model (LLM) APIs.

Prϵϵmpt achieves high utility for a diverse range of tasks while maintaining cryptographic privacy guarantees. For the experiments and results found in Prϵϵmpt: Sanitizing Sensitive Prompts for LLMs, please refer to this repo.

This is a modular version of Prϵϵmpt, meant to be used as part of other projects.

Setup

  1. Install uv following the instructions here.
  2. Create a virtual environment with Python 3.10, activate it and add preempt:
uv venv --python 3.10
. ./.venv/bin/activate
uv init
uv add preempt
  1. Import Prϵϵmpt methods and classes with from preempt.utils import * to use in your code. See the usage examples below and in demo.ipynb.

If you would like to work with the repo, then clone the repo, navigate to the base folder (preempt) and use the following:

uv venv --python 3.10
. ./.venv/bin/activate
uv sync

If you already have a project in which you would like to use Prϵϵmpt, use either pip install preempt or uv add preempt, depending on the set up.

Usage

Additional usage examples can be found in demo.ipynb.

We will add support for generalized NER and sanitization in the near future.

Complete Usage Example

This is a complete usage example where we sanitize names and currency values. Make sure you either have Universal NER or Llama-3 8B Instruct available.

  1. Import NER and Sanitizer objects:
from preempt.ner import *
from preempt.sanitizer import *
  1. Initialize a NER and Sanitizer object:
device = (
    "mps" if torch.backends.mps.is_available() 
    else "cuda" if torch.cuda.is_available() 
    else "cpu"
)
# Load NER object
# ner_model = NER("/path/to/UniNER-7B-all", device=device)
ner_model = NER("/path/to/Meta-Llama-3-8B-Instruct/", device=device)

# Load Sanitizer object
sanitizer_name = Sanitizer(ner_model, key = "EF4359D8D580AA4F7F036D6F04FC6A94", tweak = "D8E7920AFA330A73")
sanitizer_money = Sanitizer(ner_model, key = "FF4359D8D580AA4F7F036D6F04FC6A94", tweak = "E8E7920AFA330A73")

# Sentences
sentences = ["Ben Parker and John Doe went to the bank and withdrew $200.", "Adam won $20 in the lottery."]
  1. Sanitize names in sentences:
# Sanitizing names
sanitized_sentences, _ = sanitizer_name.encrypt(sentences, entity='Name', epsilon=1)
print("Sanitized sentences:")
print(sanitized_sentences)
"""
Prints:

Sanitized sentences:
['Jay Francois and Lamine Franklin went to the bank and withdrew $200.', 'Elie Vinod won $20 in the lottery.']
"""
  1. Sanitize currency values in sanitized_sentences:
# Sanitizing currency values
sanitized_sentences, _ = sanitizer_money.encrypt(sanitized_sentences, entity='Money', epsilon=1)
print("Sanitized sentences:")
print(sanitized_sentences)
"""
Prints:

Sanitized sentences:
['Jay Francois and Lamine Franklin went to the bank and withdrew $769451698.', 'Elie Vinod won $37083668 in the lottery.']
"""
  1. Desanitize encrypted names in sanitized_sentences:
# Desanitizing names
desanitized_sentences = sanitizer_name.decrypt(sanitized_sentences, entity='Name', use_cache=True)
print("Desanitized sentences:")
print(desanitized_sentences)
"""
Prints:

Desanitized sentences:
['Ben Parker and John Doe went to the bank and withdrew $769451698.', 'Adam won $37083668 in the lottery.']
"""
  1. Desanitize encrypted currency values in desanitized_sentences:
# Desanitizing currency values
desanitized_sentences = sanitizer_money.decrypt(desanitized_sentences, entity='Money', use_cache=True)
print("Desanitized sentences:")
print(desanitized_sentences)
"""
Prints:

Desanitized sentences:
['Ben Parker and John Doe went to the bank and withdrew $200.', 'Adam won $20 in the lottery.']
"""

Extraction

We currently support Universal NER and Llama-3 8B Instruct for NER. We will add support for including your own NER models in the near future.

Initialize a NER class object by passing the path to one of the supported NER models mentioned above:

device = (
    "mps" if torch.backends.mps.is_available() 
    else "cuda" if torch.cuda.is_available() 
    else "cpu"
)
ner_model = NER("/path/to/Meta-Llama-3-8B-Instruct/", device=device)

Extract PII values found in a list of target strings using ner_model.extract():

sentences = ["Ben Parker and John Doe went to the bank.", "Who was late today? Adam."]
extracted = ner_model.extract(sentences, entity_type='{Name/Money/Age}')

Sanitization

We currently only support sanitization for names, currency values and age, using either FPE or m-LDP.

Initialize a Sanitizer class object by passing the previously initialized ner_model, a key and tweak parameter (required for the FF3 cipher used for FPE).

sanitizer = Sanitizer(ner_model, key = "EF4359D8D580AA4F7F036D6F04FC6A94", tweak = "D8E7920AFA330A73")

Sanitize a list of target strings using sanitizer.encrypt():

sanitized_sentences, _ = sanitizer.encrypt(sentences, entity='Name', epsilon=1, use_fpe=True, use_mdp=False)

PII values found during NER are stored under sanitizer.new_entities as a nested list.

The mappings between plain text and cipher text PII values are stored under sanitizer.entity_mapping. FPE will typically extract PII values from the sanitized sentences before decryption.

Sanitized sentences can be desanitized using sanitizer.decrypt():

desanitized_sentences = sanitizer.decrypt(sanitized_sentences, entity='Name')

If your NER model can't reliably pick up sanitized attributes, consider setting use_cache=True, to decrypt using stored NER values.

desanitized_sentences = sanitizer.decrypt(sanitized_sentences, entity='Name', use_cache=True)

Sanitizing multiple PII attributes

If you want to sanitize multiple sensitive attributes, create a sanitizer for each category separately.

For more examples, check out demo.ipynb

Usage tips

NER typically works better when the inputs are smaller. Consider breaking a large chunk of text into smaller sentences when using the sanitizer.

Metadata

Release files for preempt 0.1.27

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for preempt 0.1.27
File Size Uploaded Provenance Actions
preempt-0.1.27.tar.gz 89.9 kB Without provenance Details for preempt-0.1.27.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for preempt 0.1.27
File Interpreter ABI Platform Actions
preempt-0.1.27-py3-none-any.whl Python 3 none any Details for preempt-0.1.27-py3-none-any.whl

Total release size: 140.1 kB

Release files / preempt-0.1.27.tar.gz

Download URL preempt-0.1.27.tar.gz
Size 89.9 kB
Tags Source
SHA-256 checksum
How to use checksums
bbc8b40d8f9aa6728c2bf6e037261bcaf08077af0792364ed21c50a684e4ade7
BLAKE2b-256 checksum
How to use checksums
871425490d93780147cf38612c660dbd92ae89b5acb6133ebd8b36ca169f590e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.4

Release files / preempt-0.1.27-py3-none-any.whl

Download URL preempt-0.1.27-py3-none-any.whl
Size 50.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ac2c9b6da786b446c8109078276f586844e839b6e9f09113c0d1f9f62b8ff8e
BLAKE2b-256 checksum
How to use checksums
5e5f6f972ff2395a10cdf1f924c7e8b7e46ffb3ba3779edc7497fd9982ac8423
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.4

Security for preempt 0.1.27

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page