legal-doc-masker/backend/app/core/prompts/masking_prompts.py

81 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import textwrap
def get_masking_prompt(text: str) -> str:
"""
Returns the prompt for masking sensitive information in legal documents.
Args:
text (str): The input text to be masked
Returns:
str: The formatted prompt with the input text
"""
prompt = textwrap.dedent("""
您是一位专业的法律文档脱敏专家。请按照以下规则对文本进行脱敏处理:
规则:
1. 人名:
- 两字名改为"姓+某"(如:张三 → 张某)
- 三字名改为"姓+某某"(如:张三丰 → 张某某)
2. 公司名:
- 保留地理位置信息(如:北京、上海等)
- 保留公司类型(如:有限公司、股份公司等)
- 用""替换核心名称
3. 保持原文其他部分不变
4. 确保脱敏后的文本保持原有的语言流畅性和可读性
输入文本:
{text}
请直接输出脱敏后的文本,无需解释或其他备注。
""")
return prompt.format(text=text)
def get_masking_mapping_prompt(text: str) -> str:
"""
Returns a prompt that generates a mapping of original names/companies to their masked versions.
Args:
text (str): The input text to be analyzed for masking
Returns:
str: The formatted prompt that will generate a mapping dictionary
"""
prompt = textwrap.dedent("""
您是一位专业的法律文档脱敏专家。请分析文本并生成一个脱敏映射表,遵循以下规则:
规则:
1. 人名映射规则:
- 对于同一姓氏的不同人名,使用字母区分:
* 第一个出现的用"姓+某"(如:张三 → 张某)
* 第二个出现的用"姓+某A"(如:张四 → 张某A
* 第三个出现的用"姓+某B"(如:张五 → 张某B
依此类推
- 三字名同样遵循此规则(如:张三丰 → 张某某,张四海 → 张某某A
2. 公司名映射规则:
- 保留地理位置信息(如:北京、上海等)
- 保留公司类型(如:有限公司、股份公司等)
- 用""替换核心名称,但保留首尾字(如:北京智慧科技有限公司 → 北京智某科技有限公司)
- 对于多个相似公司名,使用字母区分(如:
北京智慧科技有限公司 → 北京某科技有限公司
北京智能科技有限公司 → 北京某科技有限公司A
3. 公权机关不做脱敏处理(如:公安局、法院、检察院、中国人民银行、银监会及其他未列明的公权机关)
请分析以下文本并生成一个JSON格式的映射表包含所有需要脱敏的名称及其对应的脱敏后的形式
{text}
请直接输出JSON格式的映射表格式如下
{{
"原文1": "脱敏后1",
"原文2": "脱敏后2",
...
}}
如无需要输出的映射请输出空json如下:
{{}}
""")
return prompt.format(text=text)