-
Notifications
You must be signed in to change notification settings - Fork 143
feat: add Presidio integration page #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
95f330c
c0ccdda
07be7a8
4ec0a1b
7225d09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| --- | ||
| layout: integration | ||
| name: Presidio | ||
| description: PII detection and anonymization for Haystack Documents and text strings, powered by Microsoft Presidio. | ||
| authors: | ||
| - name: deepset | ||
| socials: | ||
| github: deepset-ai | ||
| twitter: deepset_ai | ||
| linkedin: https://www.linkedin.com/company/deepset-ai/ | ||
| - name: Shahmeer Ali | ||
| socials: | ||
| github: SyedShahmeerAli12 | ||
| pypi: https://pypi.org/project/presidio-haystack/ | ||
| repo: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/presidio | ||
| type: Custom Component | ||
| report_issue: https://github.com/deepset-ai/haystack-core-integrations/issues | ||
| logo: /logos/microsoft.png | ||
| version: Haystack 2.0 | ||
| toc: true | ||
| --- | ||
|
|
||
| ### Table of Contents | ||
|
|
||
| - [Overview](#overview) | ||
| - [Installation](#installation) | ||
| - [Usage](#usage) | ||
| - [Document Cleaning](#document-cleaning) | ||
| - [Text Cleaning](#text-cleaning) | ||
| - [Entity Extraction](#entity-extraction) | ||
| - [License](#license) | ||
|
|
||
| ## Overview | ||
|
|
||
| [Microsoft Presidio](https://microsoft.github.io/presidio/) is an open-source library for PII detection and anonymization using NLP-based entity recognition. | ||
|
|
||
| `presidio-haystack` provides three Haystack components: | ||
|
|
||
| | Component | Input | Purpose | | ||
| |-----------|-------|---------| | ||
| | `PresidioDocumentCleaner` | `list[Document]` | Replace PII in document text with entity type placeholders | | ||
| | `PresidioTextCleaner` | `list[str]` | Replace PII in plain strings — useful for sanitizing user queries | | ||
| | `PresidioEntityExtractor` | `list[Document]` | Detect PII and store entities as structured document metadata | | ||
|
|
||
| All components run locally — no external API required. Presidio uses spaCy NLP models under the hood. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install presidio-haystack | ||
| python -m spacy download en_core_web_lg | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also document how to download the model in the application itself and link the available spacy models? For those who have never used spacy, it might be also worth describing how to set it up for other languages. Out of curiosity - are we able to use small and large model in the same application? It seems each component accepts a language, but not the model name.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also changed this slightly in this PR deepset-ai/haystack-core-integrations#3209 where we added a default mappings of languages to models as a ClassVar
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, good questions! I've updated the Installation section to cover all of this. Added a note On your question about sm vs lg each language value maps to one spaCy model at a time,
kacperlukawski marked this conversation as resolved.
Outdated
|
||
| ``` | ||
|
|
||
| `en_core_web_lg` is the recommended English model for best accuracy. For a lighter footprint, `en_core_web_sm` works too — see the [full list of spaCy models](https://spacy.io/models/en) for options. | ||
|
|
||
| Each component accepts a `language` parameter (default `"en"`). To use a non-English language, download the corresponding spaCy model and pass the language code: | ||
|
|
||
| ```bash | ||
| # Example: Spanish | ||
| python -m spacy download es_core_news_md | ||
| ``` | ||
|
|
||
| ```python | ||
| cleaner = PresidioDocumentCleaner(language="es") | ||
| ``` | ||
|
|
||
| Note: each `language` value maps to one spaCy model at a time. You cannot select between `sm` and `lg` per component — whichever model is registered for that language in your environment will be used. | ||
|
kacperlukawski marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Usage | ||
|
|
||
| ### Document Cleaning | ||
|
|
||
| Replace PII in document content before indexing: | ||
|
|
||
| ```python | ||
| from haystack import Document | ||
| from haystack_integrations.components.preprocessors.presidio import PresidioDocumentCleaner | ||
|
|
||
| cleaner = PresidioDocumentCleaner() | ||
| result = cleaner.run(documents=[ | ||
| Document(content="Contact Alice Smith at alice@example.com or 212-555-1234.") | ||
| ]) | ||
| print(result["documents"][0].content) | ||
| # Contact <PERSON> at <EMAIL_ADDRESS> or <PHONE_NUMBER>. | ||
| ``` | ||
|
|
||
| Original documents are not mutated. Documents with no text content pass through unchanged. | ||
|
|
||
| ### Text Cleaning | ||
|
|
||
| Sanitize user queries before they reach your LLM: | ||
|
|
||
| ```python | ||
| from haystack_integrations.components.preprocessors.presidio import PresidioTextCleaner | ||
|
|
||
| cleaner = PresidioTextCleaner() | ||
| result = cleaner.run(texts=["My name is John Doe, my SSN is 123-45-6789"]) | ||
| print(result["texts"][0]) | ||
| # My name is <PERSON>, my SSN is <US_SSN> | ||
| ``` | ||
|
|
||
| ### Entity Extraction | ||
|
|
||
| Detect PII and attach it as structured metadata without modifying the document text: | ||
|
|
||
| ```python | ||
| from haystack import Document | ||
| from haystack_integrations.components.extractors.presidio import PresidioEntityExtractor | ||
|
|
||
| extractor = PresidioEntityExtractor() | ||
| result = extractor.run(documents=[ | ||
| Document(content="Contact Alice at alice@example.com") | ||
| ]) | ||
| print(result["documents"][0].meta["entities"]) | ||
| # [{"entity_type": "PERSON", "start": 8, "end": 13, "score": 0.85}, | ||
| # {"entity_type": "EMAIL_ADDRESS", "start": 17, "end": 34, "score": 1.0}] | ||
| ``` | ||
|
|
||
| All three components accept `language`, `entities`, and `score_threshold` parameters at init time. See [Presidio supported entities](https://microsoft.github.io/presidio/supported_entities/) for the full list of detectable PII types. | ||
|
|
||
| ## License | ||
|
|
||
| `presidio-haystack` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SyedShahmeerAli12 Don't you want to add yourself as an author?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review .. I've addressed all the feedback ....
time whichever is registered in the environment is used, so you can't pick between sm and lg per component