Skip to main content

Mask sensitive data in documents using a local OpenAI-compatible LLM

Project description

llm-mask

License: MIT Tests

A Python library that masks sensitive data in documents (PII, tokens, URLs, company names, etc.) using a local OpenAI-compatible LLM, and restores the original text via a saved mapping — no data leaves your infrastructure.

Installation

pip install llm-mask

Requirements

A running local LLM server with an OpenAI-compatible API, e.g. vLLM, LM Studio, or Ollama.

Quick start

from llm_mask import MaskingClient

client = MaskingClient(
    base_url="http://localhost:8001/v1",   # your LLM server
    model="local-model",
    language="ru",   # "ru" or "en"
)

text = "Привет, меня зовут Иван, работаю в Apple. Email: ivan@apple.com"

# ── mask ──────────────────────────────────────────────────────────────
masked_text, mapping = client.mask(text)
# masked_text → "Привет, меня зовут <person_1>, работаю в <company_1>. Email: <email_1>"
# mapping     → {"Иван": "<person_1>", "Apple": "<company_1>", "ivan@apple.com": "<email_1>"}

# ── unmask (no LLM call) ───────────────────────────────────────────────
original = client.unmask(masked_text, mapping)
# original → original text restored exactly

Attribute-style access also works:

result = client.mask(text)
result.masked_text
result.mapping

File & directory helpers

# Mask a file (nothing written to disk by default)
result = client.mask_file("document.md")

# Write masked file + mapping JSON to disk
result = client.mask_file(
    "document.md",
    save_masked=True,          # → document_masked.md
    save_mapping=True,         # → document_mapping.json
    mapping_dir="./mappings",
)

# Restore from files (no LLM)
original = client.unmask_file("document_masked.md", "document_mapping.json")

# Mask a whole directory
results = client.mask_directory(
    "./docs",
    pattern="*.md",
    overwrite_originals=False,          # writes *_masked.md next to originals
    mapping_store_path="./mappings.json",
)

Configuration

Parameter Default Description
base_url http://localhost:8001/v1 LLM server base URL
model local-model Model identifier
api_key EMPTY API key (ignored by most local servers)
language ru Built-in prompt language: "ru" or "en"
chunk_size 6000 Max characters per LLM call
temperature 0.0 Sampling temperature
judge_model None Optional second LLM pass to catch missed entities

Entity types

Entity Placeholder
URLs / domains <url_1>
Service names service_1
Company / brand names <company_1>
Person names / usernames <person_1>
Email addresses <email_1>
Phone numbers <phone_1>
IP addresses <ip_1>
Tokens / secrets / keys <secret_1>
Numeric IDs <id_1>
File paths <path_1>
Project / code names project_1
Infrastructure names <env_1>, <host_1>

Mapping file format

{
  "source_file": "document.md",
  "masked_at": "2026-03-05T14:22:00Z",
  "mapping": {
    "Apple": "<company_1>",
    "https://api.company.com": "<url_1>"
  }
}

Development

git clone https://github.com/KodakV/llm-mask
cd llm-mask
pip install -e ".[dev]"
pytest

See CONTRIBUTING.md for contribution guidelines.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_mask-0.1.0.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_mask-0.1.0-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file llm_mask-0.1.0.tar.gz.

File metadata

  • Download URL: llm_mask-0.1.0.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for llm_mask-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a296f5456da2341e8fa9cf762baa9522b445838660e6b1e78c867f4ac6dfc394
MD5 3955874f0194934472b5f8a1fffa5380
BLAKE2b-256 444d4f7d1eb059ffd8a1d59667b066c3880b06161c5e3909b61dc82c921dd7e7

See more details on using hashes here.

File details

Details for the file llm_mask-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: llm_mask-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for llm_mask-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6d491cdc14a3174652c167a0a0ad923f7b605ea1635e272cd241e70b859895f
MD5 97300de0fc9b3dcd1983f49885b55c98
BLAKE2b-256 7859c376c824d2824610419122da052f911a4194a108e7aa798aee423b420d85

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page