scikitplot.cleanprompt#

Redact sensitive values from a prompt, then restore them from the reply.

scikitplot.cleanprompt replaces personal data in a text with stable placeholders before that text is sent to a language model, and puts the values back afterwards. The base tier is pure standard library.

Notes

User notes.

from scikitplot.cleanprompt import Redactor, restore

redactor = Redactor()
result = redactor.redact(
    "Mail ada@example.com about the Acme deal", extra_terms=["Acme"]
)
result.text
# 'Mail [EMAIL-1] about the [CUSTOM-1] deal'

# ... send result.text to a model, get a reply ...
restore(reply, result.vault).text

result.text is safe to transmit. result.vault is not: it holds the values that were removed. Keep it on this machine and call vault.clear() when you are finished.

The optional tiers add named-entity detection, a local web interface, and authenticated vault encryption. capabilities reports which are usable:

python -m scikitplot.cleanprompt capabilities

Developer notes — the import contract.

Importing this module imports no third-party package. Not spacy, not flask, not cryptography, not numpy. This submodule can therefore be added to scikitplot without changing the import cost or the failure surface of any other submodule, and without any other submodule needing to know it exists. It imports no sibling scikitplot package either.

Optional-tier names are resolved lazily through PEP 562:

  • __all__ lists base-tier names only. __all__ is the star-import surface, and from … import * resolves every entry; listing an optional name there would defeat the lazy tier at exactly the one operation that touches all of them. The optional names remain reachable by attribute access, which is how a consumer of that tier reaches them anyway.

  • __getattr__ imports the owning module on first access, and raises CapabilityError with an install command when the tier is unavailable — raised before the missing dependency is imported, so the message is actually reachable.

  • __dir__ unions the module globals with the optional names as strings and never resolves them, so tab completion, Sphinx and hasattr stay base-safe.

import scikitplot.cleanprompt, dir(scikitplot.cleanprompt) and from scikitplot.cleanprompt import * all succeed on a base installation. Each is a checked-in regression test, not a claim.

See also

scikitplot.cleanprompt._engine

The pipeline.

scikitplot.cleanprompt._policy

Declarative configuration.

scikitplot.cleanprompt._patterns

The curated pattern library.

!! processed by numpydoc !!

User guide. See the CleanPrompt User Guide section for further details.

API Encode Prompt#

EncodedPrompt

The result of encode: a safe text and the handle to undo it.

Handle

Everything needed to decode a reply.

Session

A conversation: consistent placeholders, and a vault with a lifetime.

decode

Put the original values back into a model's reply.

encode

Replace sensitive values in text with placeholders.

session

Open a Session.

APP Session Store#

SessionStore

Bounded, expiring, server-side store of vaults.

create_app

Build the Flask application.

Capability#

TIERS

CapabilityReport

Outcome of probing one tier.

CapabilityStatus

Seven-state usability vocabulary for an optional dependency tier.

capabilities

Probe every optional tier.

probe

Report whether one optional tier is usable.

require

Return the report for tier, raising when it is not usable.

Custom Domain and Point#

load_custom

Load user definitions from one or more files into a catalog of their own.

with_custom

Return the built-in catalog with custom definitions merged in.

Detector Registry#

Detector

Base class for detectors.

DetectorRegistry

An ordered, immutable-by-convention collection of detectors.

LiteralDetector

Detector for exact strings the caller names.

RegexDetector

Detector backed by one entry of the pattern library.

default_registry

Build a registry from the pattern library, plus optional literal terms.

Diagnosis#

BlindSpot

A category of sensitive information nothing currently detects.

Diagnosis

What a given configuration can and cannot detect.

Suggestion

A candidate the active detectors did not claim.

describe_outcome

Explain a redaction outcome, especially an empty one.

diagnose

Report what a configuration can and cannot detect.

suggest_terms

Offer candidate terms the active detectors did not claim.

Redactor#

Redactor

Reusable, stateless redactor.

reserved_label_spans

Find substrings of text that already match the placeholder grammar.

resolve_spans

Reduce possibly overlapping spans to a disjoint, ascending sequence.

restore

Replace placeholders in text with the values held in vault.

Engines Named-entity#

CANONICAL_LABELS

Built-in immutable sequence.

DEFAULT_ENGINE

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

ENGINES

ENGINE_MODES

Built-in immutable sequence.

EngineSpec

Static description of one named-entity engine.

build_detectors

Construct the detectors for an engine mode.

canonical_label

Map an engine's entity label onto the canonical vocabulary.

describe_engines

Report every engine's readiness, and what mode would select.

resolve_engine

Resolve an engine mode into the engines that will actually run.

trim_entity_span

Shrink an entity span until it carries no unmatched bracket.

Format Spec#

FormatSpec

One validated file format.

Guard Spec#

Guard

Encode what goes to a model, check it, and decode what comes back.

StreamDecoder

Decode a model's reply as it streams in, never splitting a label.

Language Spec#

DEFAULT_LANGUAGE

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

LANGUAGES

MODEL_SIZES

Built-in immutable sequence.

MULTILINGUAL_MODEL

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

LanguageSpec

One language spaCy publishes models for.

installed_models

Return the spaCy models installed here, mapped to their versions.

resolve_model

Resolve a language and size into a spaCy model name.

supported_languages

Return every language code with a dedicated model.

Mcp Server#

McpServer

Answer MCP requests with cleanprompt tools.

Ner Detector#

DEFAULT_ENTITY_LABELS

frozenset() -> empty frozenset object frozenset(iterable) -> frozenset object

DEFAULT_MODEL

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

NerDetector

Detect named entities with a spaCy pipeline.

spacy_detector

Build a NerDetector.

Nltk Detector#

NltkDetector

Detect named entities with NLTK's maximum-entropy chunker.

nltk_detector

Build an NltkDetector.

Pack Pattern Detector#

FieldSpec

A field whose value is sensitive wherever it appears in a record.

PackError

A pack document is invalid.

PackSpec

One validated pack.

Pattern Spec#

PATTERNS

PatternSpec

One named structural pattern.

default_patterns

Return the patterns enabled when a policy does not name kinds.

get_pattern

Return one pattern specification by kind.

luhn_ok

Return whether digits satisfies the Luhn checksum.

Plan Spec#

CleanPlan

Everything a cleaner needs to know, and nothing it discovers.

FluentCleanPrompt

Build a CleanPlan one decision at a time.

Policy Spec#

DEFAULT_LIMITS

Hard bounds on one redaction pass.

DEFAULT_POLICY

Complete, declarative configuration for a redaction pass.

DEFAULT_TAG_STYLE

The placeholder grammar.

Limits

Hard bounds on one redaction pass.

OverlapStrategy

How to arbitrate two detections that share characters.

RedactionPolicy

Complete, declarative configuration for a redaction pass.

TagStyle

The placeholder grammar.

Render Helper#

ANSI

Terminal escape sequences used by highlight_placeholders.

highlight_placeholders

Return a copy of text with placeholders wrapped in colour.

restoration_note

Return a one-line, secret-free description of a restoration.

should_colorize

Decide whether to emit terminal colour.

summary_table

Render a table of what was redacted.

Runtime Helper#

Cleaner

Apply one validated plan to text, files, folders and archives.

Encoded

One encoded input: safe text and a report.

Item

What happened to one file of a folder or archive.

Vault#

Vault

A closed mapping from placeholder label to original secret.

Vault Encryption#

decrypt_mapping

Decrypt a mapping produced by encrypt_mapping.

encrypt_mapping

Encrypt every value of mapping, leaving the labels in the clear.