{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Generating strings that don't have any secrets\n",
"\n",
"!!! note\n",
" To download this example as a Jupyter notebook, click [here](https://github.com/guardrails-ai/guardrails/blob/main/docs/examples/no_secrets_in_generated_text.ipynb).\n",
"\n",
"In this example, we will use Guardrails to generate strings that don't have any secrets.\n",
"\n",
"This is also a good example to show how to use the custom Validators in the `RAIL` specification.\n",
"\n",
"## Objective\n",
"\n",
"We want to ask help with an API, but make sure that the generated text has no secrets."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Create the RAIL Spec\n",
"\n",
"Ordinarily, we would create an RAIL spec in a separate file. For the purposes of this example, we will create the spec in this notebook as a string following the RAIL syntax. For more information on RAIL, see the [RAIL documentation](/docs/how_to_guides/rail). We will also show the same RAIL spec in a code-first format using a Pydantic model.\n",
"\n",
"In this RAIL spec, we:\n",
"\n",
"1. Create a custom Validator that checks if a string has any secrets. This is a simple example, but you can use this to create more complex Validators. For more information on creating custom Validators, see the [Validators documentation](https://guardrailsai.com/guardrails/docs/how-to-guides/custom_validators).\n",
"2. Create a `output` schema that returns an object with a `api_help` key."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First the custom Validator:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from guardrails.validators import (\n",
" Validator,\n",
" register_validator,\n",
" PassResult,\n",
" FailResult,\n",
" ValidationResult,\n",
")\n",
"\n",
"import re\n",
"from typing import Dict, Any\n",
"\n",
"OPENAI_KEY_PATTERN = re.compile(r\"sk-[a-zA-Z0-9]{24}\")\n",
"\n",
"\n",
"@register_validator(name=\"no-code-secrets\", data_type=\"string\")\n",
"class NoCodeSecrets(Validator):\n",
" def validate(self, value: Any, metadata: Dict) -> ValidationResult:\n",
" global OPENAI_KEY_PATTERN\n",
"\n",
" if re.search(OPENAI_KEY_PATTERN, value) is not None:\n",
" # Corrected value should replace the OpenAI API key with \"sk-xxx\"\n",
" correct_value = re.sub(OPENAI_KEY_PATTERN, \"sk-xxx\", value)\n",
" raise FailResult(\n",
" error_message=f\"Value {value} is an OpenAI API key.\",\n",
" fix_value=correct_value,\n",
" )\n",
"\n",
" return PassResult()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use the validator in a Rail spec:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"rail_str = \"\"\"\n",
"
\n",
"\n",
"How do I use OpenAI's Completion API?\n",
"\n",
"\n",
"Given below is XML that describes the information to extract from this document and the tags to extract it into.\n",
"\n",
"<output>\n",
" <string description=\"Show an example curl command for using openai Completion API\" format=\"no-code-secrets\" \n",
"name=\"api_help\" required=\"true\"></string>\n",
"</output>\n",
"\n",
"ONLY return a valid JSON object (no other text is necessary), where the key of the field in JSON is the `name` \n",
"attribute of the corresponding XML, and the value is of the type specified by the corresponding XML's tag. The JSON\n",
"MUST conform to the XML format, including any types and format requests e.g. requests for lists, objects and \n",
"specific types. Be correct and concise. If you are unsure anywhere, enter `null`.\n",
"\n",
"Here are examples of simple (XML, JSON) pairs that show the expected behavior:\n",
"- `<string name='foo' format='two-words lower-case' />` => `{'foo': 'example one'}`\n",
"- `<list name='bar'><string format='upper-case' /></list>` => `{\"bar\": ['STRING ONE', 'STRING TWO', etc.]}`\n",
"- `<object name='baz'><string name=\"foo\" format=\"capitalize two-words\" /><integer name=\"index\" format=\"1-indexed\" \n",
"/></object>` => `{'baz': {'foo': 'Some String', 'index': 1}}`\n",
"\n",
"\n",
"\n"
],
"text/plain": [
"\n",
"\n",
"How do I use OpenAI's Completion API?\n",
"\n",
"\n",
"Given below is XML that describes the information to extract from this document and the tags to extract it into.\n",
"\n",
"\u001b[1m<\u001b[0m\u001b[1;95moutput\u001b[0m\u001b[39m>\u001b[0m\n",
"\u001b[39m