Natural Language to SQL | LangChain, SQL Database & OpenAI LLMs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the previous video I showed you a very important agent called as pandas data frame agent there is a similar agent for SQL as well so in this video I'll show you how you can query your SQL based databases in natch language using Lang chain so without wasting any further time let's kick start the video since I've already mentioned that this video is all about Lang chain I'll quickly install Lang chain and open AI now that the installation is done I have a secret key hidden since it's a secret key and I want to keep this as a secret from all of you I'll quickly run this cell without showing you the secret key inside the secret key is the open AIS secret key so that is all that it has so you can also get it as well now in the previous example I had a CSV file wherein I could query the CSV file in form of natural language I've used this piece of code to convert that particular CSV file into a SQL database so you can use this piece of code to convert your CSV file into a SQL database but if you already have a SQL database then you don't have to execute this which is where I've commented out the code so just to be sure that we are reading the DB correctly you will have the database as well as you will have a table so I've created this simple function called as def read underscore SQL underscore query which takes in the SQL query the database and then executes the query that we have passed in and once that's done it kind of prints row by Row the results that it gets once the query has been executed so I'll quickly run the cell so let me call the function read underscore SQL underscore query now so I say select star from Fashion underscore products limit 10 so I only want to look at 10 rows of the result output and I am passing in the actual DB name so this is your table name this is my DB name so I'll quickly run this cell so it says SQL light 3 is not defined so all I have to do is import SQL Lite 3 and finally it gives me the result so even in life if you're stuck somewhere if you're not getting the code Don't Panic just read the error message and you would basically move on to the next piece very quickly so that is something that I wanted to share here let's move forward so here's where you would find a lot of companies trying to create a solution and all of it exist in the open source world so here Lang chain has basically abstracted out all the heavy lifting that you have to do in order to convert natural language to SQL all you have to do is import open AI import SQL database and import SQL database chain all that's required is these three Imports and then you require SQL Lite 3 since I've already done that I'll remove this so not so that piece of code is not required so I'll quickly run this here the first line of code I Define where my DB exists so I already have my DB in this particular instance called as fashion underscore db.sqlite so so I'm kind of referencing this particular DB here and I'm saving it into a variable called as input underscore DB I'm creating an instance of the large language model so this is what I have here so I'll quickly run this cell finally in order to create an agent wherein you can ask questions about your DB in natural language all you have to do is create an instance of the class SQL database chain pass in the large language model pass in the DB that you have created above and verbose equal to True helps you understand the middle level outputs that are generated before you reach the final output so I'll quickly run this cell by the way this particular instance that I've created will basically be saved into a variable called as dbagent so when you have to execute commands you will say dbagent dot run now I stick to the questions that I've asked in my previous video but if you're watching this video for the first time and wondering what is he speaking about don't panic I have the questions here as well so we'll go through every question one by one so the first question is how many rows are there I am too lazy to kind of understand how many rows are there in the DB so I'll ask the question in natural language so I say DB underscore agent dot run I kind of asked this particular question and I run the cell so here it says entering new chain how many rows are there it kind of generates the query as well that is Select count star from Fashion products and this is the result so the final result that launching is giving me is there are 1000 rows in this particular table so overall again it's absolutely correct and I'm still in awe in terms of how it's able to so seamlessly generate SQL queries using natural language there are companies that are literally raising funds just for solving this particular problem can you imagine all of this is open source now let's move on to the second question how many entries of Adidas are present in this particular SQL database and here it says that there are 198 entries we've already verified this particular result so it says select count star from Fashion products where brand is equal to Adidas so again the result is absolutely correct look the third question how many Excel products of Nike are there that have a rating of more than four so again a very very complex question in terms of SQL something that's not straightforward and here it's given me the final result as 12 so there are 12 Excel products this is the SQL query that has been generated and again if you look at the output it's perfectly fine if you look at the output and if you're wondering how awesome is Lang chain then you are absolutely right Lang chin is pretty awesome in terms of generating outputs at this stage so be mesmerized with what you're seeing right now and keep enjoying the moment as I go to the next question as well moving to the fourth question give all the details of Adidas which have a size of large and have a rating of more than 4.2 so somewhat complex in terms of query building but I let Lang chain do the heavy lifting for me so here it's given me the query since I've mentioned give all the details it's giving me all the columns which is where select star comes into picture here is a table name your brand is equal to Adidas if you notice one thing very carefully I have not mentioned that Adidas is a brand it's automatically detecting Adidas as a brand I've said that the size is large so size is equal to l and rating is greater than 4.2 so entries where all of the conditions are met is what is filtered and here the Adidas products which have large size rating 4.2 are sweater dress t-shirt and sweater so there are total four entries and this is something that we validated in the previous example as well so the result is absolutely correct the level of mesmerization is what you would feel after this video right all you have done is you have kind of asked a question in natural language to a database you've not passed in any pre-built schemas as well so there is no a few short learning that's happened here or there's no zero shot learning that's happened here all you've done is you've asked question in next language Lang change agent which is related to SQL which in our case is SQL database chain as well as the database that you have to load which is SQL database these are the two things that you've done apart from that everything is asking questions in natural language this my friend is the power of Lang chain and this is something that will make huge inroads in the AI industry going forward companies will start adopting Lang chain to and fro because of all the variety of things that Lang chain has to offer so this is what I wanted to show you in this short video today I hope you enjoyed this video stay tuned for many more videos on Lang chain and the developments of llms thank you so much for watching the video [Music] thank you
Info
Channel: Bhavesh Bhatt
Views: 12,731
Rating: undefined out of 5
Keywords: Data Scientist, Machine Learning, AI, langchain, langchain chatgpt, long chain python, langchain openai, gpt 4, large language models, pinecone, hugging face, hugging face models, llm explained, langchain in python, langchain ai, langchain prompt, langchain agent, embeddings, vectore store
Id: w-eTS8YlbZ4
Channel Id: undefined
Length: 9min 4sec (544 seconds)
Published: Fri Jun 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.