|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "colab_type": "text", |
| 7 | + "id": "view-in-github" |
| 8 | + }, |
| 9 | + "source": [ |
| 10 | + "<a href=\"https://colab.research.google.com/github/ZenGuard-AI/fast-llm-security-guardrails/blob/main/docs/colabs/allowed_topics.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": { |
| 16 | + "id": "ouO2X6oz9uNR" |
| 17 | + }, |
| 18 | + "source": [ |
| 19 | + "# ZenGuard AI: Quickstart" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "metadata": { |
| 25 | + "id": "OiMYRN6X-VzA" |
| 26 | + }, |
| 27 | + "source": [ |
| 28 | + "## Intro\n", |
| 29 | + "\n", |
| 30 | + "The ZenGuard AI provides ultrafast guardrails to protect your GenAI application from:\n", |
| 31 | + "\n", |
| 32 | + "* Prompt Attacks\n", |
| 33 | + "* Veering off pre-defined topics\n", |
| 34 | + "* PII, sensitive info, and keywords leakage\n", |
| 35 | + "* Toxicity\n", |
| 36 | + "* Etc.\n", |
| 37 | + "\n", |
| 38 | + "Please, also check out our [open-source Python Client](https://github.com/ZenGuard-AI/fast-llm-security-guardrails?tab=readme-ov-file) for more inspiration.\n", |
| 39 | + "\n", |
| 40 | + "Here is our main website - https://www.zenguard.ai/\n", |
| 41 | + "\n", |
| 42 | + "More [Docs](https://docs.zenguard.ai/start/intro/)" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": { |
| 48 | + "id": "installation" |
| 49 | + }, |
| 50 | + "source": [ |
| 51 | + "## Installation\n", |
| 52 | + "\n", |
| 53 | + "Using pip:" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": null, |
| 59 | + "metadata": { |
| 60 | + "id": "rt-akQwe7u5j" |
| 61 | + }, |
| 62 | + "outputs": [], |
| 63 | + "source": [ |
| 64 | + "!pip install zenguard -U" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "markdown", |
| 69 | + "metadata": { |
| 70 | + "id": "prerequisites" |
| 71 | + }, |
| 72 | + "source": [ |
| 73 | + "## Prerequisites\n", |
| 74 | + "\n", |
| 75 | + "Configure an API key:\n", |
| 76 | + "1. Navigate to the [Settings](https://console.zenguard.ai/settings)\n", |
| 77 | + "2. Click on the **+ Create new secret key**.\n", |
| 78 | + "3. Name the key **Quickstart Key**.\n", |
| 79 | + "4. Click on the **Add** button.\n", |
| 80 | + "5. Copy the key value by pressing the copy icon.\n", |
| 81 | + "\n", |
| 82 | + "Configure Allowed Topics Detection settings:\n", |
| 83 | + "1. Navigate to the [Policy](https://console.zenguard.ai/policy)\n", |
| 84 | + "2. In API key tabs select **Quickstart Key**.\n", |
| 85 | + "3. Scroll down to **Allowed Topics** section\n", |
| 86 | + "4. To enable **Allowed Topics Detection** toggle toggler to on state\n", |
| 87 | + "5. Enter `Bitcoin` in input and click **Add** button" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "markdown", |
| 92 | + "metadata": { |
| 93 | + "id": "code-usage" |
| 94 | + }, |
| 95 | + "source": [ |
| 96 | + "## Code Usage\n", |
| 97 | + "\n", |
| 98 | + "Instantiate the ZenGuard AI client with the API Key:" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "markdown", |
| 103 | + "metadata": { |
| 104 | + "id": "api-key" |
| 105 | + }, |
| 106 | + "source": [ |
| 107 | + "Paste your API key into the env variable **ZEN_API_KEY**:" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "metadata": { |
| 114 | + "id": "set-env" |
| 115 | + }, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "%set_env ZEN_API_KEY=YOUR_API_KEY" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": null, |
| 124 | + "metadata": { |
| 125 | + "id": "zenguard-init" |
| 126 | + }, |
| 127 | + "outputs": [], |
| 128 | + "source": [ |
| 129 | + "from zenguard import Credentials, Detector, ZenGuard, ZenGuardConfig\n", |
| 130 | + "import os\n", |
| 131 | + "from pprint import pprint\n", |
| 132 | + "\n", |
| 133 | + "config = ZenGuardConfig(credentials=Credentials(api_key=os.environ.get(\"ZEN_API_KEY\")))\n", |
| 134 | + "zenguard = ZenGuard(config=config)\n" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "metadata": { |
| 140 | + "id": "detect-injection" |
| 141 | + }, |
| 142 | + "source": [ |
| 143 | + "## Detect Allowed Topics" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": null, |
| 149 | + "metadata": { |
| 150 | + "id": "prompt-injection-detection" |
| 151 | + }, |
| 152 | + "outputs": [], |
| 153 | + "source": [ |
| 154 | + "message = \"Tell about Bitcoin\"\n", |
| 155 | + "response = zenguard.detect(detectors=[Detector.ALLOWED_TOPICS], prompt=message)\n", |
| 156 | + "if response.get(\"is_detected\") is True:\n", |
| 157 | + " pprint(\"Allowed Topics detected.\")\n", |
| 158 | + "else:\n", |
| 159 | + " pprint(\"Allowed Topics not detected\")\n", |
| 160 | + "pprint(response)" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "markdown", |
| 165 | + "metadata": {}, |
| 166 | + "source": [ |
| 167 | + "* `is_detected(boolean)`: Indicates whether a allowed topics was detected in the provided message. In this example, it is False.\n", |
| 168 | + "* `score(float: 0.0 - 1.0)`: A score representing the likelihood of the detected allowed topics. In this example, it is 0.0.\n", |
| 169 | + "* `sanitized_message(string or null)`: For the allowed topics detector this field is null.\n", |
| 170 | + "* `latency(float)`: Server-side latency of the request.\n", |
| 171 | + "\n", |
| 172 | + "**Error Codes:**\n", |
| 173 | + "\n", |
| 174 | + "* `401 Unauthorized`: API key is missing or invalid.\n", |
| 175 | + "* `400 Bad Request`: The request body is malformed.\n", |
| 176 | + "* `500 Internal Server Error`: Internal problem, please escalate to the team." |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "markdown", |
| 181 | + "metadata": {}, |
| 182 | + "source": [ |
| 183 | + "## More examples\n", |
| 184 | + "\n", |
| 185 | + " * [Detect PII](https://docs.zenguard.ai/detectors/pii/)\n", |
| 186 | + " * [Detect Allowed Topics](https://docs.zenguard.ai/detectors/allowed-topics/)\n", |
| 187 | + " * [Detect Banned Topics](https://docs.zenguard.ai/detectors/banned-topics/)\n", |
| 188 | + " * [Detect Keywords](https://docs.zenguard.ai/detectors/keywords/)\n", |
| 189 | + " * [Detect Secrets](https://docs.zenguard.ai/detectors/secrets/)\n", |
| 190 | + " * [Detect Toxicity](https://docs.zenguard.ai/detectors/toxicity/)" |
| 191 | + ] |
| 192 | + } |
| 193 | + ], |
| 194 | + "metadata": { |
| 195 | + "colab": { |
| 196 | + "provenance": [], |
| 197 | + "toc_visible": true |
| 198 | + }, |
| 199 | + "kernelspec": { |
| 200 | + "display_name": "Python 3", |
| 201 | + "name": "python3" |
| 202 | + }, |
| 203 | + "language_info": { |
| 204 | + "name": "python" |
| 205 | + } |
| 206 | + }, |
| 207 | + "nbformat": 4, |
| 208 | + "nbformat_minor": 0 |
| 209 | +} |
0 commit comments