Skip to content

Commit 766d9cc

Browse files
committed
update notebooks
1 parent 6ae7f75 commit 766d9cc

8 files changed

+174
-169
lines changed

docs/examples/chatbot.ipynb

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
},
5353
{
5454
"cell_type": "code",
55-
"execution_count": 8,
55+
"execution_count": 7,
5656
"metadata": {},
5757
"outputs": [
5858
{
5959
"name": "stderr",
6060
"output_type": "stream",
6161
"text": [
62-
"/Users/dtam/.pyenv/versions/3.10.4/lib/python3.10/site-packages/pypdfium2/_helpers/textpage.py:80: UserWarning: get_text_range() call with default params will be implicitly redirected to get_text_bounded()\n",
62+
"/Users/dtam/.pyenv/versions/3.12.3/envs/060dev/lib/python3.12/site-packages/pypdfium2/_helpers/textpage.py:80: UserWarning: get_text_range() call with default params will be implicitly redirected to get_text_bounded()\n",
6363
" warnings.warn(\"get_text_range() call with default params will be implicitly redirected to get_text_bounded()\")\n"
6464
]
6565
},
@@ -97,6 +97,7 @@
9797
],
9898
"source": [
9999
"from guardrails import Guard, docs_utils\n",
100+
"from guardrails.errors import ValidationError\n",
100101
"from rich import print\n",
101102
"\n",
102103
"content = docs_utils.read_pdf(\"./data/chase_card_agreement.pdf\")\n",
@@ -113,24 +114,16 @@
113114
},
114115
{
115116
"cell_type": "code",
116-
"execution_count": 9,
117+
"execution_count": 2,
117118
"metadata": {},
118119
"outputs": [
119-
{
120-
"name": "stderr",
121-
"output_type": "stream",
122-
"text": [
123-
"/Users/dtam/.pyenv/versions/3.10.4/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
124-
" warnings.warn(\n"
125-
]
126-
},
127120
{
128121
"data": {
129122
"text/plain": [
130-
"Guard(id='OWR778', name='ChatBotGuard', description=None, validators=[ValidatorReference(id='guardrails/profanity_free', on='$', on_fail='noop', args=None, kwargs={}), ValidatorReference(id='guardrails/toxic_language', on='$', on_fail='noop', args=None, kwargs={'threshold': 0.5, 'validation_method': 'sentence'})], output_schema=ModelSchema(definitions=None, dependencies=None, anchor=None, ref=None, dynamic_ref=None, dynamic_anchor=None, vocabulary=None, comment=None, defs=None, prefix_items=None, items=None, contains=None, additional_properties=None, properties=None, pattern_properties=None, dependent_schemas=None, property_names=None, var_if=None, then=None, var_else=None, all_of=None, any_of=None, one_of=None, var_not=None, unevaluated_items=None, unevaluated_properties=None, multiple_of=None, maximum=None, exclusive_maximum=None, minimum=None, exclusive_minimum=None, max_length=None, min_length=None, pattern=None, max_items=None, min_items=None, unique_items=None, max_contains=None, min_contains=None, max_properties=None, min_properties=None, required=None, dependent_required=None, const=None, enum=None, type=ValidationType(anyof_schema_1_validator=None, anyof_schema_2_validator=None, actual_instance=<SimpleTypes.STRING: 'string'>, any_of_schemas={'SimpleTypes', 'List[SimpleTypes]'}), title=None, description=None, default=None, deprecated=None, read_only=None, write_only=None, examples=None, format=None, content_media_type=None, content_encoding=None, content_schema=None), history=[])"
123+
"Guard(id='SG816R', name='ChatBotGuard', description=None, validators=[ValidatorReference(id='guardrails/profanity_free', on='$', on_fail='exception', args=None, kwargs={}), ValidatorReference(id='guardrails/toxic_language', on='$', on_fail='exception', args=None, kwargs={'threshold': 0.5, 'validation_method': 'sentence'})], output_schema=ModelSchema(definitions=None, dependencies=None, anchor=None, ref=None, dynamic_ref=None, dynamic_anchor=None, vocabulary=None, comment=None, defs=None, prefix_items=None, items=None, contains=None, additional_properties=None, properties=None, pattern_properties=None, dependent_schemas=None, property_names=None, var_if=None, then=None, var_else=None, all_of=None, any_of=None, one_of=None, var_not=None, unevaluated_items=None, unevaluated_properties=None, multiple_of=None, maximum=None, exclusive_maximum=None, minimum=None, exclusive_minimum=None, max_length=None, min_length=None, pattern=None, max_items=None, min_items=None, unique_items=None, max_contains=None, min_contains=None, max_properties=None, min_properties=None, required=None, dependent_required=None, const=None, enum=None, type=ValidationType(anyof_schema_1_validator=None, anyof_schema_2_validator=None, actual_instance=<SimpleTypes.STRING: 'string'>, any_of_schemas={'List[SimpleTypes]', 'SimpleTypes'}), title=None, description=None, default=None, deprecated=None, read_only=None, write_only=None, examples=None, format=None, content_media_type=None, content_encoding=None, content_schema=None), history=[])"
131124
]
132125
},
133-
"execution_count": 9,
126+
"execution_count": 2,
134127
"metadata": {},
135128
"output_type": "execute_result"
136129
}
@@ -154,7 +147,7 @@
154147
},
155148
{
156149
"cell_type": "code",
157-
"execution_count": 10,
150+
"execution_count": 3,
158151
"metadata": {},
159152
"outputs": [],
160153
"source": [
@@ -199,14 +192,18 @@
199192
"def random_response(message, history):\n",
200193
" messages = history_to_messages(history)\n",
201194
" messages.append({\"role\": \"user\", \"content\": message})\n",
202-
" response = guard(\n",
203-
" model=\"gpt-4o\",\n",
204-
" messages=messages,\n",
205-
" prompt_params={\"document\": content[:6000]},\n",
206-
" temperature=0,\n",
207-
" )\n",
208-
"\n",
209-
" return response.validated_output if response.validation_passed else \"I'm sorry, I can't answer that question.\"\n",
195+
" try:\n",
196+
" response = guard(\n",
197+
" model=\"gpt-4o\",\n",
198+
" messages=messages,\n",
199+
" prompt_params={\"document\": content[:6000]},\n",
200+
" temperature=0,\n",
201+
" )\n",
202+
" except Exception as e:\n",
203+
" if isinstance(e, ValidationError):\n",
204+
" return \"I'm sorry, I can't answer that question.\"\n",
205+
" return \"I'm sorry there was a problem, I can't answer that question.\"\n",
206+
" return response.validated_output\n",
210207
"\n",
211208
"gr.ChatInterface(random_response).launch()"
212209
]
@@ -246,43 +243,11 @@
246243
{
247244
"data": {
248245
"text/html": [
249-
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Raw output: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">\"**INT. DETECTIVE'S OFFICE - NIGHT**\\n\\nThe room is dimly lit, papers scattered across the desk, and a</span>\n",
250-
"<span style=\"color: #008000; text-decoration-color: #008000\">corkboard filled with photos and notes pinned haphazardly. The DETECTIVE, a grizzled man in his late 40s with a </span>\n",
251-
"<span style=\"color: #008000; text-decoration-color: #008000\">five o'clock shadow, paces back and forth, his face a mask of frustration and anger.\\n\\n**DETECTIVE**\\n(voice </span>\n",
252-
"<span style=\"color: #008000; text-decoration-color: #008000\">trembling with rage)\\nDamn it!\\n\\nHe slams his fist onto the desk, causing a coffee mug to topple over and spill </span>\n",
253-
"<span style=\"color: #008000; text-decoration-color: #008000\">its contents. He grabs a file folder, flipping it open only to find it empty. He throws it across the room in a fit</span>\n",
254-
"<span style=\"color: #008000; text-decoration-color: #008000\">of fury.\\n\\n**DETECTIVE**\\n(under his breath, seething)\\nHow the hell did this happen?\\n\\nHe runs his hands through</span>\n",
255-
"<span style=\"color: #008000; text-decoration-color: #008000\">his hair, trying to calm himself but failing miserably. He looks at the corkboard, the photos of crime scenes and </span>\n",
256-
"<span style=\"color: #008000; text-decoration-color: #008000\">suspects now mocking him with their uselessness.\\n\\n**DETECTIVE**\\n(shouting)\\nFuck!\\n\\nHe kicks a chair, sending </span>\n",
257-
"<span style=\"color: #008000; text-decoration-color: #008000\">it skidding across the floor. He takes a deep breath, trying to regain his composure, but the anger is still </span>\n",
258-
"<span style=\"color: #008000; text-decoration-color: #008000\">boiling just beneath the surface.\\n\\n**DETECTIVE**\\n(to himself)\\nAll the evidence... gone. Every damn piece.\\n\\nHe</span>\n",
259-
"<span style=\"color: #008000; text-decoration-color: #008000\">walks over to the window, looking out into the dark, rainy night. The city lights blur through the raindrops on the</span>\n",
260-
"<span style=\"color: #008000; text-decoration-color: #008000\">glass. He clenches his fists, his knuckles turning white.\\n\\n**DETECTIVE**\\n(whispering)\\nWhoever did this... </span>\n",
261-
"<span style=\"color: #008000; text-decoration-color: #008000\">they're gonna pay.\\n\\nHe turns back to the room, his eyes now filled with a cold determination. He grabs his coat </span>\n",
262-
"<span style=\"color: #008000; text-decoration-color: #008000\">from the back of the chair and heads for the door, his mind already racing with plans to track down the </span>\n",
263-
"<span style=\"color: #008000; text-decoration-color: #008000\">thief.\\n\\n**DETECTIVE**\\n(to himself)\\nThis isn't over. Not by a long shot.\\n\\nHe exits the office, the door </span>\n",
264-
"<span style=\"color: #008000; text-decoration-color: #008000\">slamming shut behind him, leaving the room in silence except for the steady drip of the spilled coffee.\\n\\n**FADE </span>\n",
265-
"<span style=\"color: #008000; text-decoration-color: #008000\">OUT.**\"</span><span style=\"font-weight: bold\">]</span>\n",
246+
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Raw output: <span style=\"font-weight: bold\">[</span><span style=\"color: #008000; text-decoration-color: #008000\">'\"Why does everything have to be such a damn mess all the time?\"'</span><span style=\"font-weight: bold\">]</span>\n",
266247
"</pre>\n"
267248
],
268249
"text/plain": [
269-
"Raw output: \u001b[1m[\u001b[0m\u001b[32m\"**INT. DETECTIVE'S OFFICE - NIGHT**\\n\\nThe room is dimly lit, papers scattered across the desk, and a\u001b[0m\n",
270-
"\u001b[32mcorkboard filled with photos and notes pinned haphazardly. The DETECTIVE, a grizzled man in his late 40s with a \u001b[0m\n",
271-
"\u001b[32mfive o'clock shadow, paces back and forth, his face a mask of frustration and anger.\\n\\n**DETECTIVE**\\n\u001b[0m\u001b[32m(\u001b[0m\u001b[32mvoice \u001b[0m\n",
272-
"\u001b[32mtrembling with rage\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\nDamn it!\\n\\nHe slams his fist onto the desk, causing a coffee mug to topple over and spill \u001b[0m\n",
273-
"\u001b[32mits contents. He grabs a file folder, flipping it open only to find it empty. He throws it across the room in a fit\u001b[0m\n",
274-
"\u001b[32mof fury.\\n\\n**DETECTIVE**\\n\u001b[0m\u001b[32m(\u001b[0m\u001b[32munder his breath, seething\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\nHow the hell did this happen?\\n\\nHe runs his hands through\u001b[0m\n",
275-
"\u001b[32mhis hair, trying to calm himself but failing miserably. He looks at the corkboard, the photos of crime scenes and \u001b[0m\n",
276-
"\u001b[32msuspects now mocking him with their uselessness.\\n\\n**DETECTIVE**\\n\u001b[0m\u001b[32m(\u001b[0m\u001b[32mshouting\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\nFuck!\\n\\nHe kicks a chair, sending \u001b[0m\n",
277-
"\u001b[32mit skidding across the floor. He takes a deep breath, trying to regain his composure, but the anger is still \u001b[0m\n",
278-
"\u001b[32mboiling just beneath the surface.\\n\\n**DETECTIVE**\\n\u001b[0m\u001b[32m(\u001b[0m\u001b[32mto himself\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\nAll the evidence... gone. Every damn piece.\\n\\nHe\u001b[0m\n",
279-
"\u001b[32mwalks over to the window, looking out into the dark, rainy night. The city lights blur through the raindrops on the\u001b[0m\n",
280-
"\u001b[32mglass. He clenches his fists, his knuckles turning white.\\n\\n**DETECTIVE**\\n\u001b[0m\u001b[32m(\u001b[0m\u001b[32mwhispering\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\nWhoever did this... \u001b[0m\n",
281-
"\u001b[32mthey're gonna pay.\\n\\nHe turns back to the room, his eyes now filled with a cold determination. He grabs his coat \u001b[0m\n",
282-
"\u001b[32mfrom the back of the chair and heads for the door, his mind already racing with plans to track down the \u001b[0m\n",
283-
"\u001b[32mthief.\\n\\n**DETECTIVE**\\n\u001b[0m\u001b[32m(\u001b[0m\u001b[32mto himself\u001b[0m\u001b[32m)\u001b[0m\u001b[32m\\nThis isn't over. Not by a long shot.\\n\\nHe exits the office, the door \u001b[0m\n",
284-
"\u001b[32mslamming shut behind him, leaving the room in silence except for the steady drip of the spilled coffee.\\n\\n**FADE \u001b[0m\n",
285-
"\u001b[32mOUT.**\"\u001b[0m\u001b[1m]\u001b[0m\n"
250+
"Raw output: \u001b[1m[\u001b[0m\u001b[32m'\"Why does everything have to be such a damn mess all the time?\"'\u001b[0m\u001b[1m]\u001b[0m\n"
286251
]
287252
},
288253
"metadata": {},
@@ -291,11 +256,11 @@
291256
{
292257
"data": {
293258
"text/html": [
294-
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Last validation status: fail\n",
259+
"<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\">Last validation status: error\n",
295260
"</pre>\n"
296261
],
297262
"text/plain": [
298-
"Last validation status: fail\n"
263+
"Last validation status: error\n"
299264
]
300265
},
301266
"metadata": {},
@@ -313,7 +278,7 @@
313278
],
314279
"metadata": {
315280
"kernelspec": {
316-
"display_name": "Python 3",
281+
"display_name": "060dev",
317282
"language": "python",
318283
"name": "python3"
319284
},
@@ -327,7 +292,7 @@
327292
"name": "python",
328293
"nbconvert_exporter": "python",
329294
"pygments_lexer": "ipython3",
330-
"version": "3.10.4"
295+
"version": "3.12.3"
331296
}
332297
},
333298
"nbformat": 4,

docs/examples/data/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
name_case = Guard(
2323
name="name-case", description="Checks that a string is in Name Case format."
24-
).use(RegexMatch(regex="^(?:[A-Z][^\s]*\s?)+$"))
24+
).use(RegexMatch(regex="^(?:[A-Z][^\s]*\s?)+$", on_fail=OnFailAction.NOOP))
2525

2626
all_caps = Guard(
2727
name="all-caps", description="Checks that a string is all capital."
28-
).use(RegexMatch(regex="^[A-Z\\s]*$"))
28+
).use(RegexMatch(regex="^[A-Z\\s]*$", on_fail=OnFailAction.NOOP))
2929

3030

3131
@register_validator(name="custom/dynamic-enum", data_type="all")
@@ -67,4 +67,4 @@ def custom_enum_fetcher(*args):
6767
custom_code_guard = Guard(
6868
name="custom",
6969
description="Uses a custom callable init argument for dynamic enum checks",
70-
).use(DynamicEnum(custom_enum_fetcher))
70+
).use(DynamicEnum(custom_enum_fetcher, on_fail=OnFailAction.NOOP))

docs/examples/extracting_entities.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
"\n",
154154
"class Fee(BaseModel):\n",
155155
" name: str = Field(validators=[LowerCase(on_fail=\"fix\"), TwoWords(on_fail=\"reask\")])\n",
156-
" explanation: str = Field(validators=[OneLine()])\n",
156+
" explanation: str = Field(validators=[OneLine(on_fail=\"noop\")])\n",
157157
" value: float = Field(description=\"The fee amount in USD or as a percentage.\")\n",
158158
"\n",
159159
"class AccountFee(BaseModel):\n",

docs/examples/generate_structured_data.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@
8080
" user_id: str = Field(description=\"The user's id.\")\n",
8181
" user_name: str = Field(\n",
8282
" description=\"The user's first name and last name\",\n",
83-
" validators=[TwoWords()]\n",
83+
" validators=[TwoWords(on_fail=\"noop\")]\n",
8484
" )\n",
8585
" num_orders: int = Field(\n",
8686
" description=\"The number of orders the user has placed\",\n",
87-
" validators=[ValidRange(0, 50)]\n",
87+
" validators=[ValidRange(0, 50, on_fail=\"noop\")]\n",
8888
" )\n",
8989
"\n",
9090
"class Orders(BaseModel):\n",

0 commit comments

Comments
 (0)