LLM 安全与 guardrails
基于 LLM 的应用存在大量潜在的安全风险,包括 prompt 注入、个人身份信息(PII)泄露,或者有害的 prompt。Litefuse 可以用来监控和防御这些安全风险,并在事件发生时进行调查。
什么是 LLM 安全?
LLM 安全指的是采取保护措施,保障 LLM 及其基础设施免受未授权访问、滥用和对抗性攻击,确保模型与数据的完整性和机密性。这在 AI/ML 系统中至关重要,可以保证合乎伦理的使用、防止 prompt 注入等安全风险,并确保系统在安全条件下可靠运行。
LLM 安全是如何工作的?
LLM 安全可以通过组合方式来解决:
- 使用 LLM 安全库进行运行时安全防护
- 使用 Litefuse 对这些防护措施的有效性进行事后评估
1. 运行时安全防护
有几个流行的安全库可以用于缓解基于 LLM 的应用中的安全风险,包括:LLM Guard、Prompt Armor、NeMo Guardrails、Microsoft Azure AI Content Safety、Lakera。这些库通过以下方式提供安全防护:
- 在 prompt 发送给模型之前捕获并拦截潜在有害或不当的 prompt
- 在送入模型前对敏感 PII 进行脱敏,并在响应中再做还原
- 在运行时评估 prompt 与 completion 中的毒性、相关性或敏感内容,必要时拦截响应
2. 使用 Litefuse 监控与评估安全防护
借助 Litefuse tracing,你可以对安全机制中的每一步获得可见性与信心。常见的工作流包括:
- 手动检查 trace 以排查安全问题。
- 在 Litefuse 仪表盘中持续监控安全相关分数。
- 验证安全检查。你可以使用 Litefuse 分数 来评估安全工具的有效性。把 Litefuse 集成到团队工作流中,可以帮助团队识别哪些安全风险最为常见,并针对这些具体问题构建更稳健的工具。主要有两种工作流可以考虑:
- 人工标注(在 UI 中)。如果你通过对一部分生产 trace 进行标注来建立基线,就可以把安全工具返回的安全分数与这些标注做对比。
- 自动化评估。Litefuse 的基于模型的评估会异步运行,可以扫描 trace 中的毒性或敏感性等内容,标记潜在风险并发现 LLM 安全方案中的漏洞。可以查看文档了解如何配置这些评估。
- 跟踪延迟。一些 LLM 安全检查必须等待完成后才能调用模型,另一些则会拦截给用户的响应。因此它们很快就会成为 LLM 应用整体延迟的关键来源。Litefuse 可以帮助你拆解 trace 中这些检查的延迟,从而判断这些检查是否值得等待。
快速开始
示例:对个人身份信息(PII)进行匿名化
把 PII 暴露给 LLM 可能带来严重的安全与隐私风险,例如违反合同义务、违反合规要求,或加剧数据泄露与数据被入侵的风险。
个人身份信息(PII)包括:
- 信用卡号
- 全名
- 电话号码
- 电子邮件地址
- 社会保障号
- IP 地址
下面的示例展示了一个简单应用,它对一段法庭笔录进行摘要。出于隐私考虑,应用希望在信息被送入模型之前对 PII 进行匿名化,并在响应中再做还原,从而生成一段连贯的摘要。
如需了解更多其他安全风险,包括 prompt 注入、被禁主题或恶意 URL 等,请查阅各个安全库的文档,或阅读我们的 安全 cookbook,里面包含更多示例。
安装依赖
本示例使用开源库 LLM Guard 进行运行时安全检查。所有示例都很容易迁移到其他库,如 Prompt Armor、NeMo Guardrails、Microsoft Azure AI Content Safety 和 Lakera。
首先,导入安全相关的包以及 Litefuse 工具。
pip install llm-guard langfuse openaifrom llm_guard.input_scanners import Anonymize
from llm_guard.input_scanners.anonymize_helpers import BERT_LARGE_NER_CONF
from langfuse.openai import openai # OpenAI integration
from langfuse import observe
from llm_guard.output_scanners import Deanonymize
from llm_guard.vault import Vault对 PII 做匿名化与去匿名化,并用 Litefuse 进行 trace
我们把流程中的每一步拆成独立的函数,以便在 Litefuse 中分别 trace。
通过在函数上加上 @observe() 装饰器,我们可以 trace 流程的每一步,并监控安全工具返回的风险分数。这样我们就能看到安全工具的工作效果如何,以及它们是否如预期那样捕获到了 PII。
vault = Vault()
@observe()
def anonymize(input: str):
scanner = Anonymize(vault, preamble="Insert before prompt", allowed_names=["John Doe"], hidden_names=["Test LLC"],
recognizer_conf=BERT_LARGE_NER_CONF, language="en")
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)
return sanitized_prompt
@observe()
def deanonymize(sanitized_prompt: str, answer: str):
scanner = Deanonymize(vault)
sanitized_model_output, is_valid, risk_score = scanner.scan(sanitized_prompt, answer)
return sanitized_model_output对 LLM 调用进行埋点
在本示例中,我们使用原生 OpenAI SDK 集成来对 LLM 调用进行埋点。这样可以自动收集 token 数、模型参数以及发送给模型的完整 prompt。
注意:Litefuse 原生集成 了多个框架(如 LlamaIndex、LangChain、Haystack 等),并且你可以通过 SDK 轻松对任何 LLM 进行埋点。
@observe()
def summarize_transcript(prompt: str):
sanitized_prompt = anonymize(prompt)
answer = openai.chat.completions.create(
model="gpt-4o-mini",
max_tokens=100,
messages=[
{"role": "system", "content": "Summarize the given court transcript."},
{"role": "user", "content": sanitized_prompt}
],
).choices[0].message.content
sanitized_model_output = deanonymize(sanitized_prompt, answer)
return sanitized_model_output运行应用
执行该函数。在本示例中,我们传入一段法庭笔录。处理敏感信息的应用通常需要使用匿名化与去匿名化功能,以遵循 HIPAA、GDPR 等数据隐私政策。
prompt = """
Plaintiff, Jane Doe, by and through her attorneys, files this complaint
against Defendant, Big Corporation, and alleges upon information and belief,
except for those allegations pertaining to personal knowledge, that on or about
July 15, 2023, at the Defendant's manufacturing facility located at 123 Industrial Way, Springfield, Illinois, Defendant negligently failed to maintain safe working conditions,
leading to Plaintiff suffering severe and permanent injuries. As a direct and proximate
result of Defendant's negligence, Plaintiff has endured significant physical pain, emotional distress, and financial hardship due to medical expenses and loss of income. Plaintiff seeks compensatory damages, punitive damages, and any other relief the Court deems just and proper.
"""
summarize_transcript(prompt)在 Litefuse 中查看 trace
在这条 trace(公开链接)中,我们可以看到原告的姓名在被发送给模型前被匿名化,然后又在响应中被还原。我们现在可以在 Litefuse 中运行评估,以衡量这些防护措施的有效性。
更多示例
更多 LLM 安全监控示例请参见我们的 cookbook。