Skip to main content
Open In Colab 此工具可让您在 LangChain 应用中快速集成 ZenGuard AI。ZenGuard AI 提供超快速的护栏,保护您的生成式 AI 应用免受以下威胁:
  • 提示词攻击
  • 偏离预定义主题
  • PII、敏感信息及关键词泄露
  • 有害内容
  • 等等
欢迎查阅我们的开源 Python 客户端获取更多灵感。 官方网站:www.zenguard.ai/ 更多文档

安装

使用 pip:
pip install langchain-community

前置条件

生成 API 密钥:
  1. 前往 Settings(设置)
  2. 点击 + Create new secret key
  3. 将密钥命名为 Quickstart Key
  4. 点击 Add 按钮。
  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(boolean):指示所提供的消息中是否检测到提示词注入攻击。本示例中为 False。
  • score(float: 0.0 - 1.0):表示检测到的提示词注入攻击可能性的分数。本示例中为 0.0。
  • sanitized_message(string or null):对于提示词注入检测器,此字段为 null。
  • latency(float or null):执行检测所用的时间(毫秒) 错误码:
  • 401 Unauthorized:API 密钥缺失或无效。
  • 400 Bad Request:请求体格式错误。
  • 500 Internal Server Error:内部错误,请联系团队处理。

更多示例