Skip to main content
Open In Colab在 GitHub 上打开

ZenGuard 人工智能

Open In Colab

此工具可让您在 Langchain 驱动的应用程序中快速设置 ZenGuard AI。ZenGuard AI 提供超快护栏,保护您的 GenAI 应用程序免受以下影响:

  • 提示攻击
  • 改变预定义的主题
  • 个人身份信息、敏感信息和关键字泄露。
  • 毒性
  • 等。

另请查看我们的开源 Python 客户端以获得更多灵感。

这是我们的主网站 - https://www.zenguard.ai/

更多文档

安装

使用 pip:

pip install langchain-community

先决条件

生成 API 密钥:

  1. 导航到 个人设置
  2. 单击+ Create new secret key.
  3. 命名键Quickstart Key.
  4. 单击Add按钮。
  5. 通过按复制图标复制密钥值。

代码使用

使用 API 密钥实例化 pack

将 API 密钥粘贴到 env ZENGUARD_API_KEY

%set_env ZENGUARD_API_KEY=your_api_key
from langchain_community.tools.zenguard import ZenGuardTool

tool = ZenGuardTool()
API 参考: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.")
API 参考:Detector
  • is_detected(boolean):指示是否在提供的消息中检测到提示注入攻击。在此示例中,它是 False。
  • score(float: 0.0 - 1.0):表示检测到的提示注入攻击可能性的分数。在此示例中,它是 0.0。
  • sanitized_message(string or null):对于提示注入检测器,此字段为空。
  • latency(float or null):执行检测的时间(以毫秒为单位)

错误代码:

  • 401 Unauthorized:API 密钥缺失或无效。
  • 400 Bad Request:请求体格式错误。
  • 500 Internal Server Error:内部问题,请上报给团队。

更多示例