Text-to-SQL
The Text2SQL
module is a demonstration of how to use Guardrails to build a text-to-SQL workflow. It implements necessary components to build a text-to-SQL workflow, including:
- Connects to your database
- Given a DB schema, supports SQL validation and error handling
- Finds relevant few shot examples of
text2SQL
history, and uses them to generate a prompt - Optionally adds modules to guard against unsafe SQL (allowing INSERT, DROP, etc.)
- Supports reasking for all invalid SQL (SQL with syntax errors, SQL targeting non-existent tables, SQL with prohibited keywords, etc.)
Info
The text2SQL
module is intended to showcase what an end-to-end text2SQL
workflow looks like with Guardrails. As a developer, you can either use this module as a starting point for your own text2SQL
workflow or you can use it as a reference for how to use Guardrails to build your own custom text2SQL
workflow.
🎱 Why use Guardrails for text-to-SQL?
bug-free-sql
validators: Guardrails provides abug-free-sql
validator that creates a sandboxed environment to run the generated SQL against your database and check for bugs and errors.- Protection against unsafe SQL: Guardrails allows developers to constrain the generated SQL to a subset of SQL that is safe to run against your database. For example, you can configure Guardrails to only allow
SELECT
statements, and disallowINSERT
,DROP
, etc. - Reasking: Guardrails allows you to configure reasking logic, so that if the generated SQL is invalid or has bugs, you can reask the LLM to generate a new SQL. Guardrails automatically handles the reasking logic for you.
🚀 Quickstart
To get started, you'll need the following:
1. Either a connection string to your database or a database schema
2. (Optional) Dictionary of few shot examples of text2SQL
history
📝 Setup database connection and examples
💡 Use Text2SQL Application
- Sets up sandboxed DB based on your SQL schema / connection string
- Finds most relevant examples and inserts them into the prompt
- Checks that the query is valid for the schema
❎ Demonstration on an incorrect output
Below is a demonstration of how Guardrails' Text2SQL
module handles an incorrect output from the LLM. The LLM generates an SQL that has a syntax error, and Guardrails catches this error and reasks the LLM to generate a new SQL.
The output is incorrect because it refers to a table (departments
) that does not exist in the database. Guardrails catches this error and reasks the LLM to generate a new SQL.
Below, we can see the final corrected output, as well as the underlying logs to see what happened step by step:
⚒️ Customizing the Text2SQL
module
You can customize the Text2SQL
module to fit your needs. Here are some examples of how you can customize the module:
- Prompt: You can customize the
text2SQL
prompt by passing in a customRAIL
spec to the module in therail_spec
argument. - Few shot examples: You can customize how the few shot examples are stored, retrieved, and inserted into the prompt by updating the arguments
vector_db
,document_store
,num_relevant_examples
andexample_formatter
. - Reask prompt: You can customize the reask prompt by updating the
reask_prompt
argument. - LLM API: You can customize the LLM API by updating the
llm_api
argument. To pass in specific kwargs to the LLM API, you can update thellm_api_kwargs
argument (e.g.llm_api_kwargs={'max_length': 100}
).