{
"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[95mvalid_sql...\u001b[0m\n",
"✅Successfully installed guardrails/valid_sql!\n",
"\n",
"\n"
]
}
],
"source": [
"!guardrails hub install hub://guardrails/valid_sql --quiet"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Natural Language to Bug Free SQL\n",
"\n",
"!!! note\n",
" To download this example as a Jupyter notebook, click [here](https://github.com/guardrails-ai/guardrails/blob/main/docs/examples/syntax_error_free_sql.ipynb).\n",
"\n",
"In this example, we will use Guardrails to generate SQL queries from natural language. We will check that the SQL is free of any syntax errors.\n",
"\n",
"## Objective\n",
"\n",
"We want to generate SQL queries from natural language, and check that the SQL is free of any syntax errors.\n",
"\n",
"## Step 0: Setup\n",
"\n",
"In order to run this example, you will need to install the `sqlvalidator` package. You can do so by running the following command:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"! pip install sqlvalidator -q"
]
},
{
"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 an `output` schema that returns a single key-value pair. The key should be 'generated_sql' and the value should be the SQL query generated from the natural language, which is syntactically correct."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"XML option:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"rail_str = \"\"\"\n",
"
\n",
"\n",
"Generate a valid SQL query for the following natural language instruction:\n",
"\n",
"Select the name of the employee who has the highest salary.\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=\"Generate SQL for the given natural language instruction.\" format=\"guardrails/valid_sql: None\n",
"None\" name=\"generated_sql\" 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",
"Generate a valid SQL query for the following natural language instruction:\n",
"\n",
"Select the name of the employee who has the highest salary.\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