[Multi-Agent Privacy] Detection tools implementation#335
Conversation
(cherry picked from commit 794e23e)
(cherry picked from commit d8503b1)
(cherry picked from commit 11205df)
(cherry picked from commit ec9126f)
(cherry picked from commit 9797115)
(cherry picked from commit 8be4dc8)
(cherry picked from commit 6a91ebb)
(cherry picked from commit 3eae01f)
(cherry picked from commit df24d81)
(cherry picked from commit 825ff24)
(cherry picked from commit 53e384b)
(cherry picked from commit ec8001e)
…lways be ignored (cherry picked from commit 46bc5ab)
(cherry picked from commit fd49131)
(cherry picked from commit 4c34eab)
(cherry picked from commit 1d9c58e)
(cherry picked from commit 9e0b74d)
(cherry picked from commit e5723a5)
(cherry picked from commit 52f4e8c)
(cherry picked from commit 8f16202)
251c48f to
627cf02
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces a new mmore.privacy.detection toolkit providing multiple interchangeable PII detection engines (GLiNER, Presidio, OpenAI privacy-filter, and a DSPy-based LLM engine) and a shared process-wide model registry to lazily load and reuse heavy models across agents.
Changes:
- Added four detection engines with a shared
DetectionConfigschema and tool registry registration. - Implemented a shared
ModelRegistrycache with optional LRU eviction based on device/host memory. - Added unit tests for the engines and the model registry; updated config loading to support Enum casting.
Reviewed changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/test_model_registry.py | Adds unit tests validating caching, eviction, and clear behavior of the shared model registry. |
| tests/test_detection.py | Adds unit tests for all four detection engines and tool registration, using mocks to avoid model downloads. |
| src/mmore/utils.py | Updates config loading to cast Enum-typed fields during dacite parsing. |
| src/mmore/privacy/dspy_llm.py | Adds DSPy LM builder, including a local HF-backed BaseLM using the shared model registry. |
| src/mmore/privacy/detection/presidio_engine.py | Adds Presidio-based detection engine with custom clinical recognizers and shared analyzer caching. |
| src/mmore/privacy/detection/openai_filter_engine.py | Adds HuggingFace openai/privacy-filter engine with shared pipeline caching. |
| src/mmore/privacy/detection/llm_engine.py | Adds DSPy structured-output LLM detection engine with demos and span post-processing. |
| src/mmore/privacy/detection/gliner_engine.py | Adds GLiNER-based detection engine with shared model caching. |
| src/mmore/privacy/detection/defaults.py | Introduces shared defaults (models, labels, thresholds, and Presidio clinical patterns). |
| src/mmore/privacy/detection/config.py | Adds DetectionConfig dataclass schema for detection configuration. |
| src/mmore/privacy/detection/base.py | Adds the detection interface (DetectionEngine) and core types (PIISpan, DetectionEngineType). |
| src/mmore/privacy/detection/init.py | Exposes detection engines and types via package exports. |
| src/mmore/privacy/_cache.py | Implements the shared MODEL_REGISTRY with optional memory-budget LRU eviction. |
| pyproject.toml | Adds new privacy dependencies and introduces a separate privacy-openai-filter extra with declared conflicts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| logger.warning( | ||
| "spaCy model %r not found, downloading it...", | ||
| model_name, | ||
| ) | ||
| from spacy.cli.download import download | ||
|
|
||
| download(model_name) | ||
| importlib.invalidate_caches() |
There was a problem hiding this comment.
but then it should fail as early as possible in the pipeline
There was a problem hiding this comment.
Personally I'd prefer not having to bother the user to manually install the model. In any case "offline/locked-down environments" as copilot says is not realistic for the privacy pipeline as the end goal is to call an API with the sanitized context for the external model
We already do the same for the embedding models download in the Indexing pipeline
Furthermore it would be quite inconvenient because it also means changing the docker images to download the model as a separate step
This is why I think the best is to leave it as-is with the automatic download in the very first run, and this way the privacy pipeline remains self-contained
fabnemEPFL
left a comment
There was a problem hiding this comment.
please address Copilot's latest remarks
Summary
Related issue: #292
Depending on: #285
Review completed in a fork: link
Target branch:
swiss-ai:mmore/v2This PR adds a new Personally Identifiable Information (PII) detection toolkit to be later used as tools by the agentic privacy system.
What this adds
mmore.privacy.detectionwith four interchangeable engines:DetectionConfig, and registers itself in a tool registry so agents can call itMMORE_PRIVACY_MODEL_BUDGET_MBMMORE_PRIVACY_MODEL_CACHE=0Dependencies / CI
privacynow has new dependencies (gliner, presidio, spacy, dspy, and psutils for memory measurements)privacy-openai-filter(transformers>=5,peft) as currently there is a conflict withmarker-pdffrom the extraprocess(will be solved once #191 is closed)Tests
Disclaimer: the big numbers in the line differences most come from new dependencies, hence changes in the
uv.lockfileDemo
Input note (AI generated)
GLiNER (
nvidia/gliner-PII)15 spans at confidence_threshold = 0.4
Bobby3/2Linda617-555-0148AustinSt. Mary's4/23/6504/23/195512345678BCXY 99-88-77jsmith@hosp-derm.orgxxx-xx-4321LindaMaria Garcia4/1openai/privacy-filter
78 spans at confidence_threshold = 0.4
BobbyDr.GarciaLinda617-555-0148LinwoodAveDr.R.Lee4/23/6504/23/195512345678BCXY99-88-77Jan5558675309AB1234567jsmith@hosp-derm.orgxxx-xx-4321LindaDr.MariaGarciapager12345Presidio + custom clinical recognizers
26 spans at confidence_threshold = 0.4
BobbyTower 3GarciaLinda617-555-0148Austinwks agoR. Lee'sSt. Mary's4/23/6504/23/195504/23/1955123456781245-6788Jan.555 867 5309AB1234567AB1234567jsmith@hosp-derm.orghosp-derm.orglast weekVALindaMaria Garcia12345c. Tentative d/LLM
Qwen/Qwen2.5-7B-Instructvia DSPy21 spans at confidence_threshold = 0.4
Bobby3/2Dr. GarciaLinda617-555-0148metoprolol2 wksDr. R. LeeSt. Mary's123 Main123456781245-6788BCXY 99-88-77555 867 5309jsmith@hosp-derm.orgVAxxx-xx-4321Dr. Maria Garciapager 123454/1work