171 lines
4.0 KiB
Plaintext
171 lines
4.0 KiB
Plaintext
|
|
{
|
||
|
|
"cells": [
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 1,
|
||
|
|
"metadata": {},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"Installing hub:\u001b[35m/\u001b[0m\u001b[35m/guardrails/\u001b[0m\u001b[95mtwo_words...\u001b[0m\n",
|
||
|
|
"✅Successfully installed guardrails/two_words version \u001b[1;36m0.0\u001b[0m.\u001b[1;36m0\u001b[0m!\n",
|
||
|
|
"\n",
|
||
|
|
"\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"! guardrails hub install hub://guardrails/two_words --quiet"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"## Input Validation\n",
|
||
|
|
"\n",
|
||
|
|
"Guardrails supports validating inputs (messages) with string validators."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"In XML, specify the validators on the `messages` tag, as such:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 2,
|
||
|
|
"metadata": {
|
||
|
|
"is_executing": true
|
||
|
|
},
|
||
|
|
"outputs": [],
|
||
|
|
"source": [
|
||
|
|
"from guardrails import Guard\n",
|
||
|
|
"\n",
|
||
|
|
"rail_spec = \"\"\"\n",
|
||
|
|
"<rail version=\"0.1\">\n",
|
||
|
|
"<messages\n",
|
||
|
|
" validators=\"hub://guardrails/two_words\"\n",
|
||
|
|
" on-fail-two-words=\"exception\"\n",
|
||
|
|
">\n",
|
||
|
|
"<message role=\"user\">\n",
|
||
|
|
"This is not two words\n",
|
||
|
|
"</message>\n",
|
||
|
|
"</messages>\n",
|
||
|
|
"\n",
|
||
|
|
"<output type=\"string\">\n",
|
||
|
|
"</output>\n",
|
||
|
|
"</rail>\n",
|
||
|
|
"\"\"\"\n",
|
||
|
|
"\n",
|
||
|
|
"guard = Guard.for_rail_string(rail_spec)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"When `fix` is specified as the on-fail handler, the prompt will automatically be amended before calling the LLM.\n",
|
||
|
|
"\n",
|
||
|
|
"In any other case (for example, `exception`), a `ValidationError` will be returned in the outcome."
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 3,
|
||
|
|
"metadata": {
|
||
|
|
"is_executing": true
|
||
|
|
},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stderr",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"/Users/calebcourier/Projects/guardrails/docs/.venv/lib/python3.10/site-packages/guardrails/validator_service/__init__.py:75: UserWarning: Could not obtain an event loop. Falling back to synchronous validation.\n",
|
||
|
|
" warnings.warn(\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"from guardrails.errors import ValidationError\n",
|
||
|
|
"\n",
|
||
|
|
"# Add your OPENAI_API_KEY as an environment variable if it's not already set\n",
|
||
|
|
"# import os\n",
|
||
|
|
"# os.environ[\"OPENAI_API_KEY\"] = \"YOUR_API_KEY\"\n",
|
||
|
|
"\n",
|
||
|
|
"try:\n",
|
||
|
|
" guard(model=\"gpt-4o\")\n",
|
||
|
|
"except ValidationError as e:\n",
|
||
|
|
" print(e)"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "markdown",
|
||
|
|
"metadata": {},
|
||
|
|
"source": [
|
||
|
|
"When using pydantic to initialize a `Guard`, input validators can be specified by composition, as such:"
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
"cell_type": "code",
|
||
|
|
"execution_count": 4,
|
||
|
|
"metadata": {},
|
||
|
|
"outputs": [
|
||
|
|
{
|
||
|
|
"name": "stdout",
|
||
|
|
"output_type": "stream",
|
||
|
|
"text": [
|
||
|
|
"Validation failed for field with errors: Value must be exactly two words\n"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"source": [
|
||
|
|
"from guardrails.hub import TwoWords\n",
|
||
|
|
"from pydantic import BaseModel\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"class Pet(BaseModel):\n",
|
||
|
|
" name: str\n",
|
||
|
|
" age: int\n",
|
||
|
|
"\n",
|
||
|
|
"\n",
|
||
|
|
"guard = Guard.for_pydantic(Pet)\n",
|
||
|
|
"guard.use(TwoWords(on_fail=\"exception\"), on=\"messages\")\n",
|
||
|
|
"\n",
|
||
|
|
"try:\n",
|
||
|
|
" guard(\n",
|
||
|
|
" model=\"gpt-4o\",\n",
|
||
|
|
" messages=[{\"role\": \"user\", \"content\": \"This is not two words\"}],\n",
|
||
|
|
" )\n",
|
||
|
|
"except ValidationError as e:\n",
|
||
|
|
" print(e)"
|
||
|
|
]
|
||
|
|
}
|
||
|
|
],
|
||
|
|
"metadata": {
|
||
|
|
"kernelspec": {
|
||
|
|
"display_name": ".venv (3.10.16)",
|
||
|
|
"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.10.16"
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"nbformat": 4,
|
||
|
|
"nbformat_minor": 1
|
||
|
|
}
|