LangGraph Crash Course With Code Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome back to the channel so in today's video we going to go over a simple crash course on L chain L graph basically so I've been making a lot of videos on l l chain so I I you guys are probably familiar with L chain right so now I'm making taking this a bit further we making a video a Scrat course on what is lra and lra is just kind of a part or subp part of lung chain that is used to build agentic behaviors in LM applications so I think in the future of uh the future of AI is going to be more of an agentic uh kind of future whereby you have different AI agents working together to achieve one specific task because uh when you have one specific agent the only way you can make this agent better is through prompting or fine-tuning the LM model or doing some kind of U rug application right but with with an agentic behavior that can super boost performance since uh each agent or each AI model is uh has its own specific prompt and is good that doing one particular thing instead of being a general a generalist right it's now a specialist so we can think of it from that angle of view so L chain is a py python library that help us to do that using L uh L chain so L graph is just a library that help us to do that so in this crash course you're going to look at what is L graph I'll look at the simple demonstration of the project but now I'm going to leave the demonstration out I show demonstration after you build a project we look at some terminology that you need to understand when working with agentic behaviors in LM application and then we finally build a project and build a build a simple project and add grad your UI to it okay so let's get started with it so first thing this is the kind of architecture that you're going to have so you're going to have an AI agent that's going to be a supervisor right supervising all the other agents you're going to have a news a News correspondent because you're you're going to build a news agency that is powered by AI so instead of the news agency or normal news media houses they have what you call news correspondents which are located in different countries or different locations around the globe that provide uh upto-date information to news uh news agencies that that the news agencies use to publish news publication so we have a correspondent we have a news editor so the News correspondent is going to be researching on the internet to get latest information and that information is going to be passed to the news editor who is going to go ahead and write an Edition for the news publication and we pass on this to the ad ads writer which want to write the ads for based on whatever the content of the publication is and then over we have a supervisor which gener supervising all this and then finally will be the user which will be us right so that's basically what the act that you're going to do in each of these uh things right here that I showed you each of these is going to be an agent so going to have a supervisor agent a correspondent agent a news editor agent and each will have his own prompts and own set of tools that it can use to achieve its goals so it's really going to be like instead having one one prompt and one uh agent now we have multiple agents and and each agent is an expert in one particular field that is the whole idea of agentic behaviors in LM applications and you're going to do that using Lang chain L graph sorry okay so now let's go ahead and see what is L graph so L graph is this is uh something I got from their official documentation so lra is a library build uh library for building stateful multiactor applications uh basically multi-agent applications with llms is built on top of longchain and intended to be used with L chain right so it extends the long chain uh expression language right and with the ability to coordinate multiple chains and or actors across multiple steps to in computation Cycles okay in a in a cyclic manner so you might think of this as a dark but I read offici documentation they're saying this is not completely dark instead uh L graph is more like something of buil it's built on top of network X I think it's called a library called Network X which is used to build uh networks right understanding networks in Python other stuff like that so this is not a dou application like Dynamic cly graph is not something like that uh it's actually just used to build uh build like kind of a cycle where by different agent can cooperate in that cycle and that cycle goes on until the task the given task is done right so for example let me just go back in here for example uh you wrote the news right and the editor doesn't find the news important so the editor can send back the news to the sorry the supervisor can send back the news or whatever the publication was to editor to fix some issues right so it can go this CLE can go on in any direction until the given task is done and the supervisor approves of it and that's when uh we get the final output so that's basically what we mean by a cyclic manner okay so now let's look at some cool termin when working with multimodel uh multiactor or multi- agent uh LM application so let's look at first of the fundamental part is an agent what is really an agent an agent signify basically signifies individual members of a team your team right so for example in this case you have a team for a news media house where you have news correspondents a news editor so all of these agents all of these individual components form a team right so each of this individual components is what we call an agent right each distinguished by their specific function right each their news editor there is a News correspondent there is a ads writer each has his own personality has it own history has it own experiences has it own tools that it can use to achieve its Tas we also have task right so task basically the the jobs that we give to these agents to perform so represent the concise and precise assignment designated to each agent taer to their unique capabilities right so for a correspondent agent it has ability to search for our correspondent agent it has the ability to search the internet so it has a tool to do that right so it can use the tool to search the internet right so a tool is a some that help us help it to carry out the task its task right so tools donate the various instruments and devices that the agent employs to carry out their assigned task that's basically so now that's an agent a member of the team a task what the agent is supposed to do and then a tools a bunch of things that help the agent to carry out its task so that's it so uh let's get ahead and jump into the project here because I don't want to be wasting a lot of time explain to you I'll explain to you more as we go through the project so I'm going to be using uh poetry to have this set up so this is my first actually my second video on YouTube where I'm using poetry I did a video last time where I use poetry so this will be the second time I'm using poetry on a YouTube tutorial so I hope you guys are kind of familiar with poetry if you if you have poetry install in a machine just go online and search for python poetry and it's just one command to install it okay depending on operating system so I have it already installed again if you don't like poetry you can always use the PIP command uh python virtual environment that you have been using and you have done I have done so many tutorials all my tutorials so far have been based on python uh VNV so you should be comfortable doing that if you have watched the videos or you can just go back and watch any of my older videos the beginning part or you should see how to use Python virtual environment if you're not comfortable using poetry okay good again if you want you can set up you can is all everything everything in your Global python environment I would not advise that but if you want to do that feel free go ahead okay so let's go ahead and set everything up so the first thing I'm going to do I'm going to go ahead and create a new poetry project so I'm in my tutorial folder where have all my YouTube tutorial I'm going to say poetry uh new and this is new I'm going to call it l graph tutorial just like that and I'm going to give it a hyen name I'm going to call it section I'm going to call it uh sections let me call it sections right here so I'm going to have a folder called uh L graph and instead of this folder I'm going to have another folder called section where I going to keep all individual sections so I'm going to go ahead and simply change that directory into my L graph folder so once I'm in there I can see I have the this couple of folders in there good okay so now once you have that done now I want to go ahead and export my python virtual environment inside of my project so vs code doesn't give us any linting errors or any warnings like that so uh to do that you say poetry config and then virtual envs okay so let me say uh poetry config uh virtual uh virtual envs doin project okay so let me just uh config in part and you're going to say true that's it and then we run that command it's going to go ahead and create for us EnV file soorry yeah a VNV file which vs code will able to see and avoid us getting any error so if you go into sections you'll be able to see that uh andv f v andv file file okay a hidden file okay so once we have that done I'm going to go ahead and install the dependencies I'm going to go ahead and install uh poetry I'm going to sorry I'm going to go ahead and install long chain long graph and L chain open so I'm going to go ahead and say uh poetry uh poetry add a long chain uh long graph L graph and also L chain hyphen chain hyen open AI I'm also going to stall python hyen de okay that's all we need so let me just make sure I got all that uh spelling right okay so long chain and then open a i graph and then L chain okay I also need to go ahead and install Darko gradio and beautiful soup let me just also add all of that all that down here so d d uh d d go that we going to use for d d go search for our search engine to as a tool also going to go ahead and use Python deap which I have already and then I'm going to also install gradio and uh beautiful soup we going to use also for searching the web so beautiful uh beautiful soup for okay I I don't know why they call this Library beautiful soup okay like the coin you're getting from the BR from the Internet is like thought of as a soup but I just find the name really awkward when the first time I ran into this library but basically that's it so once uh we have this running it should go ahead and install that and once all this is installed I'll be back recording yeah so you can see we have an air right here and this a is something I was expecting so let me just go ahead and open up uh this in my vs code and let me show you how we can fix that error particular error so uh let me just go ahead and uh open a vs code and once vs code is opened up let me just go back into my terminal and let me show you why we're getting that error the reason we getting the error is because of the Python versions so what you need to do is just simply copy this and go back inside of our vs code uh open our python P project. Thal file and then simply update the python version which is this right here so just copy whatever you have there and just simply paste it in here okay so make sure you take care of the codes okay so that's all we need to do so once you have this running uh you can go back and uh clear the terminal and then rerun that command again okay so sometimes you might uh most of the time you might run uh you you might run into that error okay so I always run into that error so that's basically how to solve it just update the python virtual uh the python version that you're using as stated right here okay so that's should have it Sol so let's just continue installing and I'll be back once this is done installing okay there so you can see now we have all the libraries and everything installed so now we can begin to code so I'm just going to go back into my vs code right here and once I'm ins of my vs code I'm going to go under the section I'm going to go ahead and create a new folder and I'm going to just call this Basics because this a basic of l l graph maybe later you can make other videos on it so I'm going to call this for the agent. appy okay so instead of this AG you're going to go ahead and import a couple of things so I'm just going to go ahead and paste them in here so going to import fun tools you're going to import operators you're going to import request you're going to import the oper OS for dealing with files and other stuff like that you're going to also go to import a beautiful soup which you're going to use to scrap content from online you're going to also go ahead and import d d go and d d go as I mentioned earlier on is just like the counterart of Google or just a search engine tool and this Library inent enables us to use D that go to actually browse the web and you also going to say from L chain. aent you're going to import the agent executor I'm going to show we going to use this just in a second you're going to go ahead and import or create open a tools that you're going to use to create tools Al going to save from L chain. calls. message you're going to import a human message and the base message from L chain. output pr. open a function you're going to import the Json output function proc also from slang chain. you're going to import prompts prompts you're going to import the chat prompts their message prompts okay so if you probably watched my older videos on longchain you can see that a lot of things have changed right they have moved a lot of things around so previous not importing this template from prompts right right now a lot of things have really changed going to say from L graph you're going to go import the state fold graph and also the end from tools from L chain. tools you're going to import the tool you're going to use this as a decorator to create our own tools from L chain L chain open a you're going to import chart open a so this also has really changed if you are from older long chain we don't have this imported here but now they have really changed that as well and then you're just also importing some type uh type hints from python okay so good so once we have that done now we're going to go ahead and uh follow a couple of Step the first thing I want to do I also want to go ahead and say I'm going to say import I'm also going to import gradal right I have grad imported I'm also going to go ahead and say from uh decouple I'm going to go ahead and import config just like that so good so once we have this there we're going to go ahead and set up a environment variable so I going to keep this environment variable here so I'm say I'm to create a EnV file inste this going to have open uh aior apore key equals to and then you're going to go ahead and get your open API key and you're going to go ahead and add it right going to be something like this right so to get your open API key you can just simply go to open a AI Dot and then simply go ahead and say a loging and once you're logged in you can go ahead and click on the GPT AP API section and then from there you can go ahead and get your API key from here you can just go on this side and then go ahead and get your API keys right here okay so I already have an API key so let me just get that API key and use and then well add it to my file and then I'll be back recording okay because I don't want you guys to have access to this API key because I actually pay for this key okay so it w be fair if you guys have access to it right okay so let me just add it to my my EnV file which will be added in here and then I'll be back recording okay so now I've just updated my with my actual open AP let's go ahead and simply set the environment varable so I to go ahead and simply set environment uh environment variable so environment variables and environment variables will be kept in here so I'm just going to say I'm going to use the os. EnV uh environment right Environ I'm going to Simply add the open API key right here so I'm going to say uh basically open so all this has to be upper case right open aior API underscore key and it has to be exactly this okay so just make sure that you watch out for typos and you also have type the everything capitalized right so now I'm going to go ahead and simply get the the value that you have inside of our API key and our API key also called it open API key so I'm going to go ahead and simply open AI API key so just make sure you paste that here as well so that's basically going to load in our environment variable so now you have an environment variable you're going to go ahead and simply initialize uh let me just say initialize I'm going to go ahead and initialize the model that you're going to be using so initialize uh model so we're going to use the the chart model from open air you just import it right here so I'm going to go ahead and simply copy that I'm going to go down here and simply say llm is equals to this and I'm going to oh sorry my bad I copied the whole line instead of just this part of code and paste it right here and I'm going to go ahead and simply pass in my model name which I'm going to use I'm going to use the I'm going to use the GPT model so GPT 54 and then finally I'm going to use the turbo version so turbo uh Tero and preview right Tero preview and again you can go on the official documentation and check out all the available models that they have okay so I'm going to use a GPT for trouble uh preview uh preview version so once I have that done I'm going to go ahead and Define my custom tools that I'm going to use so I'm going to go ahead and uh Define Uh custom Define custom tools to use so I'm going to have a bunch of C custom tools that I'm going to be using so let me just go ahead and show you how to create the tool so I'm going to use I create a function right here and I going to have this function I'm going to call it um inter internet uh Search uncore tool just like that internet search tool and this tool is going to take in a URL that you want to browse and then this is going to be of type string and this is going to return to you a string at the end of the time right so what this is going to do we need to provide in let's just okay let's just Define what this is going to do so say response it's going to go ahead and browse the internet so it's going to get a response back so the response is going to be equals to requests dot uh this going to be a get request going to get you're going to go as say URL equals to the URL that we we just passed in as parameter so URL that's it and it's going to go ahead and retrieve uh the content of that URL so once we have the content of the URL we can go ahead and simply create a soup out of this explain to you what to beautiful soup means so this is The Soup part of it right so so I'm going say beautiful soup and then simply pass in a response do uh response. content just like that you're going to pass in the response content you're also going to go ahead and pass in the process that you want to use in this case to use HTML uh prer okay HTML pror right there so once we have this done I'm going to go ahead and simply return the following so say return sup. getet text just like that that's all we need to do so once we have this tool done now just by doing this we have a function that brows the internet and return to us the text content this is not a tool to open uh to longchain so how do you make this a tool so we have something imported called tools right here right right uh right here so you're going to use as a direct you're going to use passing as tool and once you pass that tool you're going to go ahead and give the name to so call it internet undor search uncore tool just like that and we can go ahead and simply say return uh let me just go to return uh underscore Direct equals to false so what return direct equals to false means that this tool is going to be used to browse something on the Internet we are saying return return direct equals to false meaning that you can't return whatever you have searched from the internet as an output to the human user right so you use this tool to get some information and once you get some information don't return this information to us work and process this information before you return to us like for example we have the correspondent right he's going to be using the internet to Google stuff once it does it's finished Googling stuff we don't want it to return to us as Google you want it to process whatever it as Google summarize it and pass on pass it on to the other agents to work on it right what's the point of just writing uh creating an AI just to Google stuff online you can you can as well just do that to yourself or even write a simple python function like this and do that as yourself there's no point of creating agents to do that in AI so we don't that's why we saying return direct equal to for because you are forcing the AI to process whatever it has searched before giving us an output not give us a raw output of whatever it has searched so for this to work we also need to add in a function doc string right here because this doc string describes to blank chain or an open AI what this function does right we have a bunch of tools right so what is the description have to tell the AI this tool is used to search things on the internet this tool is used to do X and Y this tool is used to do y and x right that's what you're doing right here so I'm going to say search uh provides uh sear content on the internet right so using you can say using D d. go d. go search just like that you person say used right used you okay so used to and search content on the internet using D so that's basically description of our tool so this how you can create a tool okay so going to create another tool right here going to call this tool that's going use we're going to use that tool to process whatever the AI has has searched online so you search some things online you get the text out now I have another tool for you to process this to get uh content out of it so actually I did an error right here okay so what I have done right here is not actually creating the internet search tool what they have done right here is actually creating the the processing uh tool so so let me just change this to be process Search tool okay process uh Search tool this is a process Search tool and not the internet search tool so my bad right here so this going to be process Search tool I actually confus the tool so this going to be used to process uh content on the content uh content found on the internet using the DU using okay on the internet let's just call it like that so process content found on the internet okay just like that that's all I want to do I have right here okay so process content from uh found on the internet that's basically how this function does okay so this our tool to process stuff on the internet now we need another tool to be able to search things on the internet so now that you have understand how you can create tools let me just go ahead and copy other tool and paste it in so you don't have I don't make this video extremely long okay so I'm just going to go ahead and get this from my GitHub again the GitHub link is going to be made available in the description of the video so I'm going to go ahead and simply uh copy this part right here and then go right here and simply paste it right here okay so uh you guys don't have to spend me uh watch spend me uh your precious time watching me type out all this once you have this bunch of tools you're going to go ahead and say tools put all the tools in one thing so tools is going to be a list of tools and you're going to go ahead and pass in the tools that you have created right so internet search Tool uh let me just call it internet search uncore tool and let me underscore tool just like that so let me copy this right here pass it in here and then finally get also the second tool copy this right here and pass it in here as well so we have a bunch of tools right here the internet search tool and the process Search tool okay okay so uh once we have that done we are good to go or you can call this process search result I don't know it's it's just fine can leave it like that the name is doesn't really matter I think the most important thing is the description okay so a can know what the tool is useful so now that you have that going to write a function that's going to be used to create help to us to create agent so I'm going to go ahead and have this function right here say Dev create score agent and you're going to have an llm and this going to be of chart uh type of chat open a you're going to have a tools bunch of tools which going to be type python list so python list and you're going to have a system uh system _ prompt and the system promt is going to be of type string and and this is going to return to us an agent executor okay so an agent executor right there good so once once you have this function right here this a function that we have is a bit long of a function you can break it down into multiple lines here if that's okay with you okay and have that saved so inste of this uh function you're going to do then do we going to have a prompt and the prompt is going to be equals to a chat uh a chat prompt a prompt template I going to say frommore message okay and messages and you're going to gohe and pass in a list right here so instead of this python list we going to have a couple of things we going to have a a system a system right here this will be a message to the system and this is going to be the system prompt that the user passing for this specific agent okay so going to go ahead and having in a message uh message placeholder in a message is going to have a variable a variable name a a variable uh underscore name and the variable name is going to be uh basically you're going to say message okay and you're going to pass this later on okay so me let's call it messages okay so once we have that let me just not forget to keep my comma right because it's a python list so let me maintain that also going to have a message another message placeholder and it's going to have a variable underscore name and the variable name is going to be equals to an agent agent umore scratch but okay and this is all documented on the official uh official L part documentation so agent scratch part just going be like basically scratch P right for agent to think and then pass thoughts around okay but going to have an agent uh equals to create open AI agent tools you're going to go and pass in our llm right here going to go ahe and LM that you passed in we going to go ahead and pass in our tools that we took and you're going to pass in the prompt from here so pass in the prompt that we created from the system prompt and all the other ones and also going to go ahe and pass in uh the LM that you took from here and also the tools which is going to be these tools right here or whatever tools that you have so once we have that I'm also going to go ahead and create an Executor uh an executor executor an Executor is going to be called it's going to be an agent executor and it's going to be agent is going to be equal the agent that you just created above you're also going to pass the tools and the tools going to be equals to the tools that we have there okay that's it okay so once you have that you're going to return uh the executor because Bally creating uh returning the executor right here as we specified right here returning the executor right so we're going to the execute object right here okay so that's all we need to do so once we have we to create another function that's going to be used to create the agent nodes so basically we have a graph right you can think of it like this let me just go back and right here so we have basically a graph and then each of these are called agents and then these are called ages right so these are the different nodes that I want to create in this part right here so let's go ahead and create those nodes right here so let me just go right here going to have a function def this is going to be called agentcor node and agent node you're going to have a state of that agent we going to have an agent itself you're going to have a name of that specific node right so going to Simply return the you're going to run agent. invoke you're going to invoke uh invoke that agent and you're going to pass in the state okay okay so once we have that you're also going to go ahead and return the foll so going to return you're going to Simply return a dictionary that's going to have a key of message messages and it's going to be a a bunch of U human uh messages human messages you're going to pass in the content and this content going to be equals to the results and you're going to go ahead and say get result get for me the output of that result and from there I'm going to also pass in another key right here and this is going to be um the name the name is going to be equals to name that's it okay so that's all all we need right here so once we have all that done uh we are good to go so basically we have a human message which is content going to be equals to result and then result uh seems okay I made an error right actually returning something which is wrong so I'm going to actually store this in result and not return so result is going to be equals to uh result let me just call it result instead of results okay this is a single result right so result is going to be dictionary having human message that's having all this other stuff within it okay and you also passing the name of that human message itself okay so watch out for the closing and opening parameters and uh uh the string right so make sure everything is properly opened and closed okay so once we have that now let's go ahead and create all the members that are going to be uh inside of our basically our multi- agent application LM application so we going to have the News correspondent you're going to have a news editor going to an add wrer right this are the agents and then we also have a supervisor okay but now we're going to add these three agents so let's go ahead and add those three agents we're going to say these are going to be list of Agents okay so we're going to have to say members are members of our crew right a crew a collection of agent we can also call them crew and you're going to have a newscore correspondent are going to have uh basically uh a news editor and also we going to have an ads uh writer just like that so once we have all these three in place so going to go ahead and simply specify a bunch of things right here we say system basically system underscore prompt so system prompt right here and it's going to be equals to a python uh Tuple or tle whatever you call it so let me just copy this prompt from my kab so I don't I don't uh waste a lot of time typing out all this in fact I'm going to copy this prompt and everything here so copy of that and I'm going to go ahead and add it right here so good so as a supervisor Your Role is to oversee the Insight uh between this agent between these workers and who are these workers B basically the agents the News correspondent the news editor and the ads writer right based on the users request determine which worker should take their next action so the is going to determine which uh which agent runs after what agent right depending on the task at hand uh each worker is responsible for executing a specific task and uh reporting back their findings and progress right so each does their work and reports back to supervisor supervisor sees the work and then assigns which other agent picks up from there or if the work needs to go back to the uh the one who said the agent who send the work to get anything corrected once all task are completed indicate finish say option is going to be equals to finish plus the list of members that we have and then you have a function right here and this function basically we call it a route schema that's going to determine which agent runs next select the next route right basically you have this graph right here can have different connections I didn't show all the con connections but there can be a connection here a connection here a connection here connection here so it determines what the next route to take from this graph right that's what this uh function does okay so once we have that now we can go ahead and begin to create a prompt and you're going to use a prompt to create a supervisor okay so let's go ahead and C create the prompt that's going to be used to use to create the supervisor so I'm going to paste that right here I just copy this from G again the code is going to be available to you the link going be in the description of the video I just want you guys to waste a lot of time watching me type all this out so you're going to have a prompt right here uh Char prompt template from messages you passing the system the system prompt the message Place holder messages as we had above right here right same thing and then finally we have system again another system prompt given the conversation about or who should act next Bic asking the supervisor who should act next or should we uh finish select uh select one of and then options and the options are what we Define right here right either then use correspondent then use editor or the ads writer or choose finish so it depending on which option to choose right here so we choose one of those option that we provided it and then finally we return back to us so we have the supervisor chain right here which is using the Lang chain expression language right which is just uh what Lang graph is based on just an extension of that right so that's basically how we can create the supervisor chain right there so once you have the supervisor chain we can begin to go ahead and create the different uh agents and pass in their prom so going to go on newscore correspondent uh then this correspondent is going to be equals to you're going to say create agent which is just a function that we uh we wrote above so create agent right here create agent you're going to go ahead and pass in our LM tool you're going to pass LM you're going to pass a bunch of tools that this agent can use so each agent can have their own collection of tools right in this case you're just giving all of them the ability to search the internet and process whatever they get from the internet so in other cases you might have different tools for these different agents so in this case we just have all of them using the same tool so you just keep in mind that you can have different tools for different agents now we go ahead and pass in a bunch of prompts right here that this agents can use okay so I'm going to go simply copy the prompts that I have and add it in here because it's going to be a long prompt I I don't want you guys to watch me type out all that prompt okay okay so now that I have that prompt in here so you can see this is a prompt right here your primary role is to function as an intelligent news uh researcher assistant uh de at scoring the internet for the uh for latest and most relevant Trend stories across various sectors like politics technology healthc care Global event you processes that you possess the capability to access a wide range of online news sources blogs and social media platforms to to gather real time information that's basically what the correspondent does search for the latest information so that you have the correspondent created now to go ahead and create the correspondent know so to go ahead and simply say newscore correspondent uh let me just copy this instead of just typing it all out again so underscore node you're going to create a node is going to be equals to uh basically tools sorry fun tools that you have imported at the very beginning of the of the video so fun Tool uh fun tool let me just get fun tools okay fun tools. partial and you're going to go ahead and space the phone going to say agent note which is just a function that we have above there going to go and pass in the agent equals to newscore correspondent which just right here can just P that News correspondent and if you can also pass in the name and the name can be uh news this going to be a string so news basically let me just copy what I have News correspondent that's that's it so that's basically what we have right here so I can also just underscore this and call it uh agent just like that and I can copy that and also paste it here so agent is a News correspondent agent we pass in this function and you passing also the name of this specific uh correspondent this sorry this specific uh agent which is going to be called The News correspondent agent okay so once we have that done we are good to go so you can go ahead and follow the same process and create all the the three other agent which are going to be uh let me just show you right here the supervisor the news editor agent and also the new new ads writer agent so just take your time just copy this code duplicate it uh three times okay two more times copy paste it two more times and then update it the prom to be able to create uh to update the prompts to uh to that specific agent task and also then create the different notes for it so let me just go ahead and do that and I'll be back recording okay guys so you can see I have all those done right here I have the news edit agent cre the the not for it and also I have the News ads right with also the notes right here so once we have all that done we are good to go so now we can go ahead and begin to define the agent States right because we I saw that each agent have its own State maintains all the individual interaction between different agents uh then to keep agents aware of context and what each agent has been doing so far so basically you aware of what each each agent is going to be aware of what AG other agent has done and the previous communication between him and that specific agent that's what this function does so say messages and message going to Beal to going to use annotated annotated uh typing right here annotated and you're going to go ahead and pass in a sequence right here sequence type so sequence and then fin you're going to go ah and simply say base a message okay B message not B exception B message right here and then finally you're going to go ahead and passing uh Operator Operator okay op I got that wrong so operator sometime typ H is just really annoying right so operator ad that's it that's it that's all we need to do and you're going to gohe and say next and next is going to be of type A String right that's what this simply does so keep track of all the messages so that each agent can be aware of what the other agents have done or are currently doing and what their previous conversation between them have been so once we have done this now we ready to go ahead and create the stateful graph so now we're going to go ahead and call uh create the workflow yeah it's also called workflow or the graphs so this is the stateful graph or the workflow so I'm going to Simply call it workflow right here and workflow is going to be called I'm going to say state state graph and I'm going to go ahead and pass in my agent state right here which is the function sorry the class that we just created above right here good so that's going to go ahead and create for us a workflow so create for us a workflow or graph so create workflow or graph that's what this uh this instantiation does okay so once we have this right here you can begin to Simply add nodes to our graph so now we have a graph now adding nodes to this graph so each basically we have the graph and each of these are nodes right and these are edges so node node node node node no right so let's go ahead and simply add each of those no so say workflow do addore node you're going to go ahead and say key and the key is going to be the name of what this node is you're going to have the supervisor node the supervisor node right as indicator you have a supervisor note this is going to be called this is going to be the supervisor agent right you say action is going to be equals to the supervisor underscore chain that we created supervisor chain and if you to do we going to say workflow do addore node and you're going to say key and this is going to be uh I think now we need to have the News correspondent right so we say equals to you say News correspondent and you're going to have an action for this that's going to be using the action is going to be equals to the newscore correspondent uh Noe right there that's it and you're going to Simply say workflow and add all the other no workflow. addore node and you're going say key is going to be equals to and you're going to have another workflow key right here and this is going to be uh you're going to call this news editor and this new editor is going to have an action and the action is going to be basically newscore editor underscore node right that's it you're also going to have another workflow I simply just copy this right here copy paste it right here and this is going to be the ads underscore writer and this is going to be the ads writer so addscore wrer note right adds wrer note right here so once we have all those noes added now we can go ahead and begin to define the ages so Define ages the over graph so you have the nodes now the no no no no going to add the connection between these noes so we we going to have you're going to use a follow for this say for me member uh for member in members for member in members what you're going to do going say workflow uh workflow. add H uh H right here you going to go and say start uh start uncore key and the start keys let me just and do that so start key and the start key is going to be equals the foll so start key is going to Beals to member and where does this member and then where does this n key goes so n key and the N key is going to be equal to the supervisor so make sure that is the same names that you have right here so basically each member can talk to the supervisor can report back to the supervisor okay so each of those ages starts from the supervisor starts from the member and ends are the supervisor so that's basically what you're doing right here okay so each me can report to the supervisor that's the AR the architecture that we have in case I I I didn't actually indicate this properly in here but you could also do that as well okay okay so now we going to go ahead and add in a conditional map so what a conditional map and doing a conditional map so let me just go ahead and first of all add in this conditional map and let me explain to you what this conditional map is all about yeah so this is the code that we need for the conditional map so condition basically U if a task is finished right supervisor won't send task to to an agent else the supervisor will keep on sending task to an agent until it's done right this is what the conditional AG does if a task is finished won't send a request back to that agent but if a task is that is not finished we simply continue sending back a a request that agent so this is really really important that's why I have it the commented in written a comment about it right so that's what basically the conditional statement does it just keeps on keeping the loop going until we get back what you want so basically we are done building our graph so we can just go ahead and compile the graph and then run it but uh before you can compile you can also just print and the the workflow and be able to see different things from the workflow so workflow uh workflow dot we can see the branches you can see different branches you can also print uh workflow do uh ages this will show as a different ages and then you can past this a couple of times so ages and then you can pass in the nodes and also you can pass in the channels so the different channels you can of of this uh of this uh workflow or this graft that we have created right here so that's basically you can this actually channels I know Channel okay so if you run this code right now you should be able to see all this output okay so hopefully you don't have any error in the code so let me just go ahead and change directory into section and then uh let me see okay if I I'm going to say poetry run Python 3 and then finally agentp okay so this should just return to us all the branches in our graph all the ages in our graph and you define all the ages all the different notes that you have added and also the different channels so that what does the channel channels do the channels just show us the intermediate steps that the different agents can take so that's all all about channels just a different intermediate steps that all these agents can basically take so that's basically it and we should get that running just in a short well and also you can just go once we have this I just want to show you this let me just continue just to save on time if that is going running I'll be back sometimes it just takes long time okay so I'm going to go ahead and create the graph instance and say graph and then graph is going to be equals to workflow do compile and this going to and simply compile the graph so we have created a graph now we are compiling the graph into a runable c that you can run okay okay so I have an an error right here so about the let me just read error properly so Pro missing requir variable agent scratch P okay so let me just check that somewhere above above all the way yeah here so agent underscore oh scratch okay I got the spelling there right is scratch bad okay so my bad so once I have it done uh let me go ahead and run this one more time okay so hopefully now we don't have any uh types or anything like that so actually begin to actually uh make request and see these different agents work together to accomplish a given T so in this case what they want to do the different way you can actually stream this content out if you want to uh or or you can just actually just print it out but now for now I just want to stream it out so let's just copy the code and paste it in here and then we can give this agent task and let's see how uh they're able to perform this task while cooperating together so let's check if this is running so you have another type when your type annotated uh cannot be instantiated okay so let me see uh annotated probably made an error was going a bit too fast so anotated uh actually this actually yeah it can be instantiated of course it's going to be a list sorry uh at list and then passing those stuff instead so my bad so I hope I didn't make any other types clear this let's have running okay well that is running let me go ahead and begin to write the code that is going to be be able to uh you can give an agent a task basically TK I want to give the agent is to write a news report on Space X right and then I want to see what the output is then so now that you have all that in place we can now move on to the streaming part of uh of the of the tutorial so now I also went ahead and did some changes before I I think we to move on to the streaming part I went ahead and look at the code I noticed we made an error right here right we called it streaming uh sorry editors uh node I changed down sorry news editors news editore node I change that to edit news editor just news editor because it's not a node right it's actually the editor itself so just like we have this ones right here okay so that's one of the things I I went ahead and changed okay so once we have that we can begin to run uh we can begin to do test the streaming part now I'm going to go ahead and comment out this because if I print out this thing it's going to go ahead and print out my open a API key and I don't want you guys to have access to that because I have a paid API key so I don't want to give it away way okay so now let me just go ahead and add in the functional that you're going to use for streaming right here so I'm just going to go ahead and paste this code right here so Bas basically this what we need to stream the code so basically I'm just prompting the LM to write me basically the the SW of agents to write me a report on uh Space X after research on Space X pass the findings to the news Editor to generate the final uh publication once done pass it to the ads writer to write the ads on the subject right on the on the on Space X so this I want to this all that we need for streaming so once I have this done again all this going to be in the story link in the description of the video below so once I have all this in place you can begin to run the course I'm just going to go back into my terminal and just simply say poetry uh run and then simply say Python 3 and this is going to be uh basic right so sorry agent okay and then run that so once I I run this now this should you can see you should begin to see the agents having a conversation among each other to do the task that you just assigned them which is just to make a research on Space X to print out the different uh and then pass all the findings to the different agents and each each agent will do it job so in this case you want to do a research on Space X right so the first thing that I expect you to do is to pass on to the News correspondent that's going to use the internet tool that you PR to be to perform a web search and then the findings will be passed on to the news editor and then on on like that and then you can also go back from any of this right you go from editor correspondent to uh to from correspondent from correspondent to news editor and can go any direction but right so now let's go ahead and see what's going on so you can see right now the first thing the supervisor is calling the News correspondent to do the research on Space X so now the correspondent is going to go ahead and use the internet tool to do a research on Space X on the web and it's going to return to us a report B so you can see you have the report right here returned back to us by the News correspondent now the agent is passing this onto the news editor so we went like this this is the chain that they went through correspond the supervisor to the correspondent from the correspondent now to the news editor so from the news editor let's see where it takes next let's see what the supervisor suggest okay so theor the supervisor went to the news editor and also went back again to the news editor maybe the supervisor didn't like what was being published right so it sent it back to the news editor so the news editor is still working on the editing this content that we got from uh this one right here okay so now uh let's see what goes on right here let's see what comes on so the editor is still working on this okay maybe the isn't happy with what the editor did so maybe just passes back for them to do some changes so let's see what are the changes that come up next okay so we can see now the editor uh the edor now returned a news a new report you can see this one is much better than what it wrote in the first place right so it's much longer has more much more context right so now I think theer is now happy with what it got back so it now s it to the News News ads writer which is going to go ahead and generate some ads basing on this content and it's free to use the internet tool to perform some Google searches so yeah sometimes you might see a multiple conversation between the ads sorry the news editor and the supervisor maybe just the isn't happy with what it got back so yeah the conversation can go on for a amount of time and you can control the the amount of like back and forth conversation between the agents using this uh recursion limit right here so you can say this to be i s to a high number of 150 so if you a low number sometimes your computer you might get an error saying that uh the limit has been reached right the russion limit has been has been reached without actually the agent having done the test that you are doing like they had multiple discussion many discussions that were more than the limit that you that you you set initially so sometimes if you get that eror just come back in here and make sure that you uh you update the reuss the agent can have multiple conversations before reaching a final agreement so now we can see we got back finished right here and everything is done good so you can see everything works perfectly now let's uh you can see we have this eror right here right this is just an error basically using uh because we are using asynchronous you can see a task got destroyed but it is still pending right so the the program into it for this task to finish which is just an asynchronous task so I will show you a tool that you can use to solve this this is actually coming from this uh line of code right here so let me just scroll up and show you where this is coming from so it's is coming from this dark go that you're using right uh long chain is not waiting for this to finish before it's it's ending the program so that's why you're getting that error so I'll show you a to that you can use which solves that issue that is suggested by longchain okay so don't worry now let's move on to the part if you if you don't want to stream this you just want to get a final output let me show you how to do that in a long chain so this is the this is a code that you can use to uh to print out the the content having to stream it so let me just go ahead and comment out this I also we also need to set the russion limit again so I'm just going to copy that and paste it right here so paste it right here that's going to be the russion limit and then have that saved so once I have that there I'm going going to go ahead and close this right here I'm just going to go ahead and comment it out so comment this out because it's for the streaming and again don't worry about all this code I'm going to make the code available on GitHub the links going be in the description of the video below so once I have this now let's run this code again and let's now now not stream it but now get the final findings of the AI agents right there's no no streaming content so this will take a bit of time so just be patient while this runs uh in the background just wait at the end of the time when it's completed you should now see the final output which is going to be final response you're getting the messages you're getting the first one and you're simply getting each content which is going to print out uh the the final final findings of this SW of agents that have worked together right the final result of this uh SW of Agents so just be patient and can take a bit of time but just wait and it's going to get complete yeah so you can see got back the final output right here and this is the final output report on Space X latest news and updates 6 x blah blah and you can see you also have the links to those clicks like we to include the links whatever it just find right so achievement 23 and all that and everything that we as to do as stand that good so everything is working just fine I again you can see we having this little one error right here which you need to deal with so let me show you how to deal with this era but before that let me let let's go ahead and create a simple gradual UI whereby we can actually see this in a gradal UI and run it on the web okay so let's go ahead and Implement that using grad okay so uh to do that first of all what you have to do let me just comment this out as well again the code will be available to you guys so don't worry about this and I'm going to make sure let's make make sure we have gradio imported so I'm going to go in here and say import gradio I'm going to import gradio as gr okay so once we have gr UT as gr let me go ahead and add in the code content that is going to go and create for us grad UI I have a function that gradio can call in order to pass in the prompts that we are going to pass in okay so let me just go ahead and add that in there okay so you can see just adding that functionality in there so this this is basically it so once we run this we should gradi your UI and which has a function that you're calling that is going to invoke uh the a graphs to get passing the message that you're going to pass in from the gradio UI basically let's copy this message right here I'm just going to copy it this you're going to pass into gradi your UI so I'm going to go ahead and uh copy that just copy that okay good so once I have that copied now we can go ahead and run this code again so I'm go into my terminal and clear the terminal and then simply run the code again now this should Now set up a grad UI and then you can open open up this in the web browser okay so uh just a second uh this shouldn't take that long I don't know why it's taking so long okay let me see if my PC is performance mode yeah it is why is it taking so long okay because I'm recording the video but let's give it a bit of time okay there it go so I'm just going to go ahead and simply copy this copy it right here uh copy the link and I'm going to go into my uh browser right here open up a new a new terminal and then have a paste that right there okay and then run that okay so once this is running I'm going to go ahead and grab the the promps that we are going to go ahead and pass in so just go ahead and copy all this right here okay you can basically type me anything that you want but just to save on time when to copy this that you already have and go ahead and paste it in here so paste it in here and then let me format uh this to remove those uh pound signs from there so get rid of those and also here I'll do the same similar thing okay so once that's done I'm just going to go ahead and click on submit and that's going to go ahead and send this to gradi uh to the sorry our graphs our sum of agents to do a research on Space X and then after research on Space X pass the result I hope you guys can see this let me zoom in a bit so this Bic I have passing right here and let me expand this a bit okay so you guys can see this clearly so yeah that's basically it what I have passed right here so he going to perform a research on this and then he going to display the findings in here this can take up to about 1 minute or 1 minute 30 seconds depending on the amount of traffic on open Ai apis and how many how many rounds of conversation our agents have to go through and sometimes again you might get an error saying you have reached the limit of this so what if you get that error again you just copy this right here this right line of code copy it right here and then simply paste it in here as well so comma and then paste it in here and and that should now deal with uh in case you are having any issues on uh the agent having too many conversation and you're over uh you're exceeding that limit or that you have S so the limit is I think is about by the default I think is about 25 if I'm not mistaken yeah so sometimes you might need to add this in there to avoid any error on the you exceeding the limit so that's basically it okay so now let's just give it a bit of time and now when this is done okay you have passed one minute right here so anytime from here we should now be able to see uh the final uh response from the sum of Agents right okay now you can see you have a result a result right here so yeah that's the result right here so you can see uh Space X report a glimpse into the 2023 and Ro 2024 and you can see all the reports right here that you have got back and this are the ads that they wrote for so so you can take your time and read through it imagine this was your kind of like your school assignment and you just have this agents doing for yes create a bunch of Agents doing for your school assignment and writing for your report like that's crazy right yeah but now uh we in an era where we can actually do that so you can see we got the report right here good so that's basically how it is and again we can go back into the terminal and I talked earlier on of this era that you're getting this ER right here okay so let me show you how to fix it okay so let go back into my terminal and once I'm enter my terminal I'm going to go into the L chain off your documentation so things they suggest to uh to solve this issue is installing uh Trav Trav trail. python right Trav hyen python so you need to install this so let copy this uh you can go back in here I'm going to stop this for now by hitting control and c and simply say poetry add and then simply simply paste that in there that's going to go ahead and add it so I've already added it so you don't have to uh watch me waste a lot of time on this I already added it so you can see you have it right here Trav python okay good so once you have that that added now what you need to do is actually follow the rest of the instructions so we need to go ahead and get an API key so you can just get go go ahead here and get an AP key just click on this link it should bring you to your website where you sign up once you sign up it brings you to the official uh like your account page in your account page you have an API key right here I don't want to show you my API key but just simply copy that API key and once you have it copied uh go back inside of your EnV file that you have EnV file here mean EnV and then simply copy this copy this right here you simply copy it and then go back into EnV file and inste of your EnV file let me just comment this out for now so instead of your file I can't show because has my API key so you just p p this in of your file equals to and then paste the API key that you get from your Trav account so you just go ahead and copy this API key right here copy it and then you go ahead and paste it here so copy it and then go ahead and paste it right here and then follow some Gibb stuff right so yeah that's basically how you can uh sorry some Gibb stuff that's basically all you need to do inside of your EMV file I don't want to show you my EMV file right now because it has my my API Keys which I don't want you guys to have access to good so once you have that done uh you can go ahead now and add this to to to to as a Search tool because Trav is a Search tool and so let let's read the instruction on how to add this so we need to go ahead and import this so let's go ahead and copy this copy this go back in here and just scroll all the way to the top and then simply you can add it right here so add it right there okay good so once you have it added right there now you can go ahead and actually add it in here so so I'm going to copy this duplicate it and then go back into the official documentation and uh copy this right here copy it and then go back in here paste it here and then also paste it here as well so it's going to read it from the EnV file okay good so so make sure that you have it of your EnV file as we discussed okay so once we have you have that now we can remove the internet search tool and pass in Thea Trav Search tool which does not Ser from the era that you're getting okay so just simply copy this and then simply go in here and paste it in there good so once you have this done now you can go ahead and now uh do like a stream or you can actually just not do a stream but just do a normal uh noral get the normal final respon whatever works for you so once you have that in there go back into your terminal and then run your python code again so poetry run python three and then agents. P so this will now begin to run it using the Trav internet search tool which is going to go ahead and browse the internet uh okay and get results now it don't suff from that asynchronous issue that uh that go is suffering from because this tool is designed specifically to be used by AI agent so it takes all that into account right so that's one of the advantage of using it yeah but uh now let's see this can take a bit of time as usual so just give it be patient and give it a bit of time yeah so you can see completed that task right here it took a bit of time because a lot of back and forth conversation between the news editor and the supervisor but basically we have the final report right there so you can see the the different uh conversations and right we we didn't actually get any of those warnings or errors anymore because you're using now travil right so now those are the things that you uh you can take a look at using travil and there other tools that longchain comes with so you can use those longchain tools or also open AI tools and other stuff like that so guys that's all I wanted us to cover in this video I hope you found this helpful and you actually learn something from uh this video okay so I one you can easily I also have another video on crew AI crei course that goes over the same project that we did in today's video so you can check out that video on crew AI it's much much if you're looking for something very ver is much much simpler uh but uh uh it's also based on Lang chain as well so yeah if you want to take a look you can take a look at crew Ai and I'm also going to be making other future videos on athentic AI behaviors in llm Bas application so guys if you like this video make sure that you like U uh if you like the video make sure you like hit the like button you subscribe to YouTube channel and you can support me father if you want to you can buy me coffee the links are going to be in the description of the video below so guys thanks for watching and see you in the next one keep safe again the code and all the that you use are going to be in the kab rep story The Links going be in the description of the video thank you guys for watching and see you in the next one keep safe
Info
Channel: Code With Prince
Views: 4,175
Rating: undefined out of 5
Keywords: codewithprince, programmingchannel, python, python devs, funcoding, langchain, langsmith, langgraph, multi-agent, OpenAI, nodes, LLM, large language models, tavily, generative ai, gen ai, RAG, AI chatbot, chatbots, openai, tech, coding, machine learning, ML, NLP, chatgpt, gemini, google, meta ai, llama index, vector database, LangGraph, Langchain v0.1, GPT4V, GPT-4, GPT4, Tavily, search chatbot, search system, search app, RAG search, perplexity vs google, langchain updates, langchain new version, DAG
Id: lQ5r2AvlP0Q
Channel Id: undefined
Length: 63min 37sec (3817 seconds)
Published: Wed Feb 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.