hello everyone welcome to AI anytime channel in this video we're going to look at an agentic rag flow and how we are going to build uh build an AI agent that will help you perform retrieval augmented generation and also if the relevant informations are not present in the vector database how it can also connect with the Web Internet and I know discover or feates the right information for you so we're going to just do agentic rag workflow using crew AI so crew AI is a multi-agent framework based on top of Lang chain so they have written all the high level abstract classes for you so do not have to kind of create the agents from scratch so crew AI one of the most uh repositories lately on GitHub for building AI agents we have lot of other Frameworks libraries tools whatever you call it for example langra compos you know crew AI is one of them defi gum Loop so on and so forth using all of these you can build AI agents okay and in this video we're going to look at crew Ai and how we are going to build an agentic rag workflow so we're going to upload a document and we're going to ask a questions and we'll see if it's able to generate answers from it if the answers are not there we're going to use some external apis like taviz serer Etc and we'll see if that can fetches the information from internet now without we jump into the uh this experimentation guys I want to introduce our Discord community of AI anytime we are already 300 people and we have recently announced a giveaway you can see this is how our you know Community looks like where you have like 297 members as you can see we already have couple of events going on Discord Nitro challenge we are giving Nitros uh in away and then we have our merchandise limited edition AI anytime merchandise for the community pillars so join our Discord Community guys take advantages learn from these 300 people that you have exchange ideas you can see we have different things we have uh you know Global chat we have verified chat like people who are verified they can take advantages of freelancing gigs and so on and so forth we have hackathons we have announced couple of hackerson one is Bank of B the hackerson you can see one is cod cubicle hackerson at Microsoft Office we have some project showcase Channel where we're going to Showcase some projects we have help channel for data science jna where people help each other to learn this technology faster and then we have development and here you can see that we discuss about development things so on and so forth so join our Discord we are growing fast on the Discord and we have a lot waiting for you now let's jump into our code guys here here so if you look at what I'm doing I I have installed crew AI crew AI tools and Lang chain Community because I'm going to use these libraries to build this workflow and I'm doing this in collab notebook feel free to do this in vs code jupyter notebook is Standalone wherever you want to do that once you install the specific version you have to restart the session after that I'm installing Lang chain Gro because I'm going to use Gro Cloud you can see I have a key so Gro basically gives you free keys for now they will not give it forever okay so they have deployed on their infrastructure models like Lama 3 mistl so on and so forth and they are doing all this marketing giving it in free because they have to raise a lot of funds and stuff so you can use it for free for your hobby projects they have something called lpu you know some uh uh no not lpu some tensor Processing Unit or something excuse me I for or language processing I forgot the exact thing which is basic very different than the way we use GPU and stuff so you can use grow Cloud okay uh for free don't use it in production guys I don't recommend using in production okay if you're working with sensitive data deploy through all Lama and stuff and work on that now Lang chain Gro and then I have sentence Transformer we're going to use some Ming model over there and this is what the libraries that you can see so we have chat open AI where we're going to pass our model so chat open a has compatibility you know with I'll just get rid of this Discord for now we have compatibility with different llms you know in this chat open AI class that you see and then we have PDF Search tool that we are using because we're going to upload a PDF file so from crew AI tools we are importing a PDF Search tool if you have a document Search tool you can use doc Search tool you have wave Search tool you have a lot of different types of tools guys in in crew AI documentation you can find it out then I'm using taviz search result tavil is a fantastic platform that helps you with information Discovery so we would have seen about you would have seen companies like perplexity okay so where you go and you know that basically helps you synthesize responses from internet so do not have to read tens and hundreds of links on Google it basically asks a question it gives you a very specific answer so that's what tavite does it provides you API to connect with web and that's what I'm using now here you can see what we are doing we have some crew things coming up crew task agent I'm going to explain that and user data because we have all our credentials uh sitting here in the left hand side so you can see these are our creden where I have defined I have Gro API key I have tavil API key I have HF token blah blah blah okay now I'm going to run this and once I run it you know you can see it will take a bit of time to run that uh you know the library stuff and the next thing is I'm in you know just loading the environmental key you know the secrets key from the environment and you can see os. inan gr API USG gr API you can also use os. G EnV if you want runtime disconnected for whatever reason is fine okay let's wait for that guys I'm just going to run this here s is already running let's see and after that we have our chat open we passing our openai API base where we uh giving this end point for Gro Cloud uh we are using open a compatible end points and you can see we are using this we are using Lama 38b this is the context window our temperature is very less and we have a very less Max token so let me just run this and then I'm am using a rag tool so I'm creating a rag tool variable over here and if you look at this what I'm doing I'm using a PDF Search tool which is from crew aai and inside that I am passing my document so my document name is not doc PDF my name is isoro PDF so I have a document of isoro PDF and it will download anyway so I'll just show you this PDF you can see this is the PDF that we have is sporo Health we have problem what we do what you get with investing at sporo blah blah blah so I have a single PDF and I want to retrieve some information so I'm passing that PDF and then I have a config schema over here in the config I'm passing a bunch of dictionary so because it's you have to Define that in the schema then my provider is Gro if you do not want to use Gro you can use Gemini models from Google you can use uh GPD models from open AI you can use Claud from anthropic and Lama 2 from HF now I'm doing that I'm passing my model name you can again set inference parameter over here I'm not doing it and then I have an Eder so I have two things I have the llm I have the Eder so in the embedder I'm writing hugging face which is you can also do o Lama run it locally and then open AI for large or add whatever and I'm using small BGE by Bing AI Academy and then I'm not specifying retrieval or title you can do that now I'm just going to run this here let's run it so when you run the rag tool for the first time it might take a bit of time it all depends on how big your document is if you have n number of pages and there are more pages in the document it might take a lot of time and if you're running it for the first time it has to download the model from hugging face repository the sentence Transformer you can see it's downloading the BGE model uh you know in this cache of this environment on collab and you can see it says inserting batches in chroma DB so it's automatically you know by default it us just chroma DB you can again have privilege to change that as well if you want to do that and you can see this is done okay now I'm asking what does sporo Health do so let me just run this I'm just doing rag tool.
run and you can see it's pretty fast it says using tool search PDFs content now it gives me a relevant content isoro health problems you know doctor spend 2 hours per day blah blah blah and you know it's kind of giving you an answer okay and now I'm going to use Tavi API key so Tavi as I told you that is information Discovery platform I'm only using top three documents from Tav and I'm just going to ask what does isoro Health do it will give me a bunch of things you can see it's the URL also gives me the URL you can open this URL it goes to esoro health. com and it opens something over here that's not our concern and sporo synopsis searching through blah blah blah it has a Content so these are all raw context guys now we will pass these to llms of course we will not do it agents will do it the agents that we're going to Define and they're going to look at this raw context and then synthesize the response for you so it makes a very beautiful answer in the end so this is one thing and now I'm writing a router so if you look at a decorator as called decorator is my tool I'm defining a tool at a decorator and I'm having a function called router under score tool and I'm passing my question and in the router I'm saying and these are not hardcoded because agent will be able to understand this but there are some phol that you are giving to an agent that why it should go to web so what we are saying if isoro Health in question return Vector store else return wave search so very simple function you can make it a bit more comprehensive and then I have a router agent so if you look at the router agent I'm using an agent and I'm writing role which is router and I'm giving it a goal the goal of this agent is to Route user questions to a vector store or a web search so Vector store which is locally of course it can be on cloud as well but in this case it's locally persistent and this environment and on web search it will go on internet and get the information I'm giving a backstory that's how crew so basically crew has uh role based goal backstory Etc you can Define it you can also and also talk about allow delegation you can pass One agent to the other agent and they can help each other so in back story what I'm writing you are an expert at rooting a user question to a vector store or web search use the vector store for questions on concept related to rag you do not need to be and look at this line you do not need to be stringent with the keywords in the question to this topic so it will not be hardcoded and only looking at sporo Health it will make judgment on the Fly and I'm saying verbos equals to true I would like to see all the logs and then I'm saying allow delegation false for this agent I don't want this to take anybody's sell because this is the first one in the workflow and then llm equals to llm so let me just run this here I don't know if I have run this okay I'll just run it I'll run this and then my next is the retriever agent so it retrieves from the vector database if you look at here very simple guys you know role retriever goal blah blah blah I think I'll just make this a bit better here so let me just make a roll and then I have a goal and and then I have a backstory so in the back story you can see it's pretty standard okay so I'm just going to make this a bit organized okay and here we go and then we have veros allow delegation and here I want to again say false because I don't want you to take anybody's help in this case and I'm just going to run this here so let's just run it so our retriever agent is also done then we have a greater agent now this is important now because and you can let me see if that yeah I'll just make this a bit organized as well it basically finds out if there's something wrong in the retrieval because the biggest problem in rag guys the retrieval okay retrieval uh is not that next level or the next gen right now when it comes to it okay so you can see this is a great one it basically grades that you are a grader assessing relevance of a retrieved document to us question so it looks at all the retrieved chunks context and then find out the basically graded and then here we have this looks nice I'm just going to go back here and do it we have an hallucination grader that looks at hallucinations filter out hallucinations if the answer is not grounded or supported by citations and stuff so that's the one agent that you see here the next agent so it's a multi-agent flow guys the next is answer grader I'll just also make this a bit and it basically filter out hallucinations from the answer so basically it filters out everything okay from there so I'll just run this and then I have a router task now in the router task what I'm doing analyze the keywords in the question I'm passing the questions and then it decides based on the keywords it decide whether it has to go to the vector store or it has to go to the web search so understand this more like a prompt classifier guys you know no rocket science even logistic regression can Sol this problem but then you have to write a lot of custom code by yourself and then I have a retriever task uh same way in the retriever task so first you define agent then you define the task you can see I have task router retriever grader and hallucinations for all the agent that you defined you have to define the tasks for them so what are the tasks that they're going to do and then you just Define the agent so I'm assigning agent for each task and you can see grader hallucinations task answer task and once you are you're going to Define you're going to create a flow using crew and you can see I'm defining my agents and the task and I'm call it rack crew so let me just run this and once I run it I'm asking a question does spor stimulate p in chart review I'll change this question I'm going to say what uh let me see uh tell me about isoro health isoro health and what and what they do I'm running this inputs and now I'm going to run this result and the result is first you define agents you create task you assign task and then you do kickoff you know that's what we are using so we are doing rack crew.