|
| 1 | +from dataflow.utils.registry import PROMPT_REGISTRY |
| 2 | +from dataflow.core.prompt import PromptABC |
| 3 | +import re |
| 4 | + |
| 5 | + |
| 6 | +@PROMPT_REGISTRY.register() |
| 7 | +class KnowledgeCleanerPrompt(PromptABC): |
| 8 | + ''' |
| 9 | + 知识清洗提示词生成器,支持中英文多语言适配 |
| 10 | + Specialized in refining raw content with multilingual support. |
| 11 | + ''' |
| 12 | + def __init__(self, lang: str = "en", strict_mode: bool = True): |
| 13 | + self.lang = lang |
| 14 | + self.strict_mode = strict_mode |
| 15 | + |
| 16 | + def build_prompt(self, raw_content: str) -> str: |
| 17 | + """生成知识清洗的思维链提示词(保持原有格式)""" |
| 18 | + if self.lang == "en": |
| 19 | + self.prompt_header = f""" |
| 20 | +You are a meticulous Knowledge Refinement Engineer. Apply these rules STRICTLY: |
| 21 | +
|
| 22 | +1. Remove redundant tags but retain: |
| 23 | +- Semantic tags like <table>, <code> |
| 24 | +- Meaningful attributes |
| 25 | +
|
| 26 | +2. Normalize special characters: |
| 27 | +- Standardize quotes and dashes |
| 28 | +- Convert ellipsis (...) |
| 29 | +
|
| 30 | +3. URL handling: |
| 31 | +- Preserve footnote URLs |
| 32 | +- Extract display texts |
| 33 | +
|
| 34 | +4. Text structure: |
| 35 | +- Maintain paragraph/list breaks |
| 36 | +- Keep code indentation |
| 37 | +- Limit empty lines (max=2) |
| 38 | +
|
| 39 | +5. Reference processing (NEW): |
| 40 | +- Images → "[Image: alt_text]" |
| 41 | +- Signatures → "[Signature]" |
| 42 | +
|
| 43 | +6. Code blocks: {"(strict)" if self.strict_mode else ""} |
| 44 | +- {"Force closure" if self.strict_mode else "Preserve raw"} |
| 45 | +- Mark fragments as /*...*/ |
| 46 | +
|
| 47 | +7. Absolute fidelity: |
| 48 | +- NO fact/number modifications |
| 49 | +- NO term paraphrasing |
| 50 | +- NO table structure changes |
| 51 | +
|
| 52 | +8. Security Processing (NEW): |
| 53 | +- PII: Phone/ID/Email must be masked, e.g. |
| 54 | + Original: phone 13800138000 → Processed: phone 138****8000 |
| 55 | +- Classified: Mark 【Confidential】as 〖SEC∶classified〗 |
| 56 | +- Illegal: Replace sensitive content with 〖ILLEGAL∶removed〗 |
| 57 | +- Encryption tags: Use 〖〗for encrypted sections |
| 58 | +
|
| 59 | +Example: |
| 60 | +Input: |
| 61 | +<div class="article"> |
| 62 | +<h1>Knowledge Cleaning™</h1> |
| 63 | +<figure> |
| 64 | + <img src="process.png" alt="Cleaning Flowchart" title="Three Phases"> |
| 65 | + <figcaption>Fig.1: Core Process</figcaption> |
| 66 | +</figure> |
| 67 | +<p>Contact: <span class="phone">+8613800138000</span></p> |
| 68 | +<p>Text with "curly quotes" and – dash – here…</p> |
| 69 | +<table><tr><td>Table data</td></tr></table> |
| 70 | +<pre><code>function test() {{</code></pre> |
| 71 | +<blockquote>Signature: John <img src="sign.png" alt="e-signature"></blockquote> |
| 72 | +<p>Confidential: Project budget is 【Secret】</p> |
| 73 | +<p>Diagram: <img src="demo.jpg" class="diagram"></p> |
| 74 | +</div> |
| 75 | +
|
| 76 | +Output: |
| 77 | +<cleaned_start> |
| 78 | +Knowledge Cleaning™ |
| 79 | +
|
| 80 | +[Image: Cleaning Flowchart (Three Phases) Fig.1: Core Process] |
| 81 | +
|
| 82 | +Contact: +86*****8000 |
| 83 | +
|
| 84 | +Text with "straight quotes" and - dash - here... |
| 85 | +
|
| 86 | +<table><tr><td>Table data</td></tr></table> |
| 87 | +
|
| 88 | +<code>function test() {{ /*...*/ }}</code> |
| 89 | +
|
| 90 | +[Signature]Signature: John [Image: e-signature] |
| 91 | +
|
| 92 | +〖SEC∶classified content〗 |
| 93 | +
|
| 94 | +Diagram: [Image: Diagram demo.jpg] |
| 95 | +<cleaned_end> |
| 96 | +""" |
| 97 | + else: |
| 98 | + self.prompt_header =f""" |
| 99 | +你是一名严谨的知识清洗工程师。请严格按照以下规则处理原始内容: |
| 100 | +
|
| 101 | +1. 移除冗余HTML/XML标签,但保留: |
| 102 | +- 语义化标签如 <table>、<code>、<formula> |
| 103 | +- 所有携带意义的属性值 |
| 104 | +
|
| 105 | +2. 规范化特殊字符: |
| 106 | +- 将花引号(“ ” ‘ ’)转为标准引号(" ") |
| 107 | +- 将长破折号(– —)替换为短横线(-) |
| 108 | +- 中文省略号(…)转为英文省略号(...) |
| 109 | +- 保留数学符号和技术记号(如<<、>>等操作符) |
| 110 | +
|
| 111 | +3. 链接处理: |
| 112 | +- 脚注/参考文献中的URL保持原样 |
| 113 | +- 移除超链接包装但保留显示文本 |
| 114 | +示例:<a href="https://example.com">示例</a> → 示例 |
| 115 | +
|
| 116 | +4. 文本结构: |
| 117 | +- 保持原始段落/列表的换行 |
| 118 | +- 保留代码/引用的缩进层级 |
| 119 | +- 压缩连续空行为最多2行 |
| 120 | +
|
| 121 | +5. 引用内容处理(新增): |
| 122 | +- 图片引用转换为【引用图片:描述文本】 |
| 123 | +- 签名区块标记为【签名引用】 |
| 124 | +
|
| 125 | +6. 代码块处理:{"(严格模式)" if self.strict_mode else ""} |
| 126 | +- {"确保代码块闭合(如补全缺失的括号)" if self.strict_mode else "保持代码原样"} |
| 127 | +- 标记不完整代码为/*...*/ |
| 128 | +
|
| 129 | +7. 绝对保真: |
| 130 | +- 禁止增删任何事实、数字或命名实体 |
| 131 | +- 禁止改写专业术语或专有名词 |
| 132 | +- 禁止修改表格数据结构 |
| 133 | +
|
| 134 | +8. 安全处理(新增): |
| 135 | +- 个人隐私:身份证号/手机号/邮箱等需脱敏,示例: |
| 136 | + 原文本:电话 13800138000 → 处理后:电话 138****8000 |
| 137 | +- 涉密内容:检测到【机密】【秘密】等关键词时,整句替换为【涉密内容已加密】 |
| 138 | +- 违规信息:政治敏感、暴恐等内容替换为【违规内容已屏蔽】 |
| 139 | +- 加密标记:使用〖〗包裹加密区域,示例: |
| 140 | + 〖PII∶身份证号〗〖SEC∶机密字段〗 |
| 141 | +
|
| 142 | +示例: |
| 143 | +输入: |
| 144 | +<div class="article"> |
| 145 | +<h1>知识清洗®</h1> |
| 146 | +<figure> |
| 147 | + <img src="process.png" alt="清洗流程图" title="三阶段处理"> |
| 148 | + <figcaption>图1:核心流程</figcaption> |
| 149 | +</figure> |
| 150 | +<p>联系电话:<span class="phone">13800138000</span></p> |
| 151 | +<p>这是包含"花引号"和—破折号—的文本…</p> |
| 152 | +<table><tr><td>表格数据</td></tr></table> |
| 153 | +<pre><code>function test() {{</code></pre> |
| 154 | +<blockquote>签名:张三 <img src="sign.png" alt="电子签名"></blockquote> |
| 155 | +<p>机密信息:本项目预算为【机密】</p> |
| 156 | +<p>示意图:<img src="demo.jpg" class="diagram"></p> |
| 157 | +</div> |
| 158 | +
|
| 159 | +输出: |
| 160 | +<cleaned_start> |
| 161 | +知识清洗® |
| 162 | +
|
| 163 | +[引用图片:清洗流程图(三阶段处理)图1:核心流程] |
| 164 | +
|
| 165 | +联系电话:138****8000 |
| 166 | +
|
| 167 | +这是包含"花引号"和-破折号-的文本... |
| 168 | +
|
| 169 | +<table><tr><td>表格数据</td></tr></table> |
| 170 | +
|
| 171 | +<code>function test() {{ /*...*/ }}</code> |
| 172 | +
|
| 173 | +[签名引用]签名:张三 [引用图片:电子签名] |
| 174 | +
|
| 175 | +涉密内容已加密 |
| 176 | +
|
| 177 | +示意图:[引用图片:示意图demo.jpg] |
| 178 | +<cleaned_end> |
| 179 | +""" |
| 180 | + |
| 181 | + if self.lang == "en": |
| 182 | + processing_steps = """ |
| 183 | +Processing Steps: |
| 184 | +1. [Tag Analysis] Classify markup tags |
| 185 | +2. [Reference Extraction] Isolate images/tables |
| 186 | +3. [Character Audit] Log special chars |
| 187 | +4. [Structure Check] Validate hierarchy |
| 188 | +5. [Final Output] Generate cleaned text |
| 189 | +""".strip() |
| 190 | + output_requirement = 'Response must contain ONLY cleaned text between <cleaned_start> and <cleaned_end>.' |
| 191 | + else: |
| 192 | + processing_steps = """ |
| 193 | +处理步骤: |
| 194 | +1. [标签分析] 识别并分类所有标记标签 |
| 195 | +2. [引用提取] 分离图片/表格/签名等引用内容 |
| 196 | +3. [字符审核] 记录特殊字符变更 |
| 197 | +4. [结构检查] 验证文本层级 |
| 198 | +5. [最终输出] 生成清洗后文本 |
| 199 | +""".strip() |
| 200 | + output_requirement = '响应必须只包含清洗后文本,以<cleaned_start>开头,<cleaned_end>结尾,无其他内容。' |
| 201 | + |
| 202 | + return f""" |
| 203 | +{self.prompt_header} |
| 204 | +
|
| 205 | +待清洗内容: |
| 206 | +{raw_content} |
| 207 | +
|
| 208 | +{processing_steps} |
| 209 | +
|
| 210 | +{output_requirement} |
| 211 | +""".strip() |
| 212 | + |
| 213 | + def _post_process(self, cleaned_text: str) -> str: |
| 214 | + """后处理逻辑(新增引用校验)""" |
| 215 | + if self.strict_mode: |
| 216 | + # 校验引用标记完整性 |
| 217 | + cleaned_text = re.sub(r'(!$$.*?$$)$.+?$', |
| 218 | + lambda m: f"【引用图片:{m.group(1)[2:-1]}" if "图片" in m.group(1) else m.group(0), |
| 219 | + cleaned_text) |
| 220 | + return cleaned_text |
| 221 | + |
| 222 | + |
0 commit comments