{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chatbot\n", "\n", "Guardrails can easily be integrated into flows for chatbots to help protect against common unwanted output like profanity and toxic language. \n", "\n", "## Setup\n", "As a prequisite we install the necessary validators from the Hub and gradio which we will integrate with for a interface." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mprofanity_free...\u001b[0m\n", "✅Successfully installed guardrails/profanity_free!\n", "\n", "\n", "Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mtoxic_language...\u001b[0m\n", "✅Successfully installed guardrails/toxic_language!\n", "\n", "\n" ] } ], "source": [ "! guardrails hub install hub://guardrails/profanity_free --quiet\n", "! guardrails hub install hub://guardrails/toxic_language --quiet\n", "! pip install -q gradio" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 0 Download PDF and load it as string\n", ":::note\n", " To download this example as a Jupyter notebook, click [here](https://github.com/guardrails-ai/guardrails/blob/main/docs/examples/chatbots.ipynb).\n", ":::\n", "\n", "In this example, we will set up Guardrails with a chat model that can answer questions about the card agreement." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/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", " warnings.warn(\"get_text_range() call with default params will be implicitly redirected to get_text_bounded()\")\n" ] }, { "data": { "text/html": [ "
Chase Credit Card Document:\n",
"\n",
"2/25/23, 7:59 PM about:blank\n",
"about:blank 1/4\n",
"PRICING INFORMATION\n",
"INTEREST RATES AND INTEREST CHARGES\n",
"Purchase Annual\n",
"Percentage Rate (APR) 0% Intro APR for the first 18 months that your Account is open.\n",
"After that, 19.49%. This APR will vary with the market based on the Prim\n",
"...\n",
"\n"
],
"text/plain": [
"Chase Credit Card Document:\n",
"\n",
"\u001b[1;36m2\u001b[0m/\u001b[1;36m25\u001b[0m/\u001b[1;36m23\u001b[0m, \u001b[1;92m7:59\u001b[0m PM about:blank\n",
"about:blank \u001b[1;36m1\u001b[0m/\u001b[1;36m4\u001b[0m\n",
"PRICING INFORMATION\n",
"INTEREST RATES AND INTEREST CHARGES\n",
"Purchase Annual\n",
"Percentage Rate \u001b[1m(\u001b[0mAPR\u001b[1m)\u001b[0m \u001b[1;36m0\u001b[0m% Intro APR for the first \u001b[1;36m18\u001b[0m months that your Account is open.\n",
"After that, \u001b[1;36m19.49\u001b[0m%. This APR will vary with the market based on the Prim\n",
"\u001b[33m...\u001b[0m\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from guardrails import Guard, docs_utils\n",
"from guardrails.errors import ValidationError\n",
"from rich import print\n",
"\n",
"content = docs_utils.read_pdf(\"./data/chase_card_agreement.pdf\")\n",
"print(f\"Chase Credit Card Document:\\n\\n{content[:275]}\\n...\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 1 Inititalize Guard\n",
"The guard will execute llm calls and ensure the response meets the requirements of the model and its validation."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"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=Raw output: ['\"Why does everything have to be such a damn mess all the time?\"']\n", "\n" ], "text/plain": [ "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" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
Last validation status: error\n",
"\n"
],
"text/plain": [
"Last validation status: error\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"if guard.history.last:\n",
" print(f\"Raw output: {guard.history.last.raw_outputs}\")\n",
" print(f\"Last validation status: {guard.history.last.status}\")\n",
"else:\n",
" print(\"No history yet.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "060dev",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}