bridging-presidio-and-spacy
它能做什么
这项技能把 OpenMed 临床文本工具与 Presidio、spaCy 和 LangChain 连接起来,这些都是常用的个人数据检测和语言应用工具。适合已经在使用其中某个工具、并希望把 OpenMed 集成进去的开发者。
适用场景
- ✓当你已经在使用 Microsoft Presidio,并希望在其中加入 OpenMed 临床 PII 检测的时候。
- ✓当你的 spaCy 流程需要输出 OpenMed 个人数据片段的时候。
- ✓当你在构建 LangChain 链,并希望在文本进入语言模型之前去除患者数据的时候。
技能链接
https://github.com/maziyarpanahi/openmed/blob/master/skills/bridging-presidio-and-spacy/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我已经在用 Microsoft Presidio。请把 OpenMed 临床 PII 检测加入我的 Presidio 流程。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体只安装所选工具所需的扩展包,然后使用对应的适配器,在 OpenMed 结果与该工具的格式之间进行转换。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Bridging Presidio, spaCy & LangChain
OpenMed interoperates with the dominant PII/NLP ecosystems through a single,
lazy adapter registry: openmed.interop. Adapters live behind explicit
imports, so importing openmed never drags in Presidio, spaCy, or LangChain —
each is an optional extra you install only when you need that bridge.
When to use
Reach for a bridge when:
- you already run Microsoft Presidio and want OpenMed's clinical PII recall on top (or to feed OpenMed spans back into Presidio's anonymizer);
- you have a spaCy pipeline and want OpenMed PII spans on the
Doc; - you build LangChain chains and want to redact PHI before text reaches an LLM (the on-device guardrail in front of a cloud model);
- you need OpenMed's de-identification reachable from an existing framework
instead of rewriting the pipeline around
openmed.deidentify.
The lazy adapter registry (verified)
import openmed.interop as interop
interop.available_adapters()
# ('cda', 'hl7v2', 'langchain', 'presidio', 'spacy')
spec = interop.adapter_spec("presidio")
# AdapterSpec(name='presidio', module='openmed.interop.presidio',
# extra='presidio', description='Presidio RecognizerResult adapter')
mod = interop.get_adapter("presidio") # imports openmed.interop.presidio
# Attribute access also works lazily:
openmed.interop.presidio # same module, imported on first touch
available_adapters() and adapter_spec() never import the adapter module, so
they are safe to call for discovery even without the extra installed.
get_adapter(name) (and attribute access) triggers the import — and the
adapter's own optional dependency.
Install only the extra you need:
pip install "openmed[presidio]" # Presidio RecognizerResult adapter
pip install "openmed[spacy]" # spaCy openmed_deid component
pip install "openmed[langchain]" # LangChain redaction runnable
# cda and hl7v2 adapters ship in core (no extra) — see their own skills
Presidio bridge (verified callables)
Module openmed.interop.presidio converts between Presidio
RecognizerResults and OpenMed canonical PIIEntitys, and merges both
detectors through OpenMed's semantic-unit merger.
from openmed.interop.presidio import (
to_canonical, # RecognizerResult(s) -> [PIIEntity]
from_canonical, # [PIIEntity] -> [RecognizerResult] (needs presidio extra)
merge_with_openmed, # combine OpenMed + Presidio spans, resolve overlaps
PresidioAdapterConfig,
)
import openmed
text = "Dr. Smith called patient at 617-555-0123 on 2024-03-02."
# Presidio gives you RecognizerResults; OpenMed gives PIIEntities.
openmed_spans = openmed.extract_pii(text).entities
presidio_results = analyzer.analyze(text=text, language="en") # your Presidio analyzer
merged = merge_with_openmed(
openmed_spans, presidio_results, text=text,
config=PresidioAdapterConfig(preserve_presidio_labels=True),
)
# -> de-duplicated [PIIEntity]; overlaps resolved by score, length, OpenMed-origin
Why merge instead of union: merge_with_openmed runs both detectors' spans
through merge_entities_with_semantic_units, so overlapping/adjacent detections
collapse into one correct span (e.g. PHONE from Presidio vs a partial OpenMed
hit) rather than producing double redactions. Label mapping is built in
(Presidio PHONE_NUMBER ↔ OpenMed PHONE, US_SSN ↔ SSN, etc.).
To push OpenMed spans into Presidio's anonymizer, convert back:
results = from_canonical(openmed_spans) # [RecognizerResult]
anonymized = anonymizer.anonymize(text=text, analyzer_results=results)
spaCy bridge (verified factory)
Module openmed.interop.spacy_component registers a spaCy pipeline factory
named openmed_deid. Add it to a pipeline and OpenMed PII spans land on the
Doc.
import spacy
import openmed.interop.spacy_component # registers the @Language.factory
nlp = spacy.blank("en")
nlp.add_pipe("openmed_deid", config={
"confidence_threshold": 0.5,
"lang": "en",
"target": "openmed_pii", # doc.spans key
"merge_ents": False, # set True to also write doc.ents
"alignment_mode": "expand", # char->token alignment: strict|contract|expand
})
doc = nlp("Patient John Doe, MRN 12345, seen today.")
for span in doc.spans["openmed_pii"]:
print(span.label_, span.text)
# raw char-offset spans also available on doc._.openmed_pii
`merge
常见问题
安装 OpenMed 会加载这三个工具吗?
不会。每个连接模块都是可选扩展,你只需安装所需的那一个。
OpenMed 的片段可以放回 Presidio 中吗?
可以。这项技能涵盖在 Presidio 格式与 OpenMed 结果之间互相转换。
它涵盖 LangChain 吗?
涵盖。它包括在文本进入语言模型之前先进行脱敏处理。
详情
作者的原始描述
Combine OpenMed clinical NLP with Microsoft Presidio, spaCy, or LangChain through OpenMed's built-in interop adapter registry (openmed.interop). Covers the lazy adapter registry (available_adapters, get_adapter, adapter_spec), the presidio/spacy/langchain pip extras, and the verified callables — Presidio to_canonical/from_canonical/merge_with_openmed, the spaCy openmed_deid pipeline factory, and the LangChain create_redaction_runnable. Use when the user wants to add Presidio recognizers, embed OpenMed PII detection in a spaCy pipeline, or use OpenMed de-identification as a LangChain runnable.
repo: maziyarpanahi/openmed
热度
- GitHub 星标
- 5.8k
- 排名
- #14677
- 许可证
- Apache-2.0
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
annotating-variants
5.8k帮你的智能体预测基因变异的作用,把 HGVS 名称转换为基因组位置,并使用免费的公共注释工具添加人群频率。
ask-openmed
5.8k帮你的智能体为临床文本请求选择合适的 OpenMed 工作流程,并在可能涉及患者数据时,先进行隐私检查。
assembling-fhir-bundles
5.8k把多份 FHIR 健康记录打包成一个事务 Bundle,可直接发送给 EHR 系统,并正确链接其中的内部引用。
auditing-deid-leakage
5.8k扫描已去标识化的临床文本,查找残留的标识符,例如社会安全号、银行卡号、电子邮件地址和日期,若仍有残留则阻止发布。