Welcome back to our conversational AI tutorial series with Langchain! ๐ค In this episode, we dive into the core components of the SQL database chain, unveiling the magic behind seamless data interaction through natural language. ๐ง Join me as we dissect and demystify each step of the SQL database chain: 1๏ธโฃ Create SQL Query Chain: Learn how to structure and compose SQL queries within Langchain, empowering your conversational AI to understand and execute complex database interactions. 2๏ธโฃ Fetch Data from Database: Explore the process of fetching data from SQL databases directly through Langchain, bridging the gap between data repositories and conversational interfaces. 3๏ธโฃ Generate Summarized Results with LLM: Witness the transformative power of large language models (LLMs) in summarizing and presenting queried data, enabling intuitive and insightful responses to user inquiries. ๐ By the end of this video, you'll have a solid understanding of the foundational elements of the SQL database chain and be equipped to leverage them effectively in your own projects. ๐ฌ Join me for an immersive session where theory meets practice, as we navigate the intricacies of SQL query chains, data retrieval, and LLM-driven summarization. Complete Notebook : https://github.com/TheAILearner/chat-with-data #llm #chatgpt #openai #nlp #deeplearning #conversationalai #langchain #datascience #machinelearning #tutorialseries #titanic #sql #database #chain #sqlquery #summarization #sqldatabase #sqldatabasechain
Full Transcript
welcome fellow Learners in the last two videos we have seen how we can Implement SQL database chain using length chain and the llms we have also seen how we can utilize custom prompts to get better results from these SQL database chains so we have seen it involves multiple steps to get the output from these chains we have seen that if we pass the natural queries to it as as we have seen in the last video like if we pass this natural language query to it it will first create a SQL query from it like this will create this SQL query then pass this SQL query to database and Fest the results from it once it gets the result from database it will use that database result SQL query and the input naal language query from the user and pass these input to the llm and get the summarized results from it so in this video we will uh go through each of these step individually so like first we will go how we can create uh SQL query from this naal language question second we will F the results from the Esq query and finally extract the result uh from the llm using these uh input query uh esql query and the result from the database so let's get started in the first component of SQL database chain it converts natural language question from the user into the SQL query for that part it uses create SQL query chain which is basically available in the Lang chain Library we will import this one and then pass the language model and the database to it we are using GPT 3.5 turbo model over here and in the database we are using same Titanic data set that we were using in the last few videos we can print the table info or you can say the schema of the table using this table info uh we can see uh it has given us the table schema and few rows from it so that we can see how does this data look like and then it will pass this same DV do table info to llm now we can create this uh chain uh once our chain is created we can also see what other things it contains we can see it uses the input which will be the users query table info which will be the table schema and few r from it uh apart from that it also uses a prompt which tells it like it is a SQL esite expert and it needs to create a syntactically correct esite uh query so this will will be the prompt now we can invoke uh this SQL Chain by passing uh the query to it so it it will use the llm and we can see it has generated this uh query query over here select count passenger ID as total passenger from Passenger data now our first step is done second step is to F the uh SQL results from this SQL query using the database that we were using for that part we are not going to use any language model we we are going to use Query SQL database tool from this Lang chain Community we will import this and then pass the database uh to it so it it will create a DV execution which will basically takes input SQL query and face the results from the database so so if you see in the next line I have created a execution chain which is basically combination of both SQL chain and a DB execution that we are using so this SQL chain is the same that we were using previously this create SQL query chain what will happen here whenever user asks a question it will first run through this SQL chain uh it will convert the natural language query into escal query and then pass that query into this DV execution so we will uh run this cell and then finally uh uh invoke this execution chain so so we will see the final output over here so like uh first it will convert it into esql query and then pass it to the execution of this uh database so we can see it has faced like 891 total rows over here now the second step is done so third and final step is to summarize the results for this part we are first going to import few libraries we will use these libraries in a swh uh I will be explaining these libraries when we are going to use these so first I will create a template so this template uh takes three inputs so first one is uh users input question which will be in natural language format second one is thecal query that we have generated in the previous step from the user question third one is thecal result that we have faed on the database we will run this cell and in the F in cell you are seeing uh two things one is the output and another one is chain so this output is basically a runable sequence so this is a sequence where the first one is the prompt we will it will get the prompt from this template second one it will pass this prompt to this llm Fest the results and then pass that uh llm output to this St Str output parser to pass AI output into the string format and and then you are seeing this chain so this is our complete chain so it basically first first step is using this using this same SQL chain which is create SQL query chain converts input query into the SQL query format second one is it will run this uh DV execution to F the results from the database uh it is using item getter to extract this query key from this uh dictionary run this uh DV execution F the results and then pass uh all the three uh input result query and the user's question to this template and then pass that to our llm over here and then finally uh finally get the string formated output from this St Str output parsel so don't worry I will explain uh all these step one by one so let's first run this uh this one so we are seeing this uh we are using a runable pass over here so what is renewable pass through is basically renewable pass through is uh just identity function uh it it basically helps in uh using uh the dictionary at R time like it it can be used to add few keys at run time in the dictionary if we are passing dictionary as an input to it if you see our input is in dictionary format and and the prompt which takes uh three inputs question query and result so first input is question which we are going to pass as an uh users query second one is uh SQL query which we will assign at the run time and the third one is uh the result which we will also assign at the run time for that part uh this renal pass through assign function is really helpful so we will run this uh let's say create a variable over here CH for this runable pass through now we can invoke this one cs. invoke and uh we we can pass uh the same input to this over here once we run this you will see along with this question there is another key in this dictionary which will be the query so we are going to run this and and and you can see like it has uh generated the SQL query and also like the query uh key it has added the query key to it so so this is the first step second step is to run this uh through our database and F the results and assign it to the result query so we will we will use this step in a SE separate cell again we can create another function like CS2 over here so so if you see it will first uh use this SQL chain converted into SQL query and then use the item G geta to F the query and then pass that query to DV execution to F the results from the database so we will run this cell now we have this CS2 we can invoke uh this chain using uh same invoke function and then pass the same input that we have used it already this one so so once we pass this input to it it will first uh run this SQL chain get uh the SQL query from it and assign it to the query key in the dictionary then uh in the subsequent uh uh sequence it will fetch the query key from the dictionary using item getter and then pass that uh SQL query to this DV execution it will F the results from the database and pend into the dictionary using the key result so so we will run this over here and we will see instead of two keys that was in the dictionary it has now three keys over here now we have our all the data with us question query and result uh we can use our template that we have seen uh over here and we will pass this all the three input to this template so so when we are going to use this final chain it will uh get all the results these all three results and then use this output uh runable sequence finally so it will take the prompt pass it to llm and then finally pass the llm output to S Str output parer for final results so so for that part we will run this uh final chain uh over here so you can see it will first uh create the SQL query then pass it to the data database and F the results from the database and finally use all the expected results and the input user query and then pass that to our runable uh sequence of this output and finally F the results from the chain so it will it will get us the summarized results from it so so this this this is the final output that we are getting from this complete SQL database chain that we are seeing over here but in this video we have seen all the steps all the components that it was using hope this might helps you to understand all the components that are being used in the SQL database chain goodbye until the next time
Original Description
Welcome back to our conversational AI tutorial series with Langchain!
๐ค In this episode, we dive into the core components of the SQL database chain, unveiling the magic behind seamless data interaction through natural language.
๐ง Join me as we dissect and demystify each step of the SQL database chain:
1๏ธโฃ Create SQL Query Chain: Learn how to structure and compose SQL queries within Langchain, empowering your conversational AI to understand and execute complex database interactions.
2๏ธโฃ Fetch Data from Database: Explore the process of fetching data from SQL databases directly through Langchain, bridging the gap between data repositories and conversational interfaces.
3๏ธโฃ Generate Summarized Results with LLM: Witness the transformative power of large language models (LLMs) in summarizing and presenting queried data, enabling intuitive and insightful responses to user inquiries.
๐ By the end of this video, you'll have a solid understanding of the foundational elements of the SQL database chain and be equipped to leverage them effectively in your own projects.
๐ฌ Join me for an immersive session where theory meets practice, as we navigate the intricacies of SQL query chains, data retrieval, and LLM-driven summarization.
Complete Notebook : https://github.com/TheAILearner/chat-with-data
#llm #chatgpt #openai #nlp #deeplearning #conversationalai #langchain #datascience #machinelearning #tutorialseries #titanic #sql #database #chain #sqlquery #summarization #sqldatabase #sqldatabasechain