[Multi-Agent Privacy] Detection tools implementation#1
Conversation
…to feat/detection-toolkit
…ch 'me' into feat/detection-toolkit
be1f766 to
4374156
Compare
0968f9d to
825ff24
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces a PII detection toolkit under mmore.privacy.detection to be later consumed as agent tools by the upcoming multi-agent privacy system. It adds four interchangeable detection engines (GLiNER, OpenAI privacy-filter, Presidio with custom clinical recognizers, and a DSPy-driven LLM engine), all sharing a common DetectionConfig, DetectionEngine interface, and PIISpan output type, with module-level caches for the underlying models/pipelines. Each engine self-registers a default tool function in the global tool_registry so agents can resolve them by name.
Changes:
- Adds
DetectionConfig,DetectionEngine/PIISpanbase, and four engine implementations with lazy + thread-safe model/pipeline caching, plusdspy_llm.build_dspy_lm(with aLocalHFLMfor local HF chat models). - Wires each engine to the agent tool registry via
@register_tooland exposes them throughmmore.privacy.detection.__init__. - Declares new optional
privacyextras (gliner, presidio, spacy, dspy) and a separateprivacy-openai-filterextra (transformers>=5, peft), with conflict declarations againstprocess/all; adds mock-based unit tests covering all four engines.
Reviewed changes
Copilot reviewed 11 out of 12 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pyproject.toml | Adds new dependencies to privacy extra and a new privacy-openai-filter extra with conflict declarations against process/all. |
| src/mmore/privacy/detection/init.py | Public re-exports of engines, config, base types, and registered tool callables. |
| src/mmore/privacy/detection/base.py | Defines PIISpan dataclass and abstract DetectionEngine interface. |
| src/mmore/privacy/detection/config.py | DetectionConfig dataclass schema for the YAML privacy.detection block. |
| src/mmore/privacy/detection/defaults.py | Shared default thresholds, labels, model names, clinical regex patterns, and default LLMConfig. |
| src/mmore/privacy/detection/gliner_engine.py | GLiNER engine with thread-safe model cache and detect_pii_gliner tool. |
| src/mmore/privacy/detection/openai_filter_engine.py | HF token-classification engine over openai/privacy-filter with pipeline cache and detect_pii_openai tool. |
| src/mmore/privacy/detection/presidio_engine.py | Presidio engine extended with custom clinical recognizers (MRN, HOSPITAL_DATE, INSURANCE_ID) and detect_pii_presidio tool. |
| src/mmore/privacy/detection/llm_engine.py | DSPy-based LLM engine with typed signature, demo examples, error-tolerant span post-processing, and detect_pii_llm tool. |
| src/mmore/privacy/dspy_llm.py | build_dspy_lm factory and LocalHFLM (dspy.BaseLM) wrapper around a cached transformers chat pipeline. |
| tests/test_detection.py | Mock-based unit tests covering config loading, tool registration, engine behavior, caching, and threshold/entity filtering across all four engines. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…to feat/detection-toolkit
…to feat/detection-toolkit
| class DetectionConfig: | ||
| """Schema for the ``privacy.detection`` block of a YAML config.""" | ||
|
|
||
| engine: str |
There was a problem hiding this comment.
the field engine seems to be used only in assertion tests so it sounds useless
There was a problem hiding this comment.
unless it's meant to be saved
There was a problem hiding this comment.
Yes it is still not used in this PR (it will be the case most likely in the PR were we wire the privacy layer into the mmore's RAG pipeline), but I think we want to keep it because later using this parameter the user will be able to choose a specific detection engine (instead of falling back to the default one or having the Analyzer agent infer one for the task)
There was a problem hiding this comment.
then it could make sense to have an enum with the supported detection engines
7794813 to
b42f4c1
Compare
…to feat/detection-toolkit
Co-authored-by: Jérémy Chaverot <jeremy.chaverot@epfl.ch>
Summary
Related issue: #292
Depending on: #285
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