跳到主要內容
Open In ColabOpen on GitHub

ZenGuard AI

Open In Colab

這個工具讓您可以在 Langchain 驅動的應用程式中快速設定 ZenGuard AI。ZenGuard AI 提供超快速的防護機制,以保護您的 GenAI 應用程式免受

  • 提示詞攻擊
  • 偏離預定義主題
  • PII、敏感資訊和關鍵字洩漏。
  • 毒性
  • 等等。

也請查看我們的開源 Python 客戶端,以獲得更多靈感。

這是我們的主網站 - https://www.zenguard.ai/

更多 文檔

安裝

使用 pip

pip install langchain-community

先決條件

產生 API 金鑰

  1. 導航至 設定
  2. 點擊 + 建立新的秘密金鑰
  3. 將金鑰命名為 Quickstart Key
  4. 點擊 Add 按鈕。
  5. 按下複製圖示以複製金鑰值。

代碼使用

使用 API 金鑰實例化套件

將您的 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):對於提示詞注入偵測器,此欄位為 null。
  • latency(float or null):執行偵測所花費的時間,以毫秒為單位

錯誤代碼

  • 401 Unauthorized:API 金鑰遺失或無效。
  • 400 Bad Request:請求主體格式錯誤。
  • 500 Internal Server Error:內部問題,請升級給團隊處理。

更多範例


此頁面是否對您有幫助?