SQL QnA

Learn how to query structured data


Unlike previous examples like Web Scrape QnA and Multiple Documents QnA, querying structured data does not require a vector database. At the high-level, this can be achieved with following steps:

  1. Providing the LLM:

    • overview of the SQL database schema

    • example rows data

  2. Return a SQL query with few shot prompting

  3. Validate the SQL query using an If Else node

  4. Create a custom function to execute the SQL query, and get the response

  5. Return a natural response from the executed SQL response

In this example, we are going to create a QnA chatbot that can interact with a SQL database stored in SingleStore

TL;DR

You can find the chatflow template:

1. SQL Database Schema + Example Rows

Use a Custom JS Function node to connect to SingleStore, retrieve database schema and top 3 rows.

From the research paper, it is recommended to generate a prompt with following example format:

Full Javascript Code

You can find more on how to get the HOST, USER, PASSWORD from this guide. Once finished, click Execute:

We can now see the correct format has been generated. Next step is to bring this into Prompt Template.

2. Return a SQL query with few shot prompting

Create a new Chat Model + Prompt Template + LLMChain

Specify the following prompt in the Prompt Template:

Since we are using 2 variables: {schema} and {question}, specify their values in Format Prompt Values:

You can provide more examples to the prompt (i.e few-shot prompting) to let the LLM learns better. Or take reference from dialect-specific prompting

3. Validate the SQL query using If Else node

Sometimes the SQL query is invalid, and we do not want to waste resources the execute an invalid SQL query. For example, if a user is asking a general question that is irrelevant to the SQL database. We can use an If Else node to route to different path.

For instance, we can perform a basic check to see if SELECT and WHERE are included in the SQL query given by the LLM.

In the Else Function, we will route to a Prompt Template + LLMChain that basically tells LLM that it is unable to answer user query:

4. Custom function to execute SQL query, and get the response

If it is a valid SQL query, we need to execute the query. Connect the True output from If Else node to a Custom JS Function node:

Full Javascript Code

5. Return a natural response from the executed SQL response

Create a new Chat Model + Prompt Template + LLMChain

Write the following prompt in the Prompt Template:

Specify the variables in Format Prompt Values:

Voila! Your SQL chatbot is now ready for testing!

Query

First, let's ask something related to the database.

Looking at the logs, we can see the first LLMChain is able to give us a SQL query:

Input:

Output

After executing the SQL query, the result is passed to the 2nd LLMChain:

Input

Output

Now, we if ask something that is irrelevant to the SQL database, the Else route is taken.

For first LLMChain, a SQL query is generated as below:

However, it fails the If Else check because it doesn't contains both SELECT and WHERE, hence entering the Else route that has a prompt that says:

And the final output is:

Conclusion

In this example, we have successfully created a SQL chatbot that can interact with your database, and is also able to handle questions that are irrelevant to database. Further improvement includes adding memory to provide conversation history.

You can find the chatflow below:

Last updated

Was this helpful?