6-Building Advanced RAG Q&A Project With Multiple Data Sources With Langchain

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello all my name is krishak and welcome to my YouTube channel so guys uh we are going to continue the Lang chain series and in this video we are going to probably develop an amazing multi-search agent rag application um what exactly is the project all about what all new things we are going to specifically learn and probably I think this is the most amazing thing that has been inserted in the Lang chain Library itself some of the amazing modules that are there which will actually help you to make your entire conversational chatbot quite amazing itself right so let me go ahead and discuss about this what what exactly we are going to do what kind of projects we are going to implement here so let's consider that I want to generate a gen AI powered llm application so this is my llm application that I really want to generate and this let's consider that it has dependencies on some of the other open source platforms or other websites or other data sources itself right like like RVE RVE if you don't know like all of the research papers that are available will be available over here if I probably consider with respect to Wikipedia right Wikipedia also has a huge amount of content similarly I may also have let's say my own company's PDF you know from where I also need to probably develop our Q&A applications right so all these things we will probably try to implement it now the main thing is that here you have multiple data sources and you really want to integrate all them as a rapper so that you'll be able to implement this entire Q&A solution right over here so here you're also going to learn about some new terms and right now it is becoming very popular like tools right tools what are tools in Lang chain what are agents in Lan chain what are toolkits we'll also be learning about toolkits right and how do you probably create a wrapper on top of this particular toolkits but if I just want to give a small brief introduction about tools you can see that here I have dependency on this platform on this platform I can probably use all this platforms as a separate tool so that I'll be able to ask any questions from this particular platform itself along with that I may also have my own customized PDF own custom data which will also be in the form of vector embeding so what I will do is that I will try to wrap this up in the form of toolkit and then with the help of Agents I will be able to execute any Q&A search that I really want to do it yes I have probably told a lot of terms and topics over here but let's go ahead and let's see how we can actually implement it and all this implementation that you will probably be seeing will be quite amazing lot of learnings will be specifically there and you will definitely be able to learn many things out of it okay so let me quickly go ahead and open my uh code file and I will continue the same code file that I had actually implemented earlier itself right uh one more very important thing that I really want to talk about is that uh if you remember I have uploaded five videos till now and all those videos are with respect to some different different applications right and that is how we should definitely learn all these topics so that we can actually get an efficient way of how things are basically getting used okay so as usual what I'm actually going to do is that let me go ahead and search for Lang chain tools okay so here you you if you able to see here you have something called as Lang chain tools so tools are interfaces that an agent chain or llm can interact with the world right so if I really want data from some other data sources uh some other queries that I have you can also include Google search API over here and a lot of tools are specifically given by uh lanin itself you know we have to really create a rapper and then we will be able to have a conversation with them so some of the examples with respect to the tool like what all tools are specifically provided you'll be able to see in langen these are all the built-in tools that you have Alpha Vantage fi R save AWS Lambda Bing search Brave search Chat GPT plugins you know D image generator you can also able to generate images then you have Eden AI file system golden query Google Finance Google jobs Google Lens Google Places Google scholer Google search Google serer so you can probably use any of these particular tools to extract some of the data that you want let's say that I am planning to create a chatbot which will be research more oriented and I will probably ask questions that will be related to different topics research and all other than that I will also try to create a rapper on top of Wikipedia and along with that my under the customized uh PDF files okay so I will try to include all those thing and you can use any of this that you want based on your use cases but I will try to show you an application how I will go ahead with okay so let me quickly open a new folder over here so I will go ahead and create a folder and let me go ahead and write agents over here as my folder name and I am going to create a file which is called as agents. ipynb file okay now inside this particular file I will go ahead and select my kernel step by step I will try to show it to you how things will be working in this and how I'm going to build this entire thing okay so the first thing as usual uh as I said that I will be requiring uh RF so you have to probably go ahead and install this so I will go ahead and write pip install RF so okay dou S no worries so I will go ahead and install it it'll go it'll get installed in my VV environment once this is basically getting installed what I'm going to do is that I'm going to use Wikipedia I'm going to use RF also and I'll create as a wrapper okay so first of all let's go ahead and create a wrapper on top of Wikipedia so for that what I will do I will go ahead and write from Lang chain uh Lang chain undor community so it will be present inside this community itself all the tools that are available uh within the Lang chain itself then I'm going to import Wikipedia Wikipedia query run okay so I'm going to just write Wikipedia query run I hope this works let's see whether this works or not okay this is my Lang chore Community but I think it should be working now um from lin. community. tools import Wikipedia or query run along with this what I'm going to write from lanin I'm also going to create a use some rapper class on top of it do utilities utilities import Wikipedia API rapper okay so I'm going to specifically use these two things over here one is the Wikipedia query run and Wikipedia API rapper okay for Wikipedia you don't require a separate API itself already Lang chain is taking care of that so if I go ahead and write uh and probably execute this Wikipedia API rapper I will say hey provide me the top K results right so how many results I want let's say I want one so I'll go ahead and write document uh content care Max Max to Max I will probably require 200 characters okay from whatever search I actually do from Wikipedia you can increase it so this will basically be my rapper itself API rapper okay so this in short is interacting with the Wikipedia to find out the so many number of results so this is basically giving some configuration details over here the next will be my tool as I said I will be using my Wikipedia query run tool okay and then this will be initialized and here I'm going to initialize my API wrapper with the API rapper that I've defined over here okay so this is what is my tool over here so I'm getting some error could not import Wikipedia python package please install it with Pip install Wikipedia so I will go over here and requirement. txt I will go ahead and write Wikipedia okay I will be requiring this Library along with this as I said I'm going to import also R okay so let me quickly go ahead and open my terminal and I will start the installation over here okay so let me go ahead and write paper install minus r requirement. txt so both these libraries will get installed that I specifically want initially you actually require it whatever is there this packages are available and L CH try to integrate with that and that is the most amazing thing over here okay so now I don't think so we should be getting an error so now it looks good now if you go ahead write tools over here or tool tool name over here so here you can see Wikipedia query run if I go ahead and write tool. name you'll be able to see Wikipedia is my uh tool name okay now this is one of the tool similarly what I will uh probably do is that I will also uh take a website or a PDF whatever it is right I will read all those kind of PDF okay so that I will also consider that as a data source so let me do one thing let me I will probably create another tool over here okay so let's see over here I will go ahead and go to this website okay so let me just see this website uh I've just copied this okay so this website this website okay so this is the website that I will also be using and I'll try to retrieve the content from here also so in order to read this content from this particular website I will be using webbased loader as you all know we have also discussed about that because that is also one of the data in thing right so I will write from Lang chain community. document loaders and here I'm going to specifically import web based web based loader okay along with this uh as you all know since I'm reading the content I will be using F I'll be using open a Bings and all so let me copy it from here and let me paste it over here here okay so I'll be using F I'll be using open a Bings I'll be using recursive uh character text splitter so that I'll be able to divide all those into chunks also so everything will be used over here in this particular case and one thing that you really need to understand why we are doing this because this is my own custom data let's consider this as my own custom data I need to probably convert this into a vectors so here I will go ahead and load my loader and here I will be using webbased loader and I will give my URL the URL that I had actually got from here so this URL will be basically reading the entire page okay once I probably get the loader. load okay uh loader. load it will load the entire content from that particular website so this will be my docs okay after getting the docs the next thing that I will do is my recursive character split okay and here I'm going to specifically use some chunk size so this will basically be my chunk underscore chunk uncore size is equal to th000 uh and then I will also be writing chunk uncore overlap which will be nothing but 200 okay so this is some default configuration I have actually taken in this um and then uh I will also write do splitcore documents I will split all these documents that are available inside this docs okay and I will finally get my documents itself okay and this have already done it many number of times I think in my previous tutorial also then I will go ahead and create my DB my Vector DB so I will go ahead and write Vector DB Vector DB and here I'm going to specifically write f f Do from documents from documents and here I'm going to give my documents comma my open Mings that I'm specifically going to use for this purpose right and later on if I really want to convert this Vector database into retriever all I have to write is Vector DB dot as retriever as retriever if you don't know what is retriever it is an interface which will be able to retrieve the result from this particular Vector database right so here will be my retriever right so this is my second important thing so let me go ahead and write retriever retriever okay okay so I've done I've created one tool I've created one retriever tool is for Wikipedia but still I have I've also installed Rive right so I'm also going to use Rive for the same purpose first let's uh all this particular thing happen now you have that Victor um the vector store retriever completely over here okay now uh the next thing uh that I am probably going to do over here is that I will take this Retriever and use create retriever tool so that I can actually make it as a uh as a as a in short like if I really want to ask any question I really need to make this retriever as a create retriever tool okay so if you go ahead and search for Lang chain for Lang chain create retrieval create retriever tool okay so here you'll be able to see that this is what we are going to specifically use and create retriever tool you can see create a tool to do retrieval of all the documents so we are going to specifically use this other than this if you can definitely check out all the agents that are probably available over here for that purpose right and by that you will be able to implement things so here what I'm going to do I'm going to create my retriever uh tool itself so I will write from Lang chain dot tools. retriever import create retriever tool I will initialize this create retriever tool and here I'm going to use retriever is equal to or whatever retriever name I have and then this will basically be my Langs Smith search right so because that is with respect to the Langs Smith page okay lsmith underscore search okay so that it'll be able to get identified okay like which which tool I'm basically hitting or which tool I'm basically searching okay and here I will give my third parameter and I will say hey search for information about lsmith I'll just copy and paste it I've already done this in one of my projects so so here you can see the third parameter that I'm giving in the create retrieval tool is just like a prompt like what I really want this tool to do search for Information Gain about Langs Smith for any question about Langs Smith you must use this tool so in short when I do this Creator retrieval tool right it is basically creating a tool uh to do the search for that particular page right so here I'm going to write retrieval uncore tool okay and this will get initialized right so I have got one tool over here one tool is Wikipedia and the other tool if you go and see this retriever tool. name this is nothing but lsmith search now the third retrieval tool that I'm actually going to create uh is specifically my own uh RF platform that I have actually developed so for this also I will be having one wrapper and one query Run Okay so this will basically be for my Rift Rift basically means the website where all the research papers have been uploaded okay so let's create one more tool so this is for this particular tool okay and again the same thing that we will try to do is that we have to create a rapper API rapper Rift rapper where again my document Max search will be 200 and this will be basically my for my query run and here I'm actually going to get my ri. name and here you'll be able to see that this is my tool right so this is inbuilt tool that is also provided by Lang chain Wikipedia and RC is provided by Lang chain if you really want to create your own custom tool then you can also create like this but like I have actually shown you right at the end of the day you combine all these particular tools so let me go ahead and combine it so I will write uh tools is equal to and let's go ahead and combine this so the first tool that I have I think it is name is tool only okay so I have ADD written tool so let me go ahead and write wiki wiki over here Wiki okay wiki wiki okay so here I'm going to combine all these things so first one is Wiki the second one is my ARF and the third one is my Langs Smith okay so Lang Smith uh this is nothing but it is a retrieval tool name right so I'm going to basically combine all this tools so finally I got my entire tools over here so this tools is nothing but it is a list of all the tools that that you can see over here with respect to this now my next aim is basically to query from this specific tools right so for this I can do it in multiple ways uh and that is where we will be specifically using agents okay so let me just go ahead and tell you what is the main purpose of Agents agents will be responsible in probably see if I go ahead and show you the documentation also let me go go ahead and show you the documentation agents the core idea of the agent is used to Lang is to use a language model to choose a sequence of action to take in change the sequence of action is hardcoded in code in agent a language model is used as a reasoning engineering to determine uh which action should take place in which order right now here you can see that I have given one two three tool in that specific order so what my agent will basically do is that it will whenever I perform any request right whenever I give any input to my llm model it is first of all going to first search in Wiki if it is not able to get in from this particular tool it is going to go to Rift then it is going to go to retrieval tool right so from these three tools it is going to get the query and it is going to provide you the response okay so let me quickly go ahead and write from Lang chain. agents I'm going to import create create open AI tool agent and here I'm going to Define my agent name and let me go ahead and write create open AI tools agent name and here I'm going to give my llm my tools that I have actually created and one will be my prompt okay so the prompt and Tool have not yet defined so uh what I will do here I will show you an example like how you can probably call your prompts also that is already available in langen Hub but before that let me go ahead and call my open AI API so quickly I will go ahead and call it and then this is what I'm initializing my llm models as okay so I have actually created chat open API so this is what I'm actually going to use okay now let me go so this is my llm model the next thing is that how can I probably create a prompt one way that I've already showed you of creating a prompt is just by using chat prompt template but in langen there is a module called as Hub there people have already created or langen has already created some amazing prompts a generic prompts and they have uploaded over there so in order to call from there so I will write from Lang chain uh from um Lang chain import Hub okay and then we are going to Prem uh give the prompt we are going to get the prompt and the prompt will be of this name okay so we'll also see see get the prompt to use and you can modify this so there is something called as open a function agents uh and this is the username that is present in langin Hub if I go ahead and see what is the prompt over here so I'm getting an error let's see please install langin Hub okay so we also require Lin Hub uh in order to use this see the reason why I'm showing you all these things because so many different options are there Lang chain Hub okay so let me quickly go ahead and run in the terminal pip install requirement. txt Okay so this I will probably Lang chain Lang chenore Hub let me see the error what was the error over there okay it should be something like this okay so I will go ahead and write or see in the requirement. txt the name is different now let me quickly go ahead and write it down away and install it right so here you can probably see yes the installation is done my agents is ready now let me go ahead and execute this now I think you should be able to see the messages okay now you'll be able to see the messages over here now see by default whatever prompt is available in open aai function agent you'll be able to see I have system message prompt template there is prompt variable the input variable is there in template you are a helpful assistant so what we used to do manually everything is available over here the same thing it will have one human message one system message prompt template one human message prompt template right now uh you have the prompt you have everything now we can go ahead and create my open AI tool agents once I probably create my open AI tool agents now you can probably use this agent and execute to Pro get any kind of response now in order to use this particular agents we have to use something called as agent executor okay agent executor please understand the flow the flow is important and that is how you'll be able to understand this so from Lang chain. agents import agent executor okay so agent executor is my next I that I'm going to specifically use and I'll use this agent executor here I'm going to use my agent is equal to agent along with that I will use my tools is equal to tools okay whatever tools I've given and then this verbos will be true so that I'll be able to see all the details whenever I get any response and this will basically be my agent executor so this is basically responsible in executing anything right so now if I go and see my agent executor so here you can see all all the things and it has also added runnable binding arguments over here right now in order to execute anything see if I write agent executor dot invoke so once I write invoke and I give my input and let me give the message tell me about Lang Smith now you should just think that from where should this input come already in our tools we have R platform we have lsmith search right and we have Wikipedia so obviously this this agent will execute and it will interact from the tool that is related to lsmith so once I execute this here you'll be able to see what kind of response I usually get okay so here you can see tell me about lsmith lsmith is a platform for building production G application uh llm application and here and this is probably coming from the tool itself right now here uh mostly uh the thing that you could not see right you could not see over here what exactly the thing was the reason was very simple I did not write the verbos parameter properly now you'll be able to see what details you'll be getting see once you write verbos equal to true now you'll get all the details what it has hit now see invoking Langs Smith search right it is basically searching the langmi Search tool right whatever tool was there right let me go ahead and execute once again again with respect to some different query right and here let me write tell me about machine learning now I don't know from where it'll go ahead and execute it but let's see whether it'll go with Wikipedia or RF okay so agent executor enters invoking Wikipedia with machine learning see and automatically Wikipedia it is probably giving you the entire result okay let me also try with something like RV uh uh platform so uh I have one question with some research paper okay research paper name so let me just go ahead and execute what's the paper all about the paper number is there and in RC we'll be able to find out this see now it is invoking the RF with query uh this particular tool and here you'll be able to get the response and that is the reason why I say this agent executor tools are probably the most important thing whenever you probably develop a rag application so I hope you understood this particular video you like this particular video this was it for my side I'll see you all in the next video have a great ahead in the next upcoming videos I'm going to come up with some more amazing projects as we go ahead so thank you take care have a great day bye-bye
Info
Channel: Krish Naik
Views: 15,676
Rating: undefined out of 5
Keywords: yt:cc=on, langchain tutroials, rag langchain, langchain tools, langchain agents tutorials, langchain agent executor, end to end projects ussing langchain
Id: 2_gSXyt2108
Channel Id: undefined
Length: 24min 36sec (1476 seconds)
Published: Mon Apr 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.