Multi AI-Agents Reasoning LLM - CODE Examples (Python)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello Community here we look at the code of a single agent say hello a turn yes so before we enter the multi-agent interactions we have to be able to code the single simplest agent so we say import the opinion yeah and then we have a patent class the easiest thing you can imagine we have a python class this python glass what we have we have here again our API key we have to pay openai to use their tools their models then we have here a list if you want where we have a conversation history a simple string where we can add a pen to your different text messages and then we have here the function add to conversation it's very easy as I told you takes a string and simply appends here the textual message message to this list in a string form and then we have here to generate a prompt where you say for example the following is a conversation with an AI playing the role of whatever you roll you want to Define and then of course you say here the prompt is now added to the message and then we perform a very specific task and this here is our task now what you do is you add it again to another conversation you generate your prompt you know this is the function just above here and then you have here the open AI completion API endpoint please note that this here is just the simple openai completion API endpoint and not the advanced openai chat completion API endpoint that we will use for more complicated function calling later on so here we are at the most simplest as examples so we go a text DaVinci over three the model that like we have our prompt we Define maximum token whatever you want to pay and then of course after this we want to add again the response here of gpt4 the text that has been stripped for non-specific characters here to our conversation and we return of course the response here to text you see this is the simplest conversation agent a python class you can think of it has if you want some local memory because it records the complete conversation on a specific topic with let's say gpts three or whatever now here text DaVinci or threes are a model that we're gonna use and we have here the open AI complete create API endpoints that we're gonna use for creating an answer from gpt3 of course you can do this here in a simple prompt if you just type to gpt3 but we use here this specific Python program to create a conversation agent beautiful simple example yes yes yes if you want to experience how to code here function calling with agents I have shown this in my last video and this here is hpbs link now a real agent could be much more complex with additional Logic for interpreting the response of GPT managing multiple tasks interacting with other agents so let's now enter a little bit more complicated scenario where we have two agents that interact and we make a very easy two agent scenario we have a user agent that represents for example potential or simulation of a user and we have a helper desk agent and you're not going to believe it but it represents a help desk assistant so what is the task the user asks question and the help desk answer the questions by the user unbelievable you're gonna say I know I know so how we do it again is example class python glass Java user agent with the API key remember every agent you have to pay for average and for every API and we have here the function ask a question and we have here the question by the user and then we have here the class of the help desk agent and you see it's a little bit more complicated and we get a response back so beautiful so let's have a look here at what methods we have defined so at first we have here our local memory everything that we have is a question and comes back from the system we record here the conversation history so we have a complete information after local timeline then again you notice generate the prompt and add it to our conversation history so you tell the system hey the following is a conversation within a help desk AI and then and now comes if you want the first logic part we say hey if something happens then do this so if then if you want so this method here would contain a logic to escalate the issue such as sending an email to a human operator if a particular instance happens for example it could be that the gpt4 system comes back and say hey I don't know so if you get a word wording I don't know you say hey careful issue there's an issue contact here a human operator send an email send the Bing whatever and then of course we come here to the most important task to answer as a help desk the question by the user so again you add everything to the conversation you generate your prompt again we have here our open UI complete API endpoint that creates now the answer here we have Max token is 100 we have again our text DaVinci over three we have our response we have the choices please see my last one we have the text that comes back and this is the response of the AI and we add this of course to our conversational memory if you want and if for example the response is as I told you this particular wording hey sgpt4 I'm not sure I don't know or whatever your trigger words might be you don't have here the escalate issue command where for example an email is sent to a human operator or whatever you have in mind beautiful so we have our user agent Iowa help desk agent now defined so now we more or less create and we initiate them user agent with your open AI API keys or your credit card says hello I'm here baby and then again with a helpdesk h and you have again your credit card operational right and then you simply have to use agent ask a question and I'll put here the question directly into here the string how do I reset my password on my computer or whatever the helpless is and you have the question and then you activate the help desk agent with the question answering functionality even my question as a user well you see and you print your answer by the help desk right so GPT 4 let's say this is our llm on so now how do I reset my password I suppose gpd4 knows the answer now if the user comes now with a question what is the meaning of life that is not for an IT Help Desk situated and you know yes we are now here that we say we escalate the issues we get now here this particular question in the help this agent tries to answer this but escalates the issue because it is not sure being an IT Help Desk what's suddenly this philosophical something means for him so yeah we have now this triangle answer in that escalation step yeah answer question and here we have our escalation step if for example the response is I'm not sure whatever is your trigger word so you see this is now an example of a more complex interaction interaction between a user agent and help desk agent now you say okay this two agent let's do three agent let's do a discussion between three agents I said great so we have and now if you notice we will only have two classes but three agent and I want to show you this particular example because it how you can reuse your agent definition so we have a negotiation agent class in Python represents an agent that negotiates a deal and in the middle between two parties we have here a mediator agent class that is an agent that helps facilitate the negotiation between the two parties beautiful we will see we have the method to negotiate and we have here the method mediate between those parties main script yes yes yes now let's jump into the code so at first we have here the class of the negotiation agent you now know hey open API key your credit card waves and says hello I'm here and then you record your conversation history beautiful you add this to your conversation so you have a local memory of the complete conversation you generate your prompt you add it to the message you notice and now comes the part negotiate the method negotiate now for our agent that does the negotiation you add it to a conversation you generate the prompt you have now the opening a completion create and AP API endpoint this is your engine this is your prompt your maximum token is whatever you define whatever you're ready to pay and you get back a response from chat GPT let's say twice as text you add this to your local conversation memory and you return the response by our AI system or llm system checks GPD for example beautiful this is the negotiation agent and then we have another class or a mediator agent I believe more or less the same so yes yes yes you can read it yourself I don't have to go through this again but here we have now here the method mediate remember in the other one we had here the definition negotiate the function negotiate and now we have mediate but it's the same it connects to Dexter ng03 gives it the prompt and reverse we wait for the answer to text DaVinci comes back with great you can insert any llm that you like so that we have the definition now it's time to create initiate our three agents and now you see we have negotiation aged at one negotiation agent two and they are both the same we use here the coherent definition of a negotiation agent we will just provide a different statement or a different task or a different opinion or whatever to differentiate between them and you're gonna not gonna believe it a party one and party two is here our differentiator and then we have our mediator agent yeah and if you have not provided here our credit card will provide a credit card for the agent so you see you have now if you want three instances that connect to GPT form and you pay for each instance so I have here now a very easy interaction pattern and I always put here the answer here into the parenthesis so we have the negotiation start agent one says Hey I want to buy 100 units of whatever at 10 each and the this was the the buyer and the seller says hey we can only sell 100 units at 15 each and now the mediator comes in we have here our media the agent with the method of mediate we have here the input of agent one and of agent two and you're not going to believe it they the response by gbd4 or gpd3 or chat Tubidy or whatever system you have if they say 10 and the other party has 15 now they gonna use you're not gonna believe it uh as a mediator a proposal 1250 so they meet in the middle and so beautiful this is now the let's say proposal by the mediator agent so the first one asks the other agent hey can we meet in the middle at 12 50 and the other agent is reasonable and says let's agree that we meet at 12 50. so you see unbelievable performance and if there would be some problem you just call here the media mediator agent again so let's call it gpt4 again and let's see what GPT 4 comes up with a possible solution then we present then to our agent 1 and agent 2 in their negotiation if they accept it if they're rejected but always remember the intelligent behind agent one behind agent 2 and behind our mediator agent is always gpd4 from open AI you always connect to the same llm just here with three different instances but it is always in the background GPD form just to make sure where the intelligent lies here great now you might have seen that sometimes agent can create four specific purpose from specific period of time new agents new sub agents for a very particular task that is absolutely focused and you need a specialist so it can refer to increase the complexity with 2H and now can create temporarily another age to perform a very specific task so you see now we have an agent hierarchy integrated now in our next coding example to make it easy I just operate here with one main agent and one main sub agent otherwise it would get a little bit complicated you know our class now in the sleep you know that you have to provide your credit card and you say yummy and then you have your prompt and you say hey lgbt4 please perform the following tasks you define the task you have your completion API you define your llm and you get back a response by gpt4 let's say great so now we have here this was the task agent for a task oh no no don't do that no okay I shifted this oh can't believe I've done it and then you have to control agent class now this is how I hear our our Supreme agent to this is here the boss and he can create and manage other agents so we have now here with this python class control agent yes we have our credit card ready we create here an agent and this agent now is a task agent so our control agent here creates now a task agent and the task agent has been defined over here beautiful and this control agent has now another personal functionality this is manage a task and in this function they can now set here to create an agent and this new agent that is created can perform a predefined task and this sub agent gets back a result to our control agent beautiful this is the definition of our two agents and now we create here the master agent yes yes yes so control agent and you have your credit card ready you see EP and then you say uh the control and agent manages now a new task and for example it is itself not able to perform the task but it has defined here another agent so you say control agent manage the task now here a language translation English to French for example so very easy you see here Ctrl H and manage tasks so control h and manage the task we create here a very specific agent let's say the task is not to translate languages and you say Okay perform the task and so gpt4 the intelligence in the background is now performing the task to translate an English text to a French text beautiful this is it this is all there is to it now of course this is kind of a simplification that I show you the flow that I show you how it works is this the absolute complete code with error correction and everything no this is a simplification therefore I leave you two links in the description of this video one is the the link to the official open AI cookbook and this cookbook I can show you here you go there open the eye open eye cookbook examples how to call functions with chat models this link here you can download it you have beautiful real-time examples in the full complexity but sometimes if you read this the first time you might say oops what's happening therefore I showed you a little bit simplified version with no error correction with no other objects in place but this is here the notebook download this notebook I'll leave you the link in the description and then if you're really that you say hey hey now now I'm no there's also an expert notebook that I will leave you the link in the description and this is also in the examples and this done here is function with a knowledge base for example if article summarization or you have here a conversation with a PDF for example a scientific paper in the archive uh this is a preprint normally it's a not appear reviewed paper but a pre-print paper scientific paper and you can have now a conversation with this and here with our multi-agent it is possible and here you have the complete code here in this notebook you can download for free this is here the GitHub I will leave you the link to this in the description of this video beautiful yes you might ask why do we create agents why is this necessary and hey we have not used any link link chain at all and everything yeah this is the beauty of this system so but come back additional Agents from a control agent what is the benefit now we are thinking about how we can configure our agent how many agents we need for a specific task so what is the benefit of have here control agent create other agents at first you have a task specialization each agent can be specialized to perform a scientific task you have seen that I use the agent definition for our negotiation one in negotiation two agent okay if you have the identical task you can do this but maybe your task virtualization is very dedicated you have to have different subtasks or different access to different external apis so you can build here an agent for a very special task and this agent will only be able to perform one task and one task only but highly specialized the next thing is here parallel processing if the task can be performed in parallel you have multiple API iOS to your system multiple API endpoints you can contact they perform in parallel creating separate age and for each task can speed up the overall process performance significantly you don't have to wait of course if you have logic dependencies then you have to do it but if you have resources where you aggregate the information from do this in parallel this is where agent and sub agent come into place each agent can work task independently and here control agent can collect result resource management if each task requires significant amount of CPU GPU or whatever create separate agents here so you do not reach the maximum bandwidth or whatever compute limitation you have and the last point is really important if one task or subtask fails if you have multiple agents or sub agent it won't affect the whole chain of Agents so each agent can handle some errors in its own way and the control agent can decide what to do if one task fails you can Define it in the definition you can do every try you can lock the error you can escalate the issues you send an email to a human operator whatever you have there are advantages to use here a modular approach to agent and sub agent and assign specific tasks to their intelligence but whatever you design the task force remember an agent just more or less connects to gbd4 a specific llm and external API and external database via API but the main intelligence if you want is still ppd4 or jet GPT or whatever llm system you use so do not make the mistake to think that you have multiple intelligent different intelligence here on the line no you just have you're just a probing in parallel gpd4 or jet GPT an example scenario yeah what are real world scenarios I was asked show us some real world scenarios where a multiple agent makes sense you're building a news aggregation service you have a control agent manage the complete process and it can create separate agents for each news source I don't know you have social media you have print you have video you have whatever different format different conversation rates different whatever you can imagine so each agent is responsible for fetching the news from its particular formatted Source parsing the news articles and storing them in a database multimodal so if one use or one use Gathering fails it doesn't affect the other news sourcing accumulation tasks or agent so you just lose maybe one branch of it maybe you lose the video Branch but you have to textual the social media in the end or customer service you can specialize here have a billing agent you have a technical support agent that is only optimized for the technical support of your specific I don't know system computer system you have a general inquiry agent that can handle all other types of questions and general intelligence content screen information education yeah this is nice you teach different subjects for example you can have agents that are specialized to axis specific let's say mathematical tutor agent he can connect to let's see a dozen different mathematical sources books textbooks a databases exam questions trial and error questions whatever you have history tutor agents specialized on their sources when they have they build up their local memory they know what you know and they know to how to build up on your knowledge science student you you gathered research project management everything risk management agent clear okay furthermore by having its age and specialized in a specific task you reduce of course the risks each agent can be finely tuned to its specific task you give access to different apis you can create your own set of roles what to do if something happens if the format does not fit you do this action or this action you can provide guidelines strategy you can have some complex decision path overall it leads to a better performance a better user experience instead of multiple GPT Force yeah this was a question of one of my viewers I said hey can I can I have multiple gpt4s that interact with each other and I told them hey just wait a little bit I will show you so you do not have multiple gpd4 machines but you have multiple gbd4 instances with your specific agents but remember you have to pay it for each instance so yeah there's no way to to get it for cheap this piece of information yeah remember if you have multiple agents that interact complex are dependent on each other you have a specific interaction pattern this can get complex real fast real soon and you have maybe some errors you have you are not aware of in the first time so make sure that they run effectively and that you are not overwhelmed by the complexity of the interdependencies of all the agents you create yeah it will show you a simplified real time or real life example of multi-agents llm again if you want to find it a complete detailed coding here I just recommend API openai.com version one chat completion and points have a look at this and create a model response for a given chat conversation there's also here the platform openai.com docs the API reference documentation for a chat to create this here you have the explicit code you can string copy and have all the different modalities sought about I will just show you here an abstract simplification that you see how we can build here a more complex real life example just to just for fun so let's say here we have open API key on a credit card already and we will Define here the class of a general agent now this channel agent has some options he she it can create a agent can hold H and can activate plugins use plugins this agent can generate a response from gpd4 over here we use a jet GPT June 13 Edition that is able to operate with function calls we have specific messages from the system tell him hey gpt4 you are now I don't know mathematical tutor whatever and then I say here I was a user I have here my prompt and I say help me with integrals about polynomials and then we can Define here a function this new open EI API functionality and you can Define whatever you like this is just a very simplified thing that you say I have an input schema with a string with a prompt here and I have an output schema with response of the system and then you take here the assistant so the gpd4 message and you return the message if it's not none or else or incompatible or whatever and then we have the ability as I showed you to create an agent when we take here for example here the ability the general agent definition that we say we give it a specific role now you're a history teacher can it create agents yes or no it's a Boolean operator can it hold the agent yes or no can it use plugins yes or no this is maybe important for your credit card bill that you're going to receive later on and you take care about exception that's not yes yes yes and you hold agents you get it what I'm trying to show you and then just to show you the complexity let's say we have here a discussion between two people and we have one person is more of an emotional augmentation pattern that he follows in the other person she is more of a factual person arguing with data and factual things and then since this is an official discussion maybe we're broadcasted in TV you have a moderator who moderates the whole discussion here so we have here our general agent we give this agent a specific role so the GPT 4 knows exactly what it has to perform and the task we took on tell him hey you are now a moderator you can create sub agents and you can stop sub agents and let's go there where are we the plugins that are available that you have access to for example is emotional language generation sentimental analysis fact-based language generation logical reasoning or whatever you have or you can even link this to a sub sector of gpd4 that you see and you are now a specialist in time management or you are now has a PHD in introducing complex topics in sociology or in human psychology or whatever you you get the idea so we have here our first discussion participant our second participant we have the moderator and then as I told you each agent that is simulating now this discussion can create different other agents let's say you want here if you talk about your emotional topics here that you want to have here a rhetoric agent that is specialized in the perfect rhetoric four specific audience group and a specific age group you know who are you are let's say voters that you want to address here in particular or maybe there's a minority that you want to address here in a discussion so you know what rhetoric you have to choose in your emotional augmentation so we create here an emotional agent and yes yes it has to be peso a safe and rhetorical generation and beautiful and the other person says hey I need an online fact Checker a real-time fact tracker so you create an agent that is connected to I don't know fact checking apis whatever you have then you can have other agents that are created by sub-agent you counter argument and time keeper agent and you see now we have defined quite a lot of Agents and then you have to define a turn on turn a discussion schema how those agents interact you can have that participate one can make a statement and then participant two answers and then it's a turn for participant two to bring up a new topic and participant one answer so whatever interaction pattern you define in this computer simulation of a real world discussion between two people you define it or you say hey I have a one-on-one turn for three times and then I have the moderator summing it up or changing the topic or however you define it let me show you this maybe in a visualization I think it's a little bit more self-explanatory so here we are now in a simple symbolic picture of our system so we have now our multi-agent gpt4 system for social insights in let's say a presidential election coming up 2024 in a hypothetical country so we have a candidate one we have an agent and I counted a two and an agent and we have a moderator three simple agents so what can we do now let's think about this candidate a let's say has a particular characteristic let's say his augmentation style is defined by emotional triggers in his statements so maybe we have here then another agent let's call them an emotional trigger agent that tells us hey in a discussion what is my next best emotional trigger you can connect to open AI API get access to gpt4 and see how you should formulate an emotional trigger let's say candidate 2 here is more based on factual information would like to argue here about factual data about what you can fact check so you have here also an API to open AI gpt4 let's say the same example and you have now an agent that is focused on factual topics that are important to uh a public in a specific Nation now given the course of the discussion you might find out that you will have to fact check very fast some upcoming topics so you need here are the factual topic agent need to create a sub agent that can connect to an external API let's say this external API is the homepage of New York Times a version composed of whatever publication you prefer so we fact check and we come back now with the actual fact check data with the source of the information and you say hey look you have a new piece of information to the factual topic agent that can then feed it back in an actual statement by candidate too now the same of course is true for the other candidate and let's say here this candidate here creates an external API agent that has a look at social media in real time maybe Facebook maybe Tick Tock maybe whatever medium you have and it's looking especially for the strongest emotions that are trending on a particular social media so you see you can simulate two very different agents if they have different personalities and different styles of discussion but of course you can have additional agents for example here you can have here countermeasure agents what happened if candidate 2 is somehow attacked by candidate one verbally with a statement that is not fact checkable but is strongly emotional how would a person now react what is the best way to react to an emotional attack if you are and with your personality more on The Logical factual side you can design a countermeasure agent that counters this emotional confrontation and ask opinion here let's say gpd4 how to best counter argument an emotional statement and of course exactly the same on this other side you have here an emergency agent if you somehow notice oops we are running into some troubles you have an agent that has access to a document maybe in this document there is a list of specific points that you as a candidate one want to stress in your presidential campaign and you say first topic second topics or topic so this emergence agent comes up and say hey I have the most important point now that you have to stress so you see the way you you build your agent the internal Logic the internal Circuit of augmentation you can really design this yeah of course the moderator agent you can have for example that it snaps into place after two times everybody every candidate one and two had two statements and then the moderator comes in where you have a moderator that is listening to the conversation and acts only on specific word triggers for example if it hears I won the 2020 elections you know then maybe the moderator has a path of action to go forward let's see here in a hypothetical world where this moderator is simulating Fox Entertainment a hypothetical Corporation in America that just paid some amount of money they say oops we we want to make here a statement whatever it is whatever the moderator can now interact with so you see the system here is perfect for simulating different runs so let's say just an idea after 100 computer simulations where you have exactly this phenomena of a presidential discussion of two agents with two candidates represented by agent that have varies distinct characteristics in their personality and they simulate now as an agent this real person characteristics after 100 simulation you know what is the optimum chain of discussion if you get an emotional argument how to counter this if you got a factual argument how to address now certain immersion in your audience so this here is a real real importance to the real world in the upcoming near future for a lot of Nations for a lot of countries here on this planet and you see why here's social insights is so important it's the power of open Ai apis and remember the intelligence behind all of this all of these boxes except now where you have an external API where you go to a new account watching poster whatever you have it is gpt4 or chat GPT whatever on your computer simulation there is more or less just one Company open AI with a system intelligently enough to provide real social insights but we will focus here on the compute or on the code implementation and to have here professional code implementation maybe in the next video we will talk here about some clever ideas from very clever people here from June 5th 2023 here and I'll have preprint about multi-agent collaboration how to harness here the power of intelligent llm agents and they design a new multi-agent system with enhanced capabilities so there are some very interesting research going on currently but to discuss this paper and this new coding methodology would be a little bit too much for this video I hope it was informative I hope you enjoyed it and it would be great to see you in my next video
Info
Channel: code_your_own_AI
Views: 10,244
Rating: undefined out of 5
Keywords:
Id: L7MRmCHmakE
Channel Id: undefined
Length: 41min 5sec (2465 seconds)
Published: Sun Jun 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.