|
52 | 52 | }, |
53 | 53 | { |
54 | 54 | "cell_type": "code", |
55 | | - "execution_count": 8, |
| 55 | + "execution_count": 7, |
56 | 56 | "metadata": {}, |
57 | 57 | "outputs": [ |
58 | 58 | { |
59 | 59 | "name": "stderr", |
60 | 60 | "output_type": "stream", |
61 | 61 | "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", |
63 | 63 | " warnings.warn(\"get_text_range() call with default params will be implicitly redirected to get_text_bounded()\")\n" |
64 | 64 | ] |
65 | 65 | }, |
|
97 | 97 | ], |
98 | 98 | "source": [ |
99 | 99 | "from guardrails import Guard, docs_utils\n", |
| 100 | + "from guardrails.errors import ValidationError\n", |
100 | 101 | "from rich import print\n", |
101 | 102 | "\n", |
102 | 103 | "content = docs_utils.read_pdf(\"./data/chase_card_agreement.pdf\")\n", |
|
113 | 114 | }, |
114 | 115 | { |
115 | 116 | "cell_type": "code", |
116 | | - "execution_count": 9, |
| 117 | + "execution_count": 2, |
117 | 118 | "metadata": {}, |
118 | 119 | "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 | | - }, |
127 | 120 | { |
128 | 121 | "data": { |
129 | 122 | "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=[])" |
131 | 124 | ] |
132 | 125 | }, |
133 | | - "execution_count": 9, |
| 126 | + "execution_count": 2, |
134 | 127 | "metadata": {}, |
135 | 128 | "output_type": "execute_result" |
136 | 129 | } |
|
154 | 147 | }, |
155 | 148 | { |
156 | 149 | "cell_type": "code", |
157 | | - "execution_count": 10, |
| 150 | + "execution_count": 3, |
158 | 151 | "metadata": {}, |
159 | 152 | "outputs": [], |
160 | 153 | "source": [ |
|
199 | 192 | "def random_response(message, history):\n", |
200 | 193 | " messages = history_to_messages(history)\n", |
201 | 194 | " 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", |
210 | 207 | "\n", |
211 | 208 | "gr.ChatInterface(random_response).launch()" |
212 | 209 | ] |
|
246 | 243 | { |
247 | 244 | "data": { |
248 | 245 | "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", |
266 | 247 | "</pre>\n" |
267 | 248 | ], |
268 | 249 | "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" |
286 | 251 | ] |
287 | 252 | }, |
288 | 253 | "metadata": {}, |
|
291 | 256 | { |
292 | 257 | "data": { |
293 | 258 | "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", |
295 | 260 | "</pre>\n" |
296 | 261 | ], |
297 | 262 | "text/plain": [ |
298 | | - "Last validation status: fail\n" |
| 263 | + "Last validation status: error\n" |
299 | 264 | ] |
300 | 265 | }, |
301 | 266 | "metadata": {}, |
|
313 | 278 | ], |
314 | 279 | "metadata": { |
315 | 280 | "kernelspec": { |
316 | | - "display_name": "Python 3", |
| 281 | + "display_name": "060dev", |
317 | 282 | "language": "python", |
318 | 283 | "name": "python3" |
319 | 284 | }, |
|
327 | 292 | "name": "python", |
328 | 293 | "nbconvert_exporter": "python", |
329 | 294 | "pygments_lexer": "ipython3", |
330 | | - "version": "3.10.4" |
| 295 | + "version": "3.12.3" |
331 | 296 | } |
332 | 297 | }, |
333 | 298 | "nbformat": 4, |
|
0 commit comments