Build an SQL Agent with Llama 3 | Langchain | Ollama

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome fellow Learners in this video we are going to implement a SQL agent from Lang chain using Lama 3 large language model we will be implementing this whole uh SQL agent in our local environment to utilize this Lama 3 Hassel free we are going to use olama AMA is a platform which facilitates the use of AI models is specifically large language models to utilize this olama we first need to install this AMA application so let's first check out this AMA website you can see there are lot of different AI models available in this AMA Library like Lama 3 quen 2 53 a and so on we will be using this Lama 3 so let's click on it and we can see what are the different versions of Lama 3 are available in this AMA so you can click on this drop down and you can see there are different numbers of Lama 3 versions available in this AMA like8 8 billion 70 billion instruct text and other quantized versions also so to use this llama 3 we need to do a simple uh run command o Lama run Lama 3 and it will basically fetch Lama 3 model from this o Lama and we will be able to chat with this Lama 3 directly but to run this uh Lama 3 we first need to install this o Lama to install this olama you can check out its GitHub page and you can see the there are different instructions written over there but you need not to follow other instructions so you can choose uh particular instructions as per your uh particular operating system so like for Mac OS it is written here for Windows written over here so let's say I'm using Windows you can just click on this download button and it will start downloading this uh AMA platform or o Lama application over here I have already installed this olama so for you once you have downloaded this uh AMA you just need to install this and you will be able to run that ama directly so once you have done that uh you need to do one thing you need to just run this command o Lama run Lama 3 and it will fetch Lama 3 directly and it will save in your local directory to check where this uh o Lama has downloaded this Lama 3 model you can go to your users directory and under users directory as you can see this users Atul this is my directory users directory under this directory there will be a folder created named do and in that folder you can create you can see these are the different uh uh folders and files are being created under this models folder you can check out what are the different models that are being downloaded by this AMA so as we have only uh downlo downloaded this Lama 3 Model so there will be only one major file which will be you can say around 4.34 GB and uh this is basically the Lama 3 Model that has been downloaded using this AMA so you can check out uh the size of this Lama 3 Model you can see around 4.7 GB other files will also be there like tokenizer and all those files will also be downloaded there now uh you might be thinking like uh this Lama 3 version is uh 8 billion and why this uh particular size is only 4.7 GB so in this AMA they have already quantized this Lama 3 8 billion model to 4 bits so that's why this uh model size is only uh around 4.7 GB so now next we can directly run this uh command so just copy this command and go to your command pront and uh once you have run that you will be able to see uh running this uh Lama 3 Model so one more thing like how you can check if this particular oama is running or not once you have installed the AMA and you have run the AMA it will be directly uh it will be always running in this local host and this 11434 Port so you can check out this you can use this uh port and you will be seeing this AMA running in this particular port in your local system so now once you have done that you can see uh now this Lama 3 is running you can uh ask anything or just like send hi or something and it will be responding using this Lama 3 Model large language model that is installed in your local system so now you can see it is uh streaming out this output so like it is simply saying hi it's nice to meet you is there something I can help you with or you would like to chat or something so this is the simple implementation of this llama 3 using o Lama AMA has made it really simple and hasslefree to use a lot of large language models that are open source so now we are having this Lama 3 language model in our local system so let's jump into the implementation of this SQL agent using this Lama 3 language model so as we are going to implement this using Python and we will be utilizing Lang chain so we first need to install few of the libraries that we will be requiring to install or implement this whole SQL agent so those libraries are like first one is L chain another one is L chain Community these two will be required uh when we are going to use different different modules from the L chain and third one is AMA so to utilize this Lama 3 from o Lama we need to install this o Lama Library as we are using this python to implement this we will be requiring to install this uh these libraries so as I have already installed these libraries I will just run this cell and nothing will be printed out over here as I have commented out this line so let's import this AMA directly from this Lang chain uh Lang chain Library so for that we can do something like this from Lang chain Community uh llms we can directly import this AMA you can see uh like this so This AMA will be imported in our current notebook once we have done that we can create this or import this language model directly in our notebook by just writing this simple on line uh code so like this o Lama and then we just uh we just need to pass this model name so as we are using Lama 3 uh as we are doing this uh experimentation using Lama 3 we just need to pass this Lama 3 as model name for this language model and we will be having this our uh language model with us so let's uh test it out we are going to use llm do stream to uh test it out this uh Lama 3 Model so for that we can use something like this lm. stream and then we can pass any query to it so let's say just I'm passing Hi how are you so it will ask this language model so this query and uh then we can print out this chunk and uh this chunk will be printed in a streamed way so one more thing like this print statement uh has an in uh argument which basically uh currently as will be passed as default which will be the new line and if we pass it as it is it each of the chunk will be printed in a new line that we want we don't want so for that part we just need to pass end equals to empty and uh next in a few seconds we will be seeing this output from this language model so now you can see it is giving us response like I'm just language model I don't have any feelings or something so this is pretty good now we are having language model with us next uh let's import our database for the database part we are going to utilize again language uh this L chain community so we can do something like this Lang chain community and from Lang chain Community we will be needing uh this utilities and uh from utilities we are going to import this SQL database module from it so let's create a database from this SQL database for that part we need to utilize from U function from this sqli SQL database and we need to pass a local URI so as I'm using sqi database and I'm having a chin database already in my local system so I just need to write uh this URI path and uh I can pass another argument so like how many rows I want to sample from the database so I can pass something like sample uh rows uh in uh table info so I can uh pass this argument and then I can pass how many rows I want to sample from it so once I have done that I can uh see how many uh different tables in it uh so I can use something like uh db. getet uh usable tables you can see this uh name or this uh function name and it will be showing us what are different tables that are in this database we can also print out uh the table info and it will show us uh the information about all the tables that are available in this uh database along with the table schema and the these three rows from the database so we can do that and you can see it has all the tables and there schema within it so we can see like one of the table is like album so what is its schema like it is containing album ID title artist ID what are the different data along with the three rows in this table and for other others tables also so we can we can see uh it from here so let's just comment it out and and we can we can now see we are having our language model and along with that we are also having our this uh database with us so next we are going to create a SQL agent from this to create SQL agent we need to First import two modules from L chain the first one is uh from Lang chain uh we again need to utilize this Len chain Community uh L chain community. agent toolkits uh we will be importing this SQL agent so the module name is create you can see other other modules are also available like create Json agent create open a agent so we will be utilizing this create SQL agent so we will be using this create SQL agent to uh create our agent executor so uh another uh module that we need to import is uh agent type so we can do that from uh by you doing this from Lang chain do different agent L chain. agents uh we can import this agent type so we can do that and uh we will be using this agent type to pass an argument to this create SQL agent so let's uh create our agent executor to create agent executor uh we need to pass uh this uh different different arguments like uh language model database to this create SQL agent module so we just need to do this from create SQL agent and now you can see what are the different arguments that need to be passed to this uh create SQL agent module so like uh database agent type uh this language model is the first argument so we will be passing it so the first argument is language model second AR argument is the database these two we have already uh defined uh for the third argument for the agent type uh we can utilize this agent type from this length chain. agents and we can see what are the different agent types available uh in this Lang chain so you can see a lot of different agent types are available so like chat conversational react description so like open a functions reate do store those kind of agent types are available in this langage for now we are going to use uh this zero sort react agent so uh this is one of the type of the agent that is available in the Lang chain uh the only uh part is it can be used for any simple model any simple language model and uh this is basically used when uh we want our model to uh first regon it out and then act accordingly so so on the basis of that it will work so uh we have pass these three arguments one another argument that I will be passing is veros equals to True when we are passing this veros equals to true we will be able to see what are the different uh intermediate steps that this agent executor is uh basically using so let's run this cell uh it is showing that SQL database object has no attribute get tools okay so I have uh missed some something I think I have missed a keyword argument over here so instead of passing this directly I need to pass dbal to DB and once done that I think we will be able to use this agent executor one more thing you might be wondering we haven't added any prompt to this agent executor so as this create SQL agent module is already predefined in this Lang chain it is already having an embedded prompt in it so we can print out that uh prompt so you can do do that by doing agent executor dot prompts and you can see this uh get prompts uh this method you can see uh the which particular prompt or what are the different things that are being passed to this agent executor so if you see this particular section of this uh particular uh this agent you can see what is the different text or the prompt that has been passed to this agent executor so if you see uh first you can see what are different tools that it contains you can see SQL DB query sqdb schema SQ DV list and SQ DB Checker and then after that this is the particular template of the prompt has been passed to this agent executor so like it has shown it has given like answer the following question as best as you can you have access to the following tools these are tools and uh use the following format so it has uh given one format also like question will be there and you need to thought something so uh like think it think about it and then provide a acction to it and then do that acction using the one of the tools so this is the basically prompt that has already been embedded in this particular agent so let's uh just remove this cell and uh let's uh execute this agent executor now we are having our agent executor with us we just need to do uh we just need to basically run invol function from this asent executor and we will be able to run this SQL agent uh by using this Lama 3 uh model so so just write something like this agent executed. invoke and then we can pass any query whichever we want to run uh using this uh agent executor so let's see so let's see uh which particular query we want to run from this uh database so let's first see what are different information are available in this database okay so these are different different t that are available in this database so let's ask any uh question from it so let's ask like uh uh so let's say for this how many different artist are there so let's say utilize this table I'm just asking a simple question how many different artist are in the database so for that I can just write that query uh how many uh artist are in the database so this agent executor will run now you will be seeing what are the different step this agent will follow so like you can see the first step it it shows us like entering new SQL agent executor chain so it will basically uh first run this agent executor chain so as we are running this uh Lama 3 in our local environment so as per the GPU and RAM availability uh it might take a little time so but you can see now it has shown us the first step that this particular agent has followed so we are using this zeros s description agent type so you can see first it thought like to find out how many artist in the database I will first need to check what tables exist and then query that table so you can see it first thought of like it need to use this SQL DV list tables uh Tool uh and then it it basically find out these are the different tables available in the database now then it thought of like now that I know the table exist I will query the table to get the count of distinct artist artists so you can see it just utiliz the schema of this artist table and I think it will then uh create SQL query to get the data from this artist table now you can see it has created this query select count distinct name from artist and uh and you can see there is an error while fetching the data from the database so you can see uh this error so what this agent will now do is it will again uh rethink and it will find out what is the error and then try to uh resolve that error and then again create some query now you can see uh it has again thought of like I will correct the SK query by adding the semicolon at the end of the table name since it's enclosed in double Cotes so it has found out what was the error in the query and it again regenerated the query like select count distinct name from artist and now it has added a semicolon at the end of it and then you can finally see it has fed out from the database how many different artists in the database in that artist table now uh it will again think it of like input and output and then provide the finally summarized result from it now you can see after that after finding out uh the actual number from the database it gone to some different direction some Mis Direction it followed but finally it uh find out its direction and it's shown us the total number of artists in the database is 275 so as you can see it's pretty simple to implement SQL agent using L chain and Lama 3 utilizing this AMA so you can uh write few lines of code like as you can see I have written around 10 12 lines only and created a SQL chain from from this 11 uh 11 tables database so you can do different things like you can make it more beautiful by creating a UI on top of it you can add uh gradio uh after this and you can create an UI on top of it you can use for uh you can see for other different task like in the database if there are only one table and now as we are seeing for this multiple tables it might need to create some comp queries also you can see for like if there are multi uh high cardinality in one particular column also so you can create a database toolkit and those things so you can test it out you can trial and error and see how this particular SQL database agent performs so it's really cool like we can utilize this llama 3 in our local environment and run this SQL agent thanks for making till the end of this video goodbye until the next time
Info
Channel: TheAILearner
Views: 2,623
Rating: undefined out of 5
Keywords: sql agent, ollama, sql agent with llama3
Id: GlyvykfIPJI
Channel Id: undefined
Length: 20min 28sec (1228 seconds)
Published: Fri Jun 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.