# SQL QnA

***

Unlike previous examples like [Web Scrape QnA](/readme/use-cases/web-scrape-qna.md) and [Multiple Documents QnA](/readme/use-cases/multiple-documents-qna.md), 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](/readme/utilities/if-else.md) 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

<figure><img src="/files/NBcmmvrUQrsuUNaYnDR0" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/FXrIl9CvGrGq4X5YOuR0" alt=""><figcaption></figcaption></figure>

## TL;DR

You can find the chatflow template:

{% file src="/files/u4nP1VOUkoC1m1jICylq" %}

## 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](https://arxiv.org/abs/2204.00498), it is recommended to generate a prompt with following example format:

```
CREATE TABLE samples (firstName varchar NOT NULL, lastName varchar)
SELECT * FROM samples LIMIT 3
firstName lastName
Stephen Tyler
Jack McGinnis
Steven Repici
```

<figure><img src="/files/M69coo0ODo6m0RgD9ZpS" alt=""><figcaption></figcaption></figure>

<details>

<summary>Full Javascript Code</summary>

```javascript
const HOST = 'singlestore-host.com';
const USER = 'admin';
const PASSWORD = 'mypassword';
const DATABASE = 'mydb';
const TABLE = 'samples';
const mysql = require('mysql2/promise');

let sqlSchemaPrompt;

function getSQLPrompt() {
  return new Promise(async (resolve, reject) => {
    try {
      const singleStoreConnection = mysql.createPool({
        host: HOST,
        user: USER,
        password: PASSWORD,
        database: DATABASE,
      });
  
      // Get schema info
      const [schemaInfo] = await singleStoreConnection.execute(
        `SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = "${TABLE}"`
      );
  
      const createColumns = [];
      const columnNames = [];
  
      for (const schemaData of schemaInfo) {
        columnNames.push(`${schemaData['COLUMN_NAME']}`);
        createColumns.push(`${schemaData['COLUMN_NAME']} ${schemaData['COLUMN_TYPE']} ${schemaData['IS_NULLABLE'] === 'NO' ? 'NOT NULL' : ''}`);
      }
  
      const sqlCreateTableQuery = `CREATE TABLE samples (${createColumns.join(', ')})`;
      const sqlSelectTableQuery = `SELECT * FROM samples LIMIT 3`;
  
      // Get first 3 rows
      const [rows] = await singleStoreConnection.execute(
          sqlSelectTableQuery,
      );
      
      const allValues = [];
      for (const row of rows) {
          const rowValues = [];
          for (const colName in row) {
              rowValues.push(row[colName]);
          }
          allValues.push(rowValues.join(' '));
      }
  
      sqlSchemaPrompt = sqlCreateTableQuery + '\n' + sqlSelectTableQuery + '\n' + columnNames.join(' ') + '\n' + allValues.join('\n');
      
      resolve();
    } catch (e) {
      console.error(e);
      return reject(e);
    }
  });
}

async function main() {
    await getSQLPrompt();
}

await main();

return sqlSchemaPrompt;
```

</details>

You can find more on how to get the `HOST`, `USER`, `PASSWORD` from this [guide](https://github.com/innovativeSol/tailwinds-docs/blob/main/use-cases/broken-reference/README.md). Once finished, click Execute:

<figure><img src="/files/9Hp42bqkcPVRpZmLIItN" alt=""><figcaption></figcaption></figure>

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

<figure><img src="/files/bzPAqzYSwo6QRaD4VyHt" alt=""><figcaption></figcaption></figure>

Specify the following prompt in the Prompt Template:

```
Based on the provided SQL table schema and question below, return a SQL SELECT ALL query that would answer the user's question. For example: SELECT * FROM table WHERE id = '1'.
------------
SCHEMA: {schema}
------------
QUESTION: {question}
------------
SQL QUERY:
```

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

<figure><img src="/files/2SlMabxz2NJInw62hwlI" alt="" width="563"><figcaption></figcaption></figure>

{% hint style="info" %}
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](https://js.langchain.com/docs/use_cases/sql/prompting#dialect-specific-prompting)
{% endhint %}

## 3. Validate the SQL query using [If Else](/readme/utilities/if-else.md) 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.

{% tabs %}
{% tab title="If Function" %}

```javascript
const sqlQuery = $sqlQuery.trim();

if (sqlQuery.includes("SELECT") && sqlQuery.includes("WHERE")) {
    return sqlQuery;
}
```

{% endtab %}

{% tab title="Else Function" %}

```javascript
return $sqlQuery;
```

{% endtab %}
{% endtabs %}

<figure><img src="/files/hpv5Pr2w3wiadAaVbC48" alt="" width="327"><figcaption></figcaption></figure>

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

<figure><img src="/files/boqgOQy8wseBdfkACFQb" alt=""><figcaption></figcaption></figure>

## 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:

<figure><img src="/files/IwlOOPk9pMbXlRIDgwap" alt="" width="563"><figcaption></figcaption></figure>

<details>

<summary>Full Javascript Code</summary>

```javascript
const HOST = 'singlestore-host.com';
const USER = 'admin';
const PASSWORD = 'mypassword';
const DATABASE = 'mydb';
const TABLE = 'samples';
const mysql = require('mysql2/promise');

let result;

function getSQLResult() {
  return new Promise(async (resolve, reject) => {
    try {
      const singleStoreConnection = mysql.createPool({
        host: HOST,
        user: USER,
        password: PASSWORD,
        database: DATABASE,
      });
     
      const [rows] = await singleStoreConnection.execute(
        $sqlQuery
      );
  
      result = JSON.stringify(rows)
      
      resolve();
    } catch (e) {
      console.error(e);
      return reject(e);
    }
  });
}

async function main() {
    await getSQLResult();
}

await main();

return result;
```

</details>

## 5. Return a natural response from the executed SQL response

Create a new Chat Model + Prompt Template + LLMChain

<figure><img src="/files/7B4p8IvnnJKYW9saB3lt" alt=""><figcaption></figcaption></figure>

Write the following prompt in the Prompt Template:

```
Based on the question, and SQL response, write a natural language response, be details as possible:
------------
QUESTION: {question}
------------
SQL RESPONSE: {sqlResponse}
------------
NATURAL LANGUAGE RESPONSE:
```

Specify the variables in **Format Prompt Values**:

<figure><img src="/files/PfBQEv46Yyj4M4uTZgch" alt="" width="563"><figcaption></figcaption></figure>

Voila! Your SQL chatbot is now ready for testing!

## Query

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

<figure><img src="/files/VqoddYc6gwtLFjf5CLS9" alt="" width="434"><figcaption></figcaption></figure>

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

**Input:**

{% code overflow="wrap" %}

```
Based on the provided SQL table schema and question below, return a SQL SELECT ALL query that would answer the user's question. For example: SELECT * FROM table WHERE id = '1'.\n------------\nSCHEMA: CREATE TABLE samples (id bigint(20) NOT NULL, firstName varchar(300) NOT NULL, lastName varchar(300) NOT NULL, userAddress varchar(300) NOT NULL, userState varchar(300) NOT NULL, userCode varchar(300) NOT NULL, userPostal varchar(300) NOT NULL, createdate timestamp(6) NOT NULL)\nSELECT * FROM samples LIMIT 3\nid firstName lastName userAddress userState userCode userPostal createdate\n1125899906842627 Steven Repici 14 Kingston St. Oregon NJ 5578 Thu Dec 14 2023 13:06:17 GMT+0800 (Singapore Standard Time)\n1125899906842625 John Doe 120 jefferson st. Riverside NJ 8075 Thu Dec 14 2023 13:04:32 GMT+0800 (Singapore Standard Time)\n1125899906842629 Bert Jet 9th, at Terrace plc Desert City CO 8576 Thu Dec 14 2023 13:07:11 GMT+0800 (Singapore Standard Time)\n------------\nQUESTION: what is the address of John\n------------\nSQL QUERY:
```

{% endcode %}

**Output**

<pre class="language-sql"><code class="lang-sql"><strong>SELECT userAddress FROM samples WHERE firstName = 'John'
</strong></code></pre>

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

**Input**

{% code overflow="wrap" %}

```
Based on the question, and SQL response, write a natural language response, be details as possible:\n------------\nQUESTION: what is the address of John\n------------\nSQL RESPONSE: [{\"userAddress\":\"120 jefferson st.\"}]\n------------\nNATURAL LANGUAGE RESPONSE:
```

{% endcode %}

**Output**

```
The address of John is 120 Jefferson St.
```

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

<figure><img src="/files/nPOfdMEdB0PDK276lTjE" alt="" width="428"><figcaption></figcaption></figure>

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

```sql
SELECT * FROM samples LIMIT 3
```

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:

```
Politely say "I'm not able to answer query"
```

And the final output is:

```
I apologize, but I'm not able to answer your query at the moment.
```

## 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:

{% file src="/files/u4nP1VOUkoC1m1jICylq" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tailwindsdocs.innovativesol.com/readme/use-cases/sql-qna.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
