Scroll Top

Natural language to query your SQL Database using LangChain powered by LLMs

LangChain - Unleashing the full potential of LLMs part 2-01 (1)

| Introduction

As the world is busy taking help from ChatGPT to solve their day-to-day queries and enterprises are getting ready to apply the power of Large Language Models to their business use cases, everyone has unanimously agreed and submitted to the magnanimity and potential of these Large Language Models.

One use case which has been around from times much before the terms ‘GPT’ and ‘LLM’ were so popular, is converting a user-provided natural language query to a syntactically correct SQL query that can be directly executed on the database and results can be extracted.

You may have seen one of the early examples of this in action if you are a cricket enthusiast and follow the website AskCricInfo

This use case can be applied to any enterprise which wants to enable its leaders to get a direct handle on critical data and be able to query in plain-spoken language.

Text to SQL is one of the main capabilities of Large Language Models and can be achieved by providing proper prompts directing the model with the required table schema to be considered while generating the query.

In this blog, I will show you the steps to make use of the SQLDatabaseChain feature of LangChain to achieve Text-to-SQL functionality.

Getting started with the Postgresql DB

For this purpose, I will be using Postgresql provided by ElephantSQL

We will have a simple database of authors, the blogs written by them, and a frequency table to record the frequency of publishing blogs.

To start with we will define 3 tables as below:

Using SQLDatabaseChain from LangChain

Now we will start a new jupyter notebook in a working environment or directly work on colab.

Using the SQLDatabaseChain, you can setup your text to sql chain to connect to DB and answer your questions as below.

You can see the response as below:

 

The chain is working. Now we create some dummy data and then ask some more questions.

Below are some examples of spoken language questions which fetch data from the DB after creating dummy data.

db_chain.run(“How many authors write blogs on topic of Campaigns?”)

db_chain.run(“Give me the list of authors”)

Let’s try a somewhat complex one –

This one does not work and the LLM is fetching some wrong columns which do not exist.

This will require some extra prompts to the LLM.

Using PromptTemplates from LangChain

Now we use another feature of LLMs – PromptTemplates 

 

As part of the prompt template, you can provide any additional information to be used to build the query.

Now we will modify the prompt to make use of the prompt template defined above.

Now the earlier case works as you can see below and returns proper results.

You can control the tokens by explicitly specifying which tables should be used when connecting to the DB to avoid unnecessary tables.

You can also return and access the intermediate steps of a SQL database chain using the parameter return_intermediate_steps as true. This includes the generated SQL query, and the raw sql result.

To safeguard data-sensitive projects, you may opt for return_direct=True during the initialization of SQLDatabaseChain, which will enable you to directly receive the output of the SQL query, without any extra formatting. This approach will prevent any content within the database from being visible to the LLM. It is worth noting, however, that the LLM will still have access to the database scheme by default, which includes dialect, table, and key names.

Conclusion

Prompt engineering powered by LLMs and supported by the LangChain framework can enable you to write more complex queries on bigger databases bringing to you the power of Language. 

This is one of the multiple capabilities which LLMs provide and which is further facilitated by the LangChain framework. You can apply this to different use cases where you want to simplify the interaction with your data and make it more accessible to enterprise users.

In future blogs, we will explore more of these use cases.

Do connect with us to learn how you can apply this to your enterprise and unleash the power of LLMs.

Leave a comment

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.