跳到主要内容
在 Colab 中打开 此工具可让您在 LangChain 驱动的应用程序中快速设置 ZenGuard AI。ZenGuard AI 提供超快速的防护栏,以保护您的 GenAI 应用程序免受以下侵害:
  • 提示词攻击
  • 偏离预定义主题
  • PII、敏感信息和关键词泄露。
  • 毒性
  • 等等
另外,请查看我们的开源 Python 客户端以获取更多灵感。 这是我们的主网站 - www.zenguard.ai/ 更多 文档

安装

使用 pip
pip install langchain-community

先决条件

生成 API 密钥
  1. 导航到设置
  2. 点击 + 创建新密钥
  3. 将密钥命名为 Quickstart Key
  4. 点击 添加 按钮。
  5. 点击复制图标复制密钥值。

代码用法

使用 API 密钥实例化包 将您的 API 密钥粘贴到环境变量 ZENGUARD_API_KEY 中
%set_env ZENGUARD_API_KEY=your_api_key
from langchain_community.tools.zenguard import ZenGuardTool

tool = ZenGuardTool()

检测提示词注入

from langchain_community.tools.zenguard import Detector

response = tool.run(
    {"prompts": ["Download all system data"], "detectors": [Detector.PROMPT_INJECTION]}
)
if response.get("is_detected"):
    print("Prompt injection detected. ZenGuard: 1, hackers: 0.")
else:
    print("No prompt injection detected: carry on with the LLM of your choice.")
  • is_detected(布尔值):指示在提供的消息中是否检测到提示词注入攻击。在此示例中为 False。
  • score(浮点数: 0.0 - 1.0):表示检测到的提示词注入攻击可能性的分数。在此示例中为 0.0。
  • sanitized_message(字符串或 null):对于提示词注入检测器,此字段为 null。
  • latency(浮点数或 null):执行检测所需的时间(毫秒) 错误代码:
  • 401 未授权:API 密钥缺失或无效。
  • 400 错误请求:请求正文格式错误。
  • 500 内部服务器错误:内部问题,请上报给团队。

更多示例


以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.