LLM输出文本的敏感信息检测
在大模型应用中,确保输出内容的安全性是至关重要的环节。本文将探讨如何通过技术手段检测LLM输出中的敏感信息。
敏感信息类型识别
常见的敏感信息包括:
- 身份证号、护照号等证件号码
- 银行卡号、支付账号等金融信息
- 个人联系方式、地址等隐私数据
- 企业机密、代码片段等商业信息
检测方法实现
import re
class SensitiveDetector:
def __init__(self):
# 定义敏感信息模式
self.patterns = {
'id_card': r'\d{17}[\dXx]',
'bank_card': r'\d{16,19}',
'phone': r'1[3-9]\d{9}',
'email': r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
}
def detect(self, text):
results = []
for key, pattern in self.patterns.items():
matches = re.findall(pattern, text)
if matches:
results.append({
'type': key,
'matches': matches
})
return results
# 使用示例
detector = SensitiveDetector()
text = "联系方式:13812345678,邮箱:test@example.com"
results = detector.detect(text)
print(results)
复现步骤
- 创建Python环境并安装re模块(内置)
- 复制上述代码到文件中
- 运行脚本测试不同类型的敏感信息检测
- 根据实际需求调整正则表达式模式
通过构建这样的检测机制,可以有效降低LLM输出内容带来的安全风险。

讨论