LangChain, SQL Agents & OpenAI LLMs: Query Database Using Natural Language | Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video we will look at Langston agents we will see what are those agents how do they work we will look at one specific example of you know uh how do the agents work and how we can use them and then we will focus on a specific agent called SQL database agent uh this is the agent using which you can connect your relational SQL database and then you can query database using the natural language you know that's what we are going to see in this particular video so first of all we're going to require let's say Lang chain as a framework we're going to require you know open AI uh as a large language model and we're going to use let's say mySQL database to connect with my database so we're going to use the pi MySQL and that's what so we have those things available now let's look at what exactly the you know agent so you can go and read more about but let me tell you briefly what exactly the agents are you can think of Langston agents are like an intelligent programs or you know module which can use large language model like let's say chat GPT or gpt4 to decide what actions to take for example you can give some tasks to these particular agents and then those agents will use large language model like charging to decide what action needs to be take to get that particular task done and to do those particular tasks it might require certain tools so there is a concept of tool so tool again is some kind of a program which can perform a specific task there could be a tool which will uh you know help you to search on Google there could be a tool which can help you to connect to the database and there could be a tool which can you know help you run some python code so tool is some kind of a program or there could be a tool for Elena to do some kind of a calculation so the whole thing like you have a tool you have a large language model and the age so agent is a program which is going to use large language model to decide what to do and then it has a lot of tools that uh you know disposal so that it can decide which tools to use and we will see one example so that you know things will be the clear so this is how overall you know exactly the agents and tools and all these things so let's look at one particular example so we're going to use you know so uh in the same example what they have shown in this particular document and rather I will be focusing on very custom example related to the let's say database agents right so do that particular thing what we're going to require to do this uh you know example what is shown here we require we going to require two tools one is this you know serp API which is nothing but you know which allows you to let's say search on Google for a particular so it's like a Google search API you can go here you know you can log in with your Google account and you will get some free credits I think we get 100 or something you could monthly search you know 100 searches that is what you know you will get if you log in with your Google account and you will also get one key sorry like this that you know you can use so that you can programmatically access this particular you know serp API which is Google search API so let's come back here Let Me Assume bit okay so we're going to require the surf API and let's say the task we want to do will also require some mathematical tools so we have math tool we have Search tool so here is a task for particular agent so we want to ask agent you know like who is the Leo dicarpio's girlfriend and what is her current age raised to some you know 0.43 power so this is the task agent has and now agent to fulfill this task it also has the two tools that it can use and it can also use a large language model like an open AI so see what all things we are importing suppose to Paul uh you know from LinkedIn agents we want to import load tools because we're going to use certain tools we also want to initialize agent then there are some agent types we will see uh you know only one of the one type that what we're going to use and then a large language model as an OPI so let's import those things and even you know let's create the llm I think by default uh okay so what it is saying it requires a key open aiki I think we didn't run the above sale so let's run this particular sale and we're also going to require this Google search results python library right so let's run that thing also now we have both the keys we have open AI Keys we have let's say this serp API key now it should work so we created now large language model instance from the open air by default I guess it uses gpt3 by default and then we have these tools loaded then we can now initialize the agent and while initializing agent we can give what tools it has what language model it can use to make those decision which actions to take and this is the agent type so if you go and read more about you know agent types so we're going to use this one zero short react description uh what actually what this particular thing does it's nothing but simply you know read the tools description and then you know decide you know what tools needs to be used for that particular action so for us we could see we have two tools one is the Google search API tool and the other one is the mathematical tool so it is going to look at the description of these tools and then on the whole or the Soul on the description it is going to decide which tools to you so that is what this H and type is here right so let's run and see what happens so we are running this agent with this particular task or let's say this is something what we want the agent should find out so we are saying you know that same thing that who is Leota Carpio then seems to be you know it is coming with this particular answer that you know it find out who is the girlfriend uh you know by searching on the internet and then it find out its age and then eventually it raises to it too late 0.43 power and this is what the answer we got but there are a lot of things happening under the hood and if you want to know you know you know how it is making those decision what actions it took and all these things what we can do we can put a verbose is equal to true so that we can see what's happening you know under the hood or in a thought process of the agent and this is what we could see so you see that you know it is entering into the new agent executed change then it start thinking you know I need to find you know out the who is first of all the leoti carpio's girlfriend and then it needs to calculate her age and so this is what it understands from the whatever the task we have given now based on that it has two tools the Search tool and the mathematical tool so it decided it will use the action Search tool how it decide it is going to again use the large language model to you know based on the task and whatever the tool description available which one tools needs to be used so it decided it needs to take the action which is search and it is going to use that Search tool for that now to do the search action does it needs to provide any input so yes it needs to provide you know uh what exactly it needs to search so it is going to search you know lead your carpio's girlfriend and if you could say uh it observes something so whenever that search results came based on that search analysis of search result it observes something there are some rumors and you know who is that particular girlfriend and eventually found you know the girlfriend name and the next seat saying that it needs to find the age of that particular girlfriend right so it needs to again search on the internet and once it has both the age this is what its thought process it says I need to calculate her age race to this particular thing that's why he think it needs to take action as a calculator so there was a two action search calculator so this is where large language model playing the role what actions to be taken in what sequence or what order they needs to be taken and then eventually this is the final thought that the Asian think it got the answer and this is what finally you see this is the answer so this is how agent works and they are so powerful that if while searching you know if some error comes they will try to recover that particular you know error so this is how agent works I hope you got the idea so that now we can look for the very specific agent that we are interested which is actually the SQL database agent so that's what we will see the next so uh again are we going to create the SQL agent so if you you can go again I put a link here and you can read more about what exactly the SQL database agent so these are the agents so there is a concept of SQL database chain under the hood so in the uh if you see the concepts of you know what are the chains in the line this line chain the chains are nothing but you know when you want to perform some actions in a particular sequence but when you want to combine let's say uh two three models together you can chain them together so that's what the concept of chain here you can read more about it so what exactly this you know SQL database agent is actually going to connect to our SQL database and we can ask them you know questions in the natural language so let's see how it works so first of all we need to import you know create the SQL agent there's a concept A toolkit in the Lang chain so toolkit is nothing but the specialized agent so if you go and read more about you know what exactly the toolkits what they have recently you know announced so total clients are nothing but you know agents designed to particular use case so we saw a generic agent right which actually searches on internet for something but now you know we're going to look uh sorry not here here but now we want to look at various particular agent which is very specialized in SQL database related stuff that's why uh this concept is called toolkit and then we're going to enter the SQL database the open a again we don't need to import it again but then there is something called Agent executor so that we can execute I think we might have uh okay let's see let's import this particular thing then what database I'm using so I I go to this particular you know let's say this MySQL tutorial.orgen there is a sample database what I took so you can download that database and you know import there is a schema associated with that DB which seems to be related to let's say retail kind of stuff we have a products we have you know those product order details we have customers their payment details and all those things right so typical retail so that we know we all sumat let's say familiar with something like this so I choose this thing I imported uh you know that data into the actual DB hosted on let's say AWS RDS this is where my MySQL DB is hosted and now we're going to connect this particular DB uh using this you know Lang chain SQL stuff what we require to connect to our database whether you connect via agent or whether you connect while it's a normal Python program we're going to require a couple of things we need to know first of all you know database username it's password it's host name So currently it is hosted on AWS it could be your localhost it could be whatever you know uh the server Yahoo studied and then the database name this is the name of the database what you know we actually uh imported right and then you can use you know SQL database this is what we imported here right so it has a method called SQL database from URI so every database that you want to connect it will have some connection string you can search for it and that connection string will have some kind of a combination you know it will take username password host and it will also have some kind of a you know database plus library or driver uh you know information so currently I'm going to use actually my spy MySQL to connect with that particular thing you can read more about it uh you know it is using under the hood SQL Alchemy so you can also look at so basically you just need to use proper you know whatever the uh database that you are dealing with if you are not sure which one you can comment you know enter you know video you can comment on this video and I will help you to get the correct you know connection string for whatever the database that you are working but for now let's say we are dealing with the MySQL and we are using this Pi MySQL driver or the library so if you do this all this information you should be able to get the database instance or let's say connected to that particular database now next what we got the DB now we need uh the language models on top we have used I guess in a GPT default but for now let's say we can use the chat model which is let's say GPT 3.5 or gpt4 so we can import those chat models let's say chat open Ai and I'm going to put a specific model name here you could pass GPT 4 or GPT 3.5 so let's deal with um let's say GPT 3.5 now once we have the DB once we have the large language model you know we also require this toolkit and then eventually we can create now SQL agent which will take our large language model instead of giving this particular llm I think I need to queue this one right here the llm and this is the toolkit again we will put verbose is equal to true so that you can see what's happening under the hood now let's get that particular agent so we got the agent with all these details now we can you know execute or run that particular agent giving the similar kind of task but those tasks are now not generic they are like a database related to task what we want to perform so the first task we perform you know describing at particular tables we will say describe the order related table so we are not giving any particular table name but we are saying you know we are interested more of you know order related tables and how those tables are related so let's see whether it is able to figure it out which are the tables are related to the order tables so again it starts so first action it took actually first of all it want to list all tables that's the first action it did so here is a list of all those tables okay it's running so let it finish and then we will go through it you know peacefully it's still running okay maybe let's just go through it so you know first of all it want to list those tables now you see there is no input required for such a thing so here we don't require anything input because we are just listing all the tables what we require so input is empty so what observation it got it got all the list of tables and then it started thinking you know that I should query the order details table to see if anything order related it can find there and so you know it decided to you know get the schema for orders and the order details table and eventually it got order and Order details table you know and maybe it started again thinking the foreign key constraint and everything here you could see and then eventually it did a lot of things under the hood you know to see what and what is the final answer it is giving it is saying the order table and the order details table are related by order number column which serve as a foreign key some kind of you know how how they are related and how do you want to join and all these things so this was just describing you know tables but what if we made some mistake while describing the table name let's see what happens we are saying you know describe the purchase details table but we know there is no purchase details tables as such and it got error actually so first of all you know it lists all the tables and then even though you know it started querying the purchase details table to get its schema you could see it is asking for the schema and then it got some error saying that you know purchase details table not found and then maybe it may say you know I made a typo I have tried again and it kind of you know make purchase details to order details and then you know it somewhat figured out okay this is the nearest or you know meaningfully nearest table that it should look for and then eventually it says this information that you know the purchase details table which is actually called a order details has this number of column you know all these tables so this is how good this right so let's say you run some query and after running the query you know let's say it made some mistake it come up with the SQL query but then uh you know sometimes it might come up with some wrong column name or something like that then it can correct itself this is the power of Agents so agent can correct themselves because they are able to observe when when you run an SQL query they can have an observation and based on you do the same thing right when you use chat GPT what we do we run some query we go back and paste that error in the chat GPT UI and and based on that again you know chat GPT suggests to do something right now you can think of you have an agent who can do that themselves like they can simply observe the error and then you know again have some kind of a thought it's like a thinking like a human right uh kind of an intelligent um Bing so you see we got this information now let's try to run some meaningful queries now you know we deal with the describing and all these things let's see uh we will ask find the top five products with the highest total sales and revenue and let's see how it start thinking if it wants to you know find this particular thing let's see what it thinks so again the first action it wants to do is actually simply list the tables and now once it has tables it is still thinking what to do using okay if start what what's its thought process it is saying that I should query the products and the order details table to get the total sales revenue for each product so it decided to do a action called query checker and then it read some query that you know select product start product name and then yeah if you could see our order detail tables we have you know quantity order and the price for each if you go back and see you know all the details here we have the quantity order and the price each so it needs to multiply so that you know the what is the total order sum for that particular order so that's what it is doing actually uh for this is single order then this is for all the orders you know from the products then it is joining the order tables and then it might be you know Group by by the product name or maybe the product code and then finally order total revenue descending and get the top five highest you know this is how it come up with a query and then you know the query looks correct so it even gives some you know thought to it whether it is correct or not and eventually run the query it cuts an answer right which will be typical whatever the python result you will get which is nothing but the list of tuples what it gots and eventually it frame that all information in a plain text so that you know we understand it better it says that you know the top five products with the highest totals is revenue are this particular you know one this is the second project so this is how good it is you know it can take whatever the you know it's not giving what you know this programs are communicating with each other rather whatever the response is got from the other program it's kind of you know giving in a more meaningful way so that you know it's more like a human understandable manner let's run the one more query so this is how you can you know use this SQL thing right you see we didn't require any training data you just need to connect your database you know and it's just going to work most of the time it's simply going to first of all get the I hope that your table name should be something meaningfully describable because that helps you to understand which tables to look for for the material so if your table names are self-explanatory that is going to help your agent because that's what it is helping to narrow down to you know which schema to look for and then it is going to look for those schema based on that understanding it's going to decide whether to query once or the query twice and you know it might want to do a query twice again here you know what did we search at least top three countries with the highest number of orders right and it starts something thinking uh it ran that query it looks good it got some result and eventually you know it says that it is USA France and Spain you might want to try some different queries which are complex which might require multiple steps to get that particular thing but the whole idea of this video to show you you know how how do we connect how those agents thinks actually you know what kind of actions they took and maybe based on your trial and error see uh you know maybe you can read more about how those agents work even I am interested to know what's happening under the hood and maybe how do we create our own agent right or the you know custom agent that can be you know interact with our DB so I hope you found this video useful I will be deleting all those keys whatever I expose here like both these keys will be deleted by the time this video gets live and if you have any doubts you know anything related to this video you know let me know in the comments and if you want or any have any suggestions for the next video that also you can tell me and if you face any issues you know while dealing with this particular video or anything related to The Lang chair or NLP you know you can ask those questions on the Discord there are many people who can answer your queries and whenever I get time I personally answer them thank you very much
Info
Channel: Pradip Nichite
Views: 24,111
Rating: undefined out of 5
Keywords: langchain, langchain agents, langchain ai, langchain in python, langchain prompt, langchain tutorial, langchain tools, langchain agent, langchain gpt 3, langchain agent example, langchain with api, langchain chatgpt, langchain huggingface, langchain openai, integrate openai with langchain, 1littlecoder, Data Independent, James Briggs, Yannic Kilcher, Sam Witteveen, Part Time Larry, Shweta Lodha
Id: VG9KYCS0-8E
Channel Id: undefined
Length: 20min 39sec (1239 seconds)
Published: Mon Apr 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.