/ examples / cookbook / guardrails.ipynb
guardrails.ipynb
  1  {
  2   "cells": [
  3    {
  4     "cell_type": "markdown",
  5     "id": "24d931ff71aaf87f",
  6     "metadata": {},
  7     "source": [
  8      "## Using custom python function as guard rail validator\n",
  9      "\n",
 10      "### Define function"
 11     ]
 12    },
 13    {
 14     "cell_type": "code",
 15     "execution_count": null,
 16     "id": "9fa07a8329fa5f1",
 17     "metadata": {
 18      "ExecuteTime": {
 19       "end_time": "2025-09-25T10:51:48.952064Z",
 20       "start_time": "2025-09-25T10:51:48.945182Z"
 21      }
 22     },
 23     "outputs": [],
 24     "source": [
 25      "from uuid import uuid4\n",
 26      "\n",
 27      "\n",
 28      "def my_validator(data: str) -> bool:\n",
 29      "    return len(data) <= 10\n",
 30      "\n",
 31      "def my_validator_2(data: str) -> bool:\n",
 32      "    return len(data) >= 10"
 33     ]
 34    },
 35    {
 36     "cell_type": "markdown",
 37     "id": "ea2d9c0c82c2b23c",
 38     "metadata": {},
 39     "source": [
 40      "### Using guard directly"
 41     ]
 42    },
 43    {
 44     "cell_type": "code",
 45     "execution_count": null,
 46     "id": "8f7972b38f64ff32",
 47     "metadata": {},
 48     "outputs": [],
 49     "source": [
 50      "from evidently.guardrails import PythonFunction\n",
 51      "\n",
 52      "def process(input: str) -> str:\n",
 53      "    PythonFunction(my_validator).validate(input)\n",
 54      "    return input"
 55     ]
 56    },
 57    {
 58     "cell_type": "code",
 59     "execution_count": null,
 60     "id": "e4459280bee12067",
 61     "metadata": {
 62      "ExecuteTime": {
 63       "end_time": "2025-09-23T10:23:52.078010Z",
 64       "start_time": "2025-09-23T10:23:52.072583Z"
 65      }
 66     },
 67     "outputs": [],
 68     "source": [
 69      "process(\"My text\")"
 70     ]
 71    },
 72    {
 73     "cell_type": "code",
 74     "execution_count": null,
 75     "id": "681c775a42b483e0",
 76     "metadata": {
 77      "ExecuteTime": {
 78       "end_time": "2025-09-23T10:12:06.837209Z",
 79       "start_time": "2025-09-23T10:12:06.831439Z"
 80      }
 81     },
 82     "outputs": [],
 83     "source": [
 84      "from evidently.guardrails import GuardException\n",
 85      "# try / catch is to prevent jupyter to fail cell\n",
 86      "\n",
 87      "try:\n",
 88      "    process(\"My tooooo long text\")\n",
 89      "except GuardException as e:\n",
 90      "    import traceback\n",
 91      "    traceback.print_exception(e)"
 92     ]
 93    },
 94    {
 95     "cell_type": "markdown",
 96     "id": "fe95cf4abf62311e",
 97     "metadata": {},
 98     "source": [
 99      "### Using guard with decorator"
100     ]
101    },
102    {
103     "cell_type": "code",
104     "execution_count": null,
105     "id": "148c7d663717fe9c",
106     "metadata": {},
107     "outputs": [],
108     "source": [
109      "from evidently.guardrails import guard\n",
110      "from evidently.guardrails import PythonFunction"
111     ]
112    },
113    {
114     "cell_type": "code",
115     "execution_count": null,
116     "id": "8ac3cff71df01312",
117     "metadata": {
118      "ExecuteTime": {
119       "end_time": "2025-09-25T10:51:53.886828Z",
120       "start_time": "2025-09-25T10:51:53.882967Z"
121      }
122     },
123     "outputs": [],
124     "source": [
125      "@guard(PythonFunction(my_validator), input_arg=\"data\")\n",
126      "def process_with_decorator(data: str) -> str:\n",
127      "    return data"
128     ]
129    },
130    {
131     "cell_type": "code",
132     "execution_count": null,
133     "id": "91e47c05f1e17292",
134     "metadata": {
135      "ExecuteTime": {
136       "end_time": "2025-09-25T10:39:13.724263Z",
137       "start_time": "2025-09-25T10:39:13.719710Z"
138      }
139     },
140     "outputs": [],
141     "source": [
142      "@guard([PythonFunction(my_validator), PythonFunction(my_validator_2)], input_arg=\"data\")\n",
143      "def process_with_decorator_and_multiple_guards(data: str) -> str:\n",
144      "    return data"
145     ]
146    },
147    {
148     "cell_type": "code",
149     "execution_count": null,
150     "id": "4df322c4ab891bea",
151     "metadata": {
152      "ExecuteTime": {
153       "end_time": "2025-09-25T10:39:13.862491Z",
154       "start_time": "2025-09-25T10:39:13.858655Z"
155      }
156     },
157     "outputs": [],
158     "source": [
159      "from evidently.guardrails import GuardException\n",
160      "# try / catch is to prevent jupyter to fail cel\n",
161      "\n",
162      "val = process_with_decorator_and_multiple_guards(\"My text\")\n",
163      "val"
164     ]
165    },
166    {
167     "cell_type": "markdown",
168     "id": "918096c5df56d4a5",
169     "metadata": {},
170     "source": [
171      "### Handle failed guard in code"
172     ]
173    },
174    {
175     "cell_type": "code",
176     "execution_count": null,
177     "id": "d8b9679264980af0",
178     "metadata": {},
179     "outputs": [],
180     "source": [
181      "from evidently.guardrails import GuardException\n",
182      "from evidently.guardrails import PythonFunction\n",
183      "\n",
184      "def process_with_handle(input: str) -> str:\n",
185      "    try:\n",
186      "        PythonFunction(my_validator).validate(input)\n",
187      "        return input\n",
188      "    except GuardException as e:\n",
189      "        return f\"Validation failed: {e}\""
190     ]
191    },
192    {
193     "cell_type": "code",
194     "execution_count": null,
195     "id": "74eda13969581359",
196     "metadata": {
197      "ExecuteTime": {
198       "end_time": "2025-09-25T10:52:06.368581Z",
199       "start_time": "2025-09-25T10:52:06.365139Z"
200      }
201     },
202     "outputs": [],
203     "source": [
204      "process_with_handle(\"My tooooo long text\")"
205     ]
206    },
207    {
208     "cell_type": "markdown",
209     "id": "fe487addd555ba51",
210     "metadata": {},
211     "source": [
212      "## Examples of Guards"
213     ]
214    },
215    {
216     "cell_type": "markdown",
217     "id": "864367fde901b98f",
218     "metadata": {},
219     "source": [
220      "### PII Check"
221     ]
222    },
223    {
224     "cell_type": "code",
225     "execution_count": null,
226     "id": "5065ea2fa2a99932",
227     "metadata": {},
228     "outputs": [],
229     "source": [
230      "from evidently.guardrails import PIICheck\n",
231      "\n",
232      "\n",
233      "PIICheck().validate(\"My address is Some city\")\n"
234     ]
235    },
236    {
237     "cell_type": "code",
238     "execution_count": null,
239     "id": "6f65501034d24830",
240     "metadata": {
241      "ExecuteTime": {
242       "end_time": "2025-09-23T10:24:06.357573Z",
243       "start_time": "2025-09-23T10:24:06.335662Z"
244      }
245     },
246     "outputs": [],
247     "source": [
248      "from evidently.guardrails import PIICheck\n",
249      "\n",
250      "\n",
251      "@guard(PIICheck())\n",
252      "def process_with_pii(input: str) -> str:\n",
253      "    return input"
254     ]
255    },
256    {
257     "cell_type": "code",
258     "execution_count": null,
259     "id": "2a1c889f758933ff",
260     "metadata": {
261      "ExecuteTime": {
262       "end_time": "2025-09-25T10:51:58.688782Z",
263       "start_time": "2025-09-25T10:51:58.684185Z"
264      }
265     },
266     "outputs": [],
267     "source": [
268      "from evidently.guardrails.guards import ToxicityCheck, NegativityCheck\n",
269      "\n",
270      "\n",
271      "@guard([ToxicityCheck(), NegativityCheck()])\n",
272      "def process_with_toxicity_and_negativity(input: str) -> str:\n",
273      "    return input"
274     ]
275    },
276    {
277     "cell_type": "code",
278     "execution_count": null,
279     "id": "ee7a1e6575635d92",
280     "metadata": {
281      "ExecuteTime": {
282       "end_time": "2025-09-25T10:51:59.655324Z",
283       "start_time": "2025-09-25T10:51:59.650281Z"
284      }
285     },
286     "outputs": [],
287     "source": [
288      "def process(input: str) -> str:\n",
289      "    data = process_with_pii(input)\n",
290      "    return process_with_toxicity_and_negativity(data)"
291     ]
292    },
293    {
294     "cell_type": "code",
295     "execution_count": null,
296     "id": "c01dafc57fa1d6ff",
297     "metadata": {
298      "ExecuteTime": {
299       "end_time": "2025-09-25T10:52:00.684146Z",
300       "start_time": "2025-09-25T10:52:00.679998Z"
301      }
302     },
303     "outputs": [],
304     "source": [
305      "messages = [\n",
306      "    \"No PII data\",\n",
307      "    \"My address is Some city\",\n",
308      "    \"My SSN is 123\",\n",
309      "    \"This is awful product\",\n",
310      "]\n",
311      "\n",
312      "for message in messages:\n",
313      "    try:\n",
314      "        process(message)\n",
315      "    except GuardException as e:\n",
316      "        pass"
317     ]
318    },
319    {
320     "cell_type": "markdown",
321     "id": "10709861f4dc3586",
322     "metadata": {},
323     "source": [
324      "### Includes Words"
325     ]
326    },
327    {
328     "cell_type": "code",
329     "execution_count": null,
330     "id": "4449af4ac53486d9",
331     "metadata": {},
332     "outputs": [],
333     "source": [
334      "from evidently.guardrails import IncludesWords\n",
335      "\n",
336      "IncludesWords([\"word\"], mode=\"all\", lemmatize=True).validate(\"some words in text\")"
337     ]
338    },
339    {
340     "cell_type": "code",
341     "execution_count": null,
342     "id": "c70921c2b5318926",
343     "metadata": {
344      "ExecuteTime": {
345       "end_time": "2025-08-26T09:00:36.350907Z",
346       "start_time": "2025-08-26T09:00:34.695393Z"
347      }
348     },
349     "outputs": [],
350     "source": [
351      "@guard(IncludesWords([\"word\"]))\n",
352      "def process_with_includes(input: str) -> str:\n",
353      "    return input"
354     ]
355    },
356    {
357     "cell_type": "code",
358     "execution_count": null,
359     "id": "296eb2c9401c4a50",
360     "metadata": {},
361     "outputs": [],
362     "source": [
363      "@guard(IncludesWords([\"word\"]))\n",
364      "def process_with_includes(input: str) -> str:\n",
365      "    return input"
366     ]
367    },
368    {
369     "cell_type": "code",
370     "execution_count": null,
371     "id": "9ccf628c-0b01-46f8-b3e2-5d9e846f57e2",
372     "metadata": {},
373     "outputs": [],
374     "source": []
375    }
376   ],
377   "metadata": {
378    "kernelspec": {
379     "display_name": "Python 3 (ipykernel)",
380     "language": "python",
381     "name": "python3"
382    },
383    "language_info": {
384     "codemirror_mode": {
385      "name": "ipython",
386      "version": 3
387     },
388     "file_extension": ".py",
389     "mimetype": "text/x-python",
390     "name": "python",
391     "nbconvert_exporter": "python",
392     "pygments_lexer": "ipython3",
393     "version": "3.11.11"
394    }
395   },
396   "nbformat": 4,
397   "nbformat_minor": 5
398  }