LangGraph: Hierarchical Agents - How to build Boss & Subordinate Agents

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone in this video going to show you how you can create a hierarchical multi-agent system with langra we will build up on the knowledge of the introduction video for this video so if you have not seen it yet First Watch the introduction to langra because this will be a rather complex example so here it is we will create an agent system where we have multiple autonomous agents where llm is the key part of each agent one evaluator agent which decides whether an article is interesting enough to publish or not and if yes the article gets passed to the news chef who decides whether the article has be translated or Rewritten if the article is fine it gets passed to the publisher who publishes the article important the translator and the expander only report back to the new chef but have no authority to pass the article to the publisher so this is where we've got our hierarchy implemented let's directly jump into the code you will find the link to the code in the description and if you clone the repository then please open the agent team IPython notebook here will find the header team of Agents with a supervisor so this is what we're going to implement we first going to start by implementing the evaluator agent the evaluator agent is responsible for telling the news Chef if an article is relevant to be posted or not so we going to make that by user by using a structured output and we're going to use a penic class here so we inherit from a base model and here in the attribute section we Define the attribute binary score which is a string and in the field we provide a description for the llm so the description is the article is about football transfers yes or no so this is what we going to get back from the llm then we instantiate the chat openi class we um pass the transfer news grader here as argument to the with structured output method and then we Define our chat prompt template so in our system message we say you are our greater assessing whether a news article concerns a football transfer and so on and so on so provide a binary score yes or no to indicate whether the news um that is about a football transfer or not then we pass that system message here system message to the from messages method and we pass a human message which comes from the state later and here we pass in the original article we want to evaluate and then we use the Lang and expression language and pipe the prompt to the structured llm grader so this is our chain and we call it evaluator so let executed this code and now we can try to invoke that there are rumors that Messi would switch from Real Madrid to FC Barcelona or in the other direction and now we can have a look at how the evaluation looks like so this the result binary score is yes and we can see yeah this is how it works with a structural output and we can also access here the binary score attrib rute so we just get the string yes so that was quite easy let's now continue with the new Chef who is responsible for evaluating if an article is ready to be posted yes or no and if it meets the word count so an article has to have at least 200 words yes or no then it has to be sensationalistic and if that's not the case we say it's not and otherwise it's yes and also we only want to provide an German article so if if the is German we output yes and otherwise we output no and the responsibility for the new Chef is to evaluate that all these attributes here are yes then the article can be posted otherwise they cannot be posted let's say we only got 50 words then the article will be routed to the expander who will then expand the article to at least 200 words if it's in English let's say then it will be routed to the trans Lor and this will translate the article to German so that's how the workflow works so let's first do that independently so we create our chat openi class again we pass the structured output we pass here the article possibility grader and again we have a system message you are a greater assessing whether an new article is already posted or not make it in yes or no and then we pipe again our prompt template to the structured output llm and again we can check our result here so let's invoke that article is messy Real Madrid to have the Barcelona vexel so this is already in German that's the same like before and here we can see cannot be posted because it doesn't meet the requirements yet word count no sensationalistic is no and the language is yes because because that is already in German so this is how it works so the logic for each of our agents keeps the same and now we're going to implement the subordinates of the new Chef so this is the translation agent who is responsible for translating an article into German and here we've got a simple translation system message your translator converting articles into German and we again pipe that to the llm we don't need a structural output here and because we just want to translate the article so let's invoke that I guess nothing special so here we've got this article in English and here is the content in German so not really difficult in this case again the the expander who is responsible for writing an article in at least 200 words otherwise because it's not ready to be posted so here we've got only a few words and the expander is then responsible for creating a longer article here you can see this article is much longer okay now we've got all of our agents or LMS that we need now we can combine that to energetic workflow with the Lang graph so the actual system is quite easy so we only got an article state which always is evaluated we don't have any more attributes than this article state so let's use that as state for our graph so now let's define some functions which are used in the nodes of L graph so the first one is get transfer news grd so this is where the evaluator works and the valuator actually does nothing with the state so we only pass the state and return the same state again we add some print statements but actually the evaluator is not responsible for doing anything with the article then we've got the news chef and again the news Chef is not responsible for doing anything with the article so we again pass the stateus argument and return the same state as before and then we've got the translate article function so this is where we actually access the article so we going to do this just like a normal dictionary we pass the state and we access here the uh article by using the article State key and then we pass the article here to the translator and then we overwrite the article State and then we return the whole state again the same applies for the expander which is responsible for writing at least 200 words so we access the article Again by accessing this article State key we pass that to the expander and we overwrite the state by overriding that with the result do content and again we convert and we return the whole state again so quite simple actually and then we've got the publisher and the publisher also does nothing with the article only is responsible for publishing it so again we pass the state and return the state and here we could actually do something with it okay so far we only created functions which changed the state but now we have to create functions which are used in the condition edges these functions do not return a state but return a string and based on the output of such a router function that determines the next action in the agent workflow so here we've got our evaluator router and we evaluate whether an article can be is relevant or not so if it's related to football transfers then we grade it as yes and other we create it as no and if we say yes then we want to Route it to the news chef and this will be used in a mapping later in the conditional edges so if you scroll down a little bit here you can see that the evaluator workflow is is such that if um it's yes then we will return news chef and this will route to the news Chef node and if it's not relevant then we will return an end node so the workflow is finished so this is how this works with the conditional workflows so we've got two conditional workflows and that is the evaluator which routes to the new Chef or otherwise to the end note when it's not relevant and then we've got the news Chef router which evaluates if an article can be posted then it will be directly routed to the publisher so if that's not the case but the language is already German then we will have a look at the word count and if it's s journalistic and if that's not the case we will route to the expander and it's if it's not German then we will route to the translator so these theel and the new Chef are our conditional workflows and now we've everything to create our final workflow so we pass the agent state which we created earlier which just is this simple type dick with a an article State attribute so we pass this to the state graph and then we create our nodes so we use the add note method of the workflow and as key we provide the value first and here we set the function to get Transfer News grade so nothing happens here this will not change the state same applies for the new chef but for the translator we pass the translate article function where actually we work with a state and return a changed State same for the expander where we expand the article to 200 words and in the publisher route we actually do nothing again so this the publisher route takes the state and Returns the state again so these are the only two functions which actually do something with the state we set the entry point then which is the evaluator so first every article gets evaluated and then passed to the new Chef or not the next step is to create our conditional edges so the conditional edges Define where the workflow goes next so in this evaluator router function we've got multiple outputs the new Chef are not relevant so again I going to have a look at that to make it clearer so we've got this new Chef string and this not relevant string and these are used in the mapping here and if that's the case then we will route to the news Chef node so we will run this and otherwise we will go to the end node and this will end the workflow we do the same for the new chef and here if the output of the new Chef router is translator we will route to the translator node if we return publisher we will return to the publisher node and the expander if that's the output of this new Chef router function we route to the expander node the next step is now key for the hierarchy we Implement and this is the translator note that this always routes back to the new chef and the expander too so the expander and translator never route to the publisher only the new Chef is allowed to do this so this is where we create our hierarchy now let's actually create this workflow and I think it's always helpful to again visualize it here we can see that we've got the evaluator if it's relevant we pass it to the new Chef if not then we end it and here we can see that the translator always here routes back to the new Chef same for the expander and that only the new Chef is able to route to the publisher and then we can end our gentic workflow okay let's try it out so the first article is the pope will visit Spain today so let's have a look add the result then here we can see some print statements evaluator binary score is no and the final result is the pope will visit Spain so we did not change the state and yeah it was not evaluated not translated or anything here when we've got the same Messi going to switch from barcel to Real Madrid then this should be created as true here we can see binary scores yes and at the end we should have a new article and here is the final article so this is in German of course Leon Messi and the best and so on and so on so this is in German and it's a lot of text now and this is something that the publisher could not publish so we could of course also send the result of the publisher to an API or whatever we want to do as publisher okay great that's it I hope you now know how to implement hierarchical workflows with the lra if you got questions please let me know that in the comments and thanks for watching see you bye-bye
Info
Channel: Coding Crash Courses
Views: 1,442
Rating: undefined out of 5
Keywords: langchain, langgraph, agents, hierarchical agents, llm, openai
Id: 9HhcFiSgLok
Channel Id: undefined
Length: 13min 35sec (815 seconds)
Published: Mon Jun 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.