一切从脱敏开始——Everything starts with redaction—
本地AI+规则程序的文档处理框架A document-processing framework combining local AI and rule-based programs
首先展示成品。把需求和处理思路交给Codex 持续迭代,利用免费额度完成了这个初版。First, here is the finished tool. I gave Codex the requirements and processing approach, then iterated with it to complete this initial version using the free allowance.



效果举例:An example of the results:

这是一份包含大量主体、公司、合伙企业和开户行的法律文书,工具成功识别并完成了初步脱敏。当然,自动处理不能代替最终责任,输出结果仍应由人工审核确认。This legal document contains numerous individuals, companies, partnerships and account-holding banks. The tool identified them and completed an initial round of redaction. Automated processing does not, of course, replace responsibility for the final result: a person must still review and confirm the output.
为了便于展示,图片中的部分内容作了加粗处理。可以看到,文档主体很多,脱敏时必须保留足够的区分信息,否则主体关系和文义就会变得难以理解。Some text in the image has been bolded for illustration. With so many parties in the document, redaction must retain enough distinguishing information; otherwise, the relationships between the parties and the meaning of the text become difficult to follow.
即使不作加粗,当前结果也已经具有较高的可辨识度和脱敏程度。如果需要进一步隐藏地名,也可以调整Prompt中的规则。脱敏尺度不是固定答案,而应根据使用目的和风险要求进行配置。Even without the bolding, the current result already combines a high degree of redaction with considerable readability. If place names also need to be concealed, the rules in the prompt can be adjusted. There is no single correct level of redaction: it must be configured for the intended use and the relevant risks.

后续会增加Prompt写入窗口,方便直接调整AI指令。至于保护规则、校验逻辑和正则表达式,仍需要通过Codex修改程序后写入执行层。I plan to add a prompt input field so that AI instructions can be adjusted directly. Protection rules, validation logic and regular expressions will still need to be implemented in the execution layer by modifying the program through Codex.
一、为什么从脱敏开始?I. Why start with redaction?
脱敏考验的能力非常综合:Redaction tests a broad combination of abilities:
1、你得有读懂文本的能力,能够区分哪些是敏感信息,哪些不是;1. You must understand the text well enough to distinguish sensitive information from information that is not sensitive.
2、你得知道如何遮蔽敏感信息,使用什么符号,遵循什么规则;2. You must know how to conceal sensitive information, which symbols to use and which rules to follow.
3、你还得理解同类敏感信息之间的差异,以及这些差异对文本语义的重要性,例如不同的人、不同的性别、不同的公司、企业主体和开户行。3. You must also understand differences within the same category of sensitive information and why those differences matter to the meaning of the text—for example, different individuals, genders, companies, business entities and account-holding banks.
脱敏看似是小事,真正做好却需要对文本进行全面理解和精确处理。Redaction may look minor, but doing it properly requires a comprehensive understanding of the text and precise processing.
能脱敏,那就能干更多的文档处理任务。A system capable of redaction should also be capable of more document-processing tasks.
更重要的是:More significantly:
我们正处在AI快速进入各类工作场景,而隐私保护制度、职业保密义务和内部数据规则仍需持续适应的阶段。AI is rapidly entering many working environments, while privacy protections, professional duties of confidentiality and internal data rules continue to adapt.
AI的使用需求巨大,效率优势也很明显,很难简单依靠禁止使用来解决问题。Demand for AI is substantial, and its efficiency benefits are clear. Simply prohibiting its use is unlikely to resolve the issue.
但既有的数据合规责任和职业保密义务并不会因为AI便利而消失,因此有必要保留一套本地文档处理工具。需要强调的是,本地并不天然等于完全隐私:只有模型推理、OCR或视觉识别、临时文件和日志均留在本机,并且没有启用在线API时,数据处理才可能形成尽量闭合的本地链路。Existing data-compliance responsibilities and professional confidentiality duties do not disappear because AI is convenient. There is therefore a reason to maintain a set of local document-processing tools. Local operation does not inherently guarantee complete privacy: a substantially closed local processing chain is possible only when model inference, OCR or visual recognition, temporary files and logs all remain on the device and no online API is enabled.
二、脱敏的方案II. Approaches to redaction
在制作工具的过程中,数次失败让我顿悟了一点。Several failures during development led me to a realization.
对于自己熟悉、篇幅不长、主体关系明确的Word文档,人工查找和替换往往是最直接、最可靠的脱敏方式。For a short Word document whose content and relationships between parties are familiar, manual find-and-replace is often the most direct and reliable redaction method.

是的你没看错,查找和替换。Yes, you read that correctly: find-and-replace.
对于自己制作或长期经手的文件,我们往往能够迅速理解主体之间的关系和区别。For documents we have drafted ourselves or handled over time, we can often quickly understand the relationships and distinctions between the parties.
因此,对于单份且内容熟悉的Word文档,直接查找和替换通常效率很高,人的语境判断力依然不可替代。Direct find-and-replace is therefore usually efficient for a single, familiar Word document. Human judgment about context remains indispensable.
但我们有时候也不仅仅面对的是word,还有pdf,还有图片这些证据材料。But our materials are not always Word documents: evidence also comes in PDFs and images.
总不能一个一个来涂画吧。We cannot reasonably paint over every item individually.
因此,这个工具的目标是方便批量文本处理:面对Word、PDF、图片和Excel文件时,先识别或提取核心文本,再按照统一规则进行脱敏。The tool is therefore intended to facilitate batch text processing: identify or extract the core text from Word, PDF, image and Excel files, then redact it under a common set of rules.
这样更适合批量处理,也能减少重复劳动。This better suits batch processing and reduces repetitive work.
接下来说工具。Now, the tools themselves.
常见方案之一,是使用Python 配合正则表达式进行脱敏。程序预先设定识别和替换规则,根据字符特征进行匹配,再执行确定性替换。这里依赖的是模式识别,而不是对全文语义的理解。One common approach combines Python and regular expressions. The program applies predefined identification and replacement rules, matches character patterns and executes deterministic replacements. This relies on pattern recognition, rather than an understanding of the entire document's meaning.
这种方式效率很高,但缺少语义理解能力。面对敏感信息多、主体关系复杂的文档时,容易出现遗漏、误判和简称错配。That approach is efficient but lacks semantic understanding. Documents with substantial sensitive information and complex relationships can result in omissions, misidentification and mismatched abbreviations.
然后是本地AI。Then there is local AI.
程序提取文档文本后,本地AI通常能够较完整地识别敏感信息,并理解主体之间的上下文关系。After the program extracts the document text, local AI can usually identify sensitive information relatively comprehensively and understand contextual relationships between the parties.
但如果让AI在对话中直接生成脱敏全文,仍可能因为Prompt理解偏差而遗漏信息,也可能越过处理边界,改写本应保留的原文。But asking AI to generate a fully redacted document in a conversation can still lead to omissions caused by misunderstood prompts. It may also exceed its remit and rewrite text that should have been preserved.
纯对话输出通常只是重新生成一份文字,不便于在原文对应位置稳定写回,也难以可靠保留文档格式。Conversational output generally regenerates text rather than reliably writing changes back to their corresponding locations in the original. It also struggles to preserve document formatting consistently.
因此,更稳妥的方式是把AI与本地规则程序结合起来。A more dependable approach therefore combines AI with a local rule-based program.
AI负责理解全文、识别实体并制定修改方案;Python规则程序负责校验方案、保护不得改动的内容,并在文档中精确执行替换。AI reads the complete text, identifies entities and prepares an amendment plan. The Python program validates that plan, protects content that must remain unchanged and precisely executes the replacements in the document.
正则表达式是规则程序的一部分,主要承担特征保护、格式校验和高风险数字掩码规范化,从而限制AI输出的不确定性。Regular expressions form part of the rule-based program. Their principal roles are protecting specified patterns, validating formatting and standardizing masks for high-risk numerical information, thereby constraining uncertainty in AI output.

所以总结,这个“本地AI+规则程序”脱敏工具的处理思路是:In summary, this local-AI-plus-rules redaction tool works as follows:
1、程序提取并汇总文档文本,本地AI完整通读一次,识别敏感信息及其上下文关系;1. The program extracts and compiles the document text. Local AI reads it in full once, identifying sensitive information and its contextual relationships.
2、AI不直接改写全文,而是输出“原文—替换文本”的结构化映射表;2. Instead of directly rewriting the document, AI produces a structured mapping from original text to replacement text.
3、Python规则程序检查映射是否合法,保护金额、日期、时间和法律依据等不得改动的内容,再执行精确替换;3. The Python program checks whether the mapping is valid, protects amounts, dates, times, legal authorities and other content that must remain unchanged, and then performs precise replacements.
4、程序在源文件同目录生成脱敏副本,源文件保持不变;Word和Excel尽量保留原格式;4. The program creates a redacted copy in the original file's directory, leaving the source unchanged. It preserves Word and Excel formatting as far as possible.
5、文字型PDF直接提取文本,扫描PDF和图片通过OCR或视觉模型识别,结果生成结构简单、兼容性优先的脱敏Word;5. Text-based PDFs are processed by direct text extraction. Scanned PDFs and images are recognized using OCR or a vision model, with the results placed in a redacted Word document that prioritizes a simple structure and compatibility.
6、文件可以直接拖放处理,模型接口可以配置为本地模型或兼容接口的在线AI。选择在线API时,应另行评估数据传输和服务商隐私政策。6. Files can be processed by drag-and-drop. The model interface can connect either to a local model or to online AI through a compatible API. Selecting an online API requires a separate assessment of data transmission and the provider's privacy policy.
三、工具制作和选择III. Building and selecting the tools
本地AI可以使用Ollama 或LM Studio部署,这部分此前已经分享过多次。Local AI can be deployed with Ollama or LM Studio; I have discussed that process several times before.
我习惯使用LM Studio,目前测试的模型是qwen/qwen3.6-35b-a3b 。I generally use LM Studio. The model currently under test is qwen/qwen3.6-35b-a3b.
需要Codex完成的,是编写一个能够调用本地AI的Python程序,并实现结构化映射、映射校验、精确替换、正则保护和文档写回。Codex's task is to write a Python program that calls local AI and implements structured mappings, mapping validation, precise replacement, regular-expression safeguards and document writeback.
这些需求都可以通过与Codex持续对话、测试和迭代来完成。These requirements can be developed through continued discussion, testing and iteration with Codex.
现在确实进入了许多想法可以通过对话迅速转化为可用工具的时代。We have indeed reached a point where many ideas can quickly become usable tools through conversation.

四、踩过的坑IV. Problems encountered
在本次任务和当前模型上,关闭长推理后反而更加稳定。脱敏需要的是严格执行既定规则,而不是不断扩展解释空间;推理链过长时,本地AI更容易偏离输出格式或擅自改写内容。这是当前测试中的经验结论,不一定适用于所有模型。For this task and the model currently being tested, disabling extended reasoning made performance more stable. Redaction requires strict adherence to existing rules, rather than an expanding scope of interpretation. When reasoning chains became too long, the local AI was more likely to deviate from the required output format or rewrite content without authorization. This is an observation from the present tests, not necessarily a conclusion applicable to every model.
另外,我曾经设想在处理完成后再让AI复核一次,以提高脱敏成功率。I also considered having AI review the result after processing to improve the success rate of redaction.
最初的架构是:先由AI生成方案并让规则程序执行一次,再把处理后的全文交给AI进行第二轮复核和修改。The initial architecture had AI produce a plan for the rule-based program to execute, then submitted the processed text to AI for a second round of review and amendment.

结果出现了严重问题:第二轮AI并不是单纯检查,而是重新改写和脱敏全文,造成重复替换、过度脱敏和文本污染。This caused a serious problem: the second AI pass did not simply check the output. It rewrote and redacted the entire text again, causing repeated replacements, excessive redaction and text corruption.

发现问题后,最终确认了现在的分工原则:AI只生成一次结构化修改方案,程序只执行一次确定性替换,不让AI重复改写全文。After identifying the problem, I settled on the current division of work: AI generates a structured amendment plan once, and the program executes deterministic replacements once. AI is not allowed to rewrite the complete text repeatedly.

因此,需要正视本地AI的能力边界,尽量使用简单、明确、可校验的指令。自动处理后仍应人工复核;如果需要AI辅助复审,也只允许它输出疑似遗漏清单,不能直接再次写回文档。We therefore need to recognize the limits of local AI and use instructions that are as simple, clear and verifiable as possible. Automated output still requires human review. If AI assists with a further review, it should only list suspected omissions and must not write additional changes directly into the document.
在这次项目开发中,使用Codex原生GPT模型进行代码协作相对稳定。我也尝试接入其他模型,但在代码理解和连续迭代方面差异较大。这只是本项目中的个人测试体验,并不代表对所有模型的普遍评价。In this project, coding collaboration using Codex's native GPT model was relatively stable. I also tried connecting other models, but their code comprehension and ability to support continuous iteration varied considerably. This is a personal observation from this project, not a general assessment of all models.
五、后续开发V. Further development
以“AI制定方案、规则程序确定性执行”的思路为框架,后续计划增加:Within the framework of AI preparing a plan and the rule-based program executing it deterministically, I plan to add:
1、增加Prompt写入框,方便修改AI指令;1. A prompt input field to make AI instructions easier to change.
2、增加其他文档处理规则,例如修改格式、填写信息等;2. Additional document-processing rules, such as formatting changes and information entry.
3、增加映射表预览和人工确认,让高风险替换在执行前可见;3. Mapping previews and human confirmation so that high-risk replacements are visible before execution.
4、增加非写入式风险审计,只生成疑似遗漏清单和处理报告;4. A read-only risk audit that produces lists of suspected omissions and processing reports without making further changes.
5、在同一框架下扩展合同审查、格式整理、信息提取和批量文档修改等本地处理能力。5. Further local capabilities within the same framework, including contract review, formatting, information extraction and batch document amendment.
欢迎多交流!Further discussion is welcome!
