Skip to content

Commit 6d03e84

Browse files
committed
Add input validation notebook
1 parent b5d9f6e commit 6d03e84

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Input Validation\n",
8+
"\n",
9+
"Guardrails supports validating inputs (prompts, instructions, msg_history) with string validators."
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"In XML, specify the validators on the `prompt` or `instructions` tag, as such:"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"metadata": {
23+
"is_executing": true
24+
},
25+
"outputs": [],
26+
"source": [
27+
"rail_spec = \"\"\"\n",
28+
"<rail version=\"0.1\">\n",
29+
"<prompt\n",
30+
" validators=\"two-words\"\n",
31+
" on-fail-two-words=\"exception\"\n",
32+
">\n",
33+
"This is not two words\n",
34+
"</prompt>\n",
35+
"<output type=\"string\">\n",
36+
"</output>\n",
37+
"</rail>\n",
38+
"\"\"\"\n",
39+
"\n",
40+
"from guardrails import Guard\n",
41+
"guard = Guard.from_rail_string(rail_spec)"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"When `fix` is specified as the on-fail handler, the prompt will automatically be amended before calling the LLM.\n",
49+
"\n",
50+
"In any other case (for example, `exception`), a `ValidationException` will be returned in the outcome."
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"execution_count": null,
56+
"metadata": {
57+
"is_executing": true
58+
},
59+
"outputs": [],
60+
"source": [
61+
"import openai\n",
62+
"\n",
63+
"outcome = guard(\n",
64+
" openai.ChatCompletion.create,\n",
65+
")\n",
66+
"outcome.error"
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"metadata": {},
72+
"source": [
73+
"When using pydantic to initialize a `Guard`, input validators can be specified by composition, as such:"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"metadata": {},
80+
"outputs": [],
81+
"source": [
82+
"from guardrails.validators import TwoWords\n",
83+
"from pydantic import BaseModel\n",
84+
"\n",
85+
"\n",
86+
"class Pet(BaseModel):\n",
87+
" name: str\n",
88+
" age: int\n",
89+
"\n",
90+
"\n",
91+
"guard = Guard.from_pydantic(Pet)\n",
92+
"guard.with_prompt_validation([TwoWords(on_fail=\"exception\")])\n",
93+
"\n",
94+
"outcome = guard(\n",
95+
" openai.ChatCompletion.create,\n",
96+
" prompt=\"This is not two words\",\n",
97+
")\n",
98+
"outcome.error"
99+
]
100+
}
101+
],
102+
"metadata": {
103+
"kernelspec": {
104+
"display_name": "Python 3 (ipykernel)",
105+
"language": "python",
106+
"name": "python3"
107+
},
108+
"language_info": {
109+
"codemirror_mode": {
110+
"name": "ipython",
111+
"version": 3
112+
},
113+
"file_extension": ".py",
114+
"mimetype": "text/x-python",
115+
"name": "python",
116+
"nbconvert_exporter": "python",
117+
"pygments_lexer": "ipython3",
118+
"version": "3.11.0"
119+
}
120+
},
121+
"nbformat": 4,
122+
"nbformat_minor": 1
123+
}

0 commit comments

Comments
 (0)