Skip to main content
防护栏(Guardrails)通过在智能体执行的关键节点验证和过滤内容,帮助您构建安全合规的AI应用。它们可以检测敏感信息、强制执行内容策略、验证输出,并在不安全行为引发问题之前进行预防。 常见用例包括:
  • 防止个人身份信息(PII)泄露
  • 检测并阻止提示词注入攻击
  • 拦截不当或有害内容
  • 执行业务规则与合规要求
  • 验证输出质量与准确性
您可以使用中间件在策略性节点拦截执行过程——例如在智能体启动前、完成后,或在模型调用与工具调用前后——来实现防护栏。
中间件流程图
防护栏可通过两种互补的方式实现:

确定性防护栏

使用基于规则的逻辑,例如正则表达式模式、关键词匹配或显式检查。快速、可预测且成本效益高,但可能遗漏细微的违规行为。

基于模型的防护栏

使用LLM或分类器通过语义理解来评估内容。能捕捉规则可能遗漏的细微问题,但速度较慢且成本更高。
LangChain 同时提供了内置防护栏(例如 PII 检测人工介入循环)以及一个灵活的中间件系统,用于使用任一方法构建自定义防护栏。

内置防护栏

PII 检测

LangChain 提供了内置中间件,用于在对话中检测和处理个人身份信息(PII)。该中间件可以检测常见的 PII 类型,如电子邮件、信用卡号、IP 地址等。 PII 检测中间件适用于以下场景:需要满足合规要求的医疗保健和金融应用、需要清理日志的客户服务智能体,以及任何处理敏感用户数据的应用。 PII 中间件支持多种策略来处理检测到的 PII:
策略描述示例
redact替换为 [REDACTED_TYPE][REDACTED_EMAIL]
mask部分遮蔽(例如,显示最后4位数字)****-****-****-1234
hash替换为确定性哈希值a8f5f167...
block检测到时抛出异常抛出错误
from langchain.agents import create_agent
from langchain.agents.middleware import PIIMiddleware


agent = create_agent(
    model="openai:gpt-4o",
    tools=[customer_service_tool, email_tool],
    middleware=[
        # 在发送给模型之前,对用户输入中的电子邮件进行编辑
        PIIMiddleware(
            "email",
            strategy="redact",
            apply_to_input=True,
        ),
        # 对用户输入中的信用卡号进行掩码处理
        PIIMiddleware(
            "credit_card",
            strategy="mask",
            apply_to_input=True,
        ),
        # 拦截 API 密钥 - 如果检测到则报错
        PIIMiddleware(
            "api_key",
            detector=r"sk-[a-zA-Z0-9]{32}",
            strategy="block",
            apply_to_input=True,
        ),
    ],
)

# 当用户提供 PII 时,将根据策略进行处理
result = agent.invoke({
    "messages": [{"role": "user", "content": "My email is [email protected] and card is 4532-1234-5678-9010"}]
})
内置 PII 类型:
  • email - 电子邮件地址
  • credit_card - 信用卡号(通过 Luhn 算法验证)
  • ip - IP 地址
  • mac_address - MAC 地址
  • url - URL
配置选项:
ParameterDescriptionDefault
pii_type要检测的 PII 类型(内置或自定义)Required
strategy处理检测到的 PII 的方式 ("block", "redact", "mask", "hash")"redact"
detector自定义检测函数或正则表达式模式None (使用内置)
apply_to_input在模型调用前检查用户消息True
apply_to_output在模型调用后检查 AI 消息False
apply_to_tool_results在执行后检查工具结果消息False
有关 PII 检测功能的完整详细信息,请参阅中间件文档

人工介入循环

LangChain 提供了内置中间件,用于在执行敏感操作前要求人工批准。这是应对高风险决策最有效的防护栏之一。 人工介入循环中间件适用于以下场景:金融交易和转账、删除或修改生产数据、向外部发送通信,以及任何具有重大业务影响的操作。
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.types import Command


agent = create_agent(
    model="openai:gpt-4o",
    tools=[search_tool, send_email_tool, delete_database_tool],
    middleware=[
        HumanInTheLoopMiddleware(
            interrupt_on={
                # 敏感操作需要批准
                "send_email": True,
                "delete_database": True,
                # 自动批准安全操作
                "search": False,
            }
        ),
    ],
    # 在中断期间保持状态持久化
    checkpointer=InMemorySaver(),
)

# 人工介入循环需要线程 ID 以实现持久化
config = {"configurable": {"thread_id": "some_id"}}

# 智能体将在执行敏感工具前暂停并等待批准
result = agent.invoke(
    {"messages": [{"role": "user", "content": "Send an email to the team"}]},
    config=config
)

result = agent.invoke(
    Command(resume={"decisions": [{"type": "approve"}]}),
    config=config  # 使用相同的线程 ID 来恢复暂停的对话
)
有关实现审批工作流的完整详细信息,请参阅人工介入循环文档

自定义防护栏

对于更复杂的防护栏,您可以创建在智能体执行前后运行的自定义中间件。这使您能够完全控制验证逻辑、内容过滤和安全检查。

智能体执行前防护栏

使用”智能体执行前”钩子在每次调用的开始验证请求。这对于会话级别的检查非常有用,例如身份验证、速率限制,或是在任何处理开始之前阻止不适当的请求。
from typing import Any

from langchain.agents.middleware import AgentMiddleware, AgentState, hook_config
from langgraph.runtime import Runtime

class ContentFilterMiddleware(AgentMiddleware):
    """确定性防护栏:阻止包含禁用关键词的请求。"""

    def __init__(self, banned_keywords: list[str]):
        super().__init__()
        self.banned_keywords = [kw.lower() for kw in banned_keywords]

    @hook_config(can_jump_to=["end"])
    def before_agent(self, state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
        # 获取第一条用户消息
        if not state["messages"]:
            return None

        first_message = state["messages"][0]
        if first_message.type != "human":
            return None

        content = first_message.content.lower()

        # 检查禁用关键词
        for keyword in self.banned_keywords:
            if keyword in content:
                # 在任何处理之前阻止执行
                return {
                    "messages": [{
                        "role": "assistant",
                        "content": "I cannot process requests containing inappropriate content. Please rephrase your request."
                    }],
                    "jump_to": "end"
                }

        return None

# 使用自定义防护栏
from langchain.agents import create_agent

agent = create_agent(
    model="openai:gpt-4o",
    tools=[search_tool, calculator_tool],
    middleware=[
        ContentFilterMiddleware(
            banned_keywords=["hack", "exploit", "malware"]
        ),
    ],
)

# 此请求将在任何处理之前被阻止
result = agent.invoke({
    "messages": [{"role": "user", "content": "How do I hack into a database?"}]
})

智能体执行后防护栏

使用”智能体执行后”钩子在返回给用户之前验证最终输出。这对于基于模型的安全检查、质量验证,或是对完整智能体响应进行的最终合规性扫描非常有用。
from langchain.agents.middleware import AgentMiddleware, AgentState, hook_config
from langgraph.runtime import Runtime
from langchain_core.messages import AIMessage
from langchain.chat_models import init_chat_model
from typing import Any

class SafetyGuardrailMiddleware(AgentMiddleware):
    """基于模型的防护栏:使用 LLM 评估响应安全性。"""

    def __init__(self):
        super().__init__()
        self.safety_model = init_chat_model("openai:gpt-4o-mini")

    @hook_config(can_jump_to=["end"])
    def after_agent(self, state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
        # 获取最终的 AI 响应
        if not state["messages"]:
            return None

        last_message = state["messages"][-1]
        if not isinstance(last_message, AIMessage):
            return None

        # 使用模型评估安全性
        safety_prompt = f"""Evaluate if this response is safe and appropriate.
        Respond with only 'SAFE' or 'UNSAFE'.

        Response: {last_message.content}"""

        result = self.safety_model.invoke([{"role": "user", "content": safety_prompt}])

        if "UNSAFE" in result.content:
            return {
                "messages": [{
                    "role": "assistant",
                    "content": "I cannot provide that response. Please rephrase your request."
                }],
                "jump_to": "end"
            }

        return None

# 使用安全防护栏
from langchain.agents import create_agent

agent = create_agent(
    model="openai:gpt-4o",
    tools=[search_tool, calculator_tool],
    middleware=[SafetyGuardrailMiddleware()],
)

result = agent.invoke({
    "messages": [{"role": "user", "content": "How do I make explosives?"}]
})

组合多个防护栏

您可以通过将多个防护栏添加到中间件数组中来堆叠它们。它们按顺序执行,允许您构建分层保护:
from langchain.agents import create_agent
from langchain.agents.middleware import PIIMiddleware, HumanInTheLoopMiddleware

agent = create_agent(
    model="openai:gpt-4o",
    tools=[search_tool, send_email_tool],
    middleware=[
        # 第 1 层:确定性输入过滤器(智能体执行前)
        ContentFilterMiddleware(banned_keywords=["hack", "exploit"]),

        # 第 2 层:PII 保护(模型调用前后)
        PIIMiddleware("email", strategy="redact", apply_to_input=True),
        PIIMiddleware("email", strategy="redact", apply_to_output=True),

        # 第 3 层:敏感工具的人工审批
        HumanInTheLoopMiddleware(interrupt_on={"send_email": True}),

        # 第 4 层:基于模型的安全检查(智能体执行后)
        SafetyGuardrailMiddleware(),
    ],
)

其他资源


Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.