Getting Started with ReAct AI agents work using langchain

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back so as you probably know in 2024 one of the biggest Trends in AI is the emergence of agent platforms if you don't know what an agent is it's essentially AI that can make autonomous decisions so rather than you telling the AI specifically what to do you give it a goal and then it uses the right tools and its own thinking processes and reasoning to be able to solve the problem and one of the best known patterns in AI today is what's called the react pattern which is based on Chain of Thought and it's this idea of reasoning and actions so essentially you get the agent to think about what it needs to do to solve the problem and then it will use any particular tools it knows about to then make executions now why this pattern is so powerful is that it gets to use things like tools so you can Define tools such as maybe a calculator or a clock or uh python for example and therefore rather than just reasoning it can lean into those tools to solve those particular problems and of course that solves problems today like uh AI struggles to count so therefore it can use the calculator it doesn't have an upto-date knowledge Bas so it can use tools such as the browser to go and be able to get the most recent information using things like Google or of course using any other tool to solve a particular problem so today I'm going to break down the real act pattern and I'm going to do that using Lang chain so we're not just going to execute it but we're going to look at how it really works step by step underneath the hood and before we get under the hood what I'm going to show you is a very simple agent that I built earlier which will get you the current time using the system time tool and then throughout this video I'm going to show you how that actually works step by step so if I just run a script the actual system prompt that it has is what is the current time so if you see here it's going to have a look look at this and says entering a new agent execution time it sees the thought process I need to check the system time to answer this question and then here's the action remember what I said about uh reasoning and action so it's reason that needs to check the system time and then the action is going to call this tool called check system time here's the format and then it says I now know the current time and the final answer is 2024 uh 13th of April 16 1020 and again if we actually just look at the python script underneath that for one second you can see here I am using gp4 as the model of course I'm using Lang chain as my framework here the query is what is the current time we'll talk about what this prompt template is in a second and then I'm using this tool check system time again I'll will go into those details a little bit later and then I'm going to construct this react agent I'm going to create my agent executor and then I'm going to execute the agent uh passing in that query what is the current time and then it will go off and uh execute that call and again if I run that one more time you will see here um this is where it does the reasoning so I've asked at the question what is the current time and then there's the reason I need to check the time there's the action remember that tool check system time that I said before earlier gets the format of uh what it needs to pass in for the uh you know the time zone Etc or the datetime format passes that incorrectly gets the result back from the tool you know now it's 16 11: and it says the current time is 10 4 so let's break down how this actually works so to do that we're going to use Lang chain so L chain is probably one of the best tools for being able to work with agents so if you haven't got Lang chain already installed using python you can just do pip install a lang chain and then that will get you installed so as you see I've already got that installed so there's not a problem there the other thing that I will want to install is I am going to use uh open AI I'm going to use GPT 4 for my agents just now in another video I'll cover how to work with other uh llms but for just now we're going to use gp4 so I need to do a pip install a lang chain uh- openai and that will give me the open AI apis within my Lang chain framework so once I've have done that what I am going to need to do is I'm going to need to pass in my open AI API key so to do that I'm just going to do at touch. M I'm going to do a m file you can of course Set uh your variable in memory and use it as an environment variable uh but I'm just going to uh create an N file here and then all you need to do is go to platform. open.com and then you can click on API keys and then of course you can create a new key from there once you've generated that key you just need to paste it into the M file so if I open this up in Visual Studio code we open up the M file what you would do is set open AI uh API key equal to and then just paste in that value I'm obviously not going to show you what my API key is but that is where you would paste this in all righty so what we're going to do now is I'm just going to get you a little bit comfortable with using Lang chain we're not going to use agents just yet we're just going to add some basic queries so I am going to uh create ourselves a new file I'm just going to do a touch uh hello and we'll call it Lang chain uh dop uh so now that we've got that well let's just uh open up in Visual Studio code and then we'll start writing some Lang chain so actually the first thing that I'm going to need to do is I'm going to need to access my environment variable so I'm just going to do a from. MV and then I'll do an import uh load. MV here uh you will need to do a pip install MV if you want to uh use MV in the same way as I have now the other thing I need to import is I'm going to need to uh import some Lang chain uh modules so I'm going to import Lang chain core. messages uh and specifically I'm going to import human message and system message again we won't use that later on but I just want to do this for this very simple Lang chain example and then the other thing I'm going to need to uh import is going to be chat uh open AI so once I've done that I just need to uh import my environment variables I do that by doing load .m and then I need to pick the llm that I'm going to use I'm going to say llm is equal to chat open Ai and I'm going to set the model is going to be equal to GPT 4 now if I want to within platform. open AI I can use any of the models uh within my uh account so if I click on here playground for a second you will see here model I'm using GPT 4 but if I wanted to I could use the new GPT 4 Turbo I could use G pt35 turbo 16k basically I just need to put whatever the model name is as my llm and my python code and it's got to basically be the same name as in this list the next thing I need to do is I need to set my query so we we will build up to the uh what is the current time for just now I'm just going to type in uh who is ky Perry you can probably guess from my Kitty Purry in the background for anyone who's got a Keen Eye then I'm obviously a big fan of KT Perry uh so now what I need to do is pass in both my system prompt and my user prompt to gp4 so to do that I'm going to create a new array called messages and in that I'm going to pass a system message and I'm going to set my system prompt to being something like uh your a helpful uh assistant now of course I can set that to be anything uh but I think that will just do for just now and then I will set the user prompt as human message and I'll set the content is going to be equal to the query so in this case it is going to be who is KT Perry and then all I need to do is execute my llmm so I'm going to do it result equals llm do invoke and I'm going to pass in my messages array and then finally I'm just going to print out the result so if I now save that and we come back into my terminal and if I run python uh hello Lang chain you're going to see It'll take a second I've not enabled anything like streaming but it's going to go make that call to GPT 4 it's going to ask the question who is Katy Perry it's going to come back with a big answer and there it is so you can see Katherine Elizabeth husen known professional as Katy Perry is an American singer blah blah blah blah so there you go that's your basic introduction to kind of Lang chain so one of the things you might have heard of with uh Frameworks such as Lang chain is this concept of prompt templates where you can set a template for the uh prompt that you want to send and then just inject the variables at change which again this is really useful for if you're asking a specific who is questions and you just want to put a different person's name in all the time maybe you're looking up a database or whatever then that's a good technique to be able to do that so we're going to convert the Lang chain that we just wrote which was kind of more at chat basis and we're going to move that into uh The Prompt template type format so to do that I'm just going to remove this from Lang chain core. messages and instead I'm going to import um a couple of things first one going to be the uh from Lang chain uhor core. prompts and then that is going to give me access to this prompt template which I am going to import once I've done that I can get rid of that messages piece and then I can write prompt template uh is going to be equal to uh prompt template do from uh template and then I'm going to write in who is and then I can pass in whatever I want so in this case I was using Katy Perry but this is going to be my uh you know my template in general so always I'm going to have who is whatever the person's name is and input is going to be essentially the substitution variable so in this case I can now change the who is Katy Perry in my query and I can just make that as KT Perry now the next thing that I want to be able to do is execute this so there's a couple of other changes that I will need to do I'm going to introduce this concept of chain that's going to be uh really important later so the first thing we will do is we're are going to have an output parser and at the moment we're just going to deal with things like strings so we will just set this to be uh stir output parser as you can see I've got some uh nice squigglies there so I need to do another import so I'm just going to say from Lang chain uhor core do output parsers and then I'm going to import and specifically I'm going to pick store output parser now as you may have not notice there are other types such as Json output parsers again so but for Simplicity sake we are just going to use string for just now but again as you explore Lang chain for yourself uh you may want to use different uh parser types and we're going to use the Lang chains built-in uh chaining mechanism so I am just going to pass in prompt template I'm going to pipe that with uh the llm and I'm going to pipe that with output parser and then essentially now when I want to do my result rather than do an llm invoke I can invoke my chain and now I can pass in the input and that is going to be set to the query and now I can get rid of the messages and then I can just uh put in uh some brackets here and I will pass in input uh which again is the substitution variable here so I'm going to set input and I'm going to make that equal to query so I am essentially passing in Katy Perry and input so if you think about what's going on I'm doing a chain invoke the chain has been made up of the prompt template the llm which is GPT 4 and the output parser uh which is the string output parser and then of course this chain here is just going to combine those items it knows what to do with them so when I call the invoke I'm going to pass in the data that I want to be combined with the prompt template executed llm and then output it using the string output parser it will execute it via the invoke and then it's going to come back with the result so now if I just run my hello Lang chain again you're going to see I'm going to get the exact same result it's going to say who is KT Perry uh gives a different answer because of course it always does but the nice thing that I can do here is if I want to change this and rather than say uh who is k Katy Perry I can just substitute this with who is Taylor Swift run that more one more time and there you go it's came back with Taylor swi is an American singer songwriter now this is all very great but the problem that we have with this technique it is only interacting with the knowledge that the model has I'm not interacting with anything external to the model so if for example the bio of Katy Perry or Taylor Swift has changed over time it's not going to have that latest information about those folks to do that we would need to either have the model updated with latest knowledge or I would want to be able to use tools to go and ask a larger ecosystem and that is really what we're going to be doing here with agents we are going to combine the knowledge that the the model has from its own knowledge base and then we're going to use tools to augment it with knowledge from the outside world and to demonstrate that we can actually come back to Lang chain for a second and we can get rid of the who is Katy Perry we'll keep with the input and now we will actually ask it what is the current time so this is coming back to the example that I showed you at the beginning so now if I run that you can see GPT 4 is coming back saying as an AI I don't have the ability to provide real time information including the current time so it doesn't know and the reason it doesn't know is in order to access the time you need to access a tool of some sort that has the answer it's not going to have the time from its knowledge base because time is a moving thing and this is where Agents come in so what we are now going to do is we are going to move to using this react agent reasoning and act agent to allow the llm to start making autonomous decisions and be able to have access to tools so what we will now do is we're going to do a quick install so I'm going to do a pip install at Lang chain uh Hub I'm going to explain to you why we're going to need that in a second but essentially I just want to get access to a prompt template so rather than using uh the who is KT Perry prompt template what I want to be able to do is pull a prompt template from the internet and in this case The Prompt template that I need which is the react prompt template that is going to come from Lang chain Hub don't worry we are going to break down exactly what that prompt template looks like a little bit later on so let's come back into vs code for a second and we're going to change a few things so the first thing is I'm going to get rid of this prompt template from uh template here uh H simply because I want to replace that with the prompt template that we're going to pull from Lang chain Hub so to do that I'm going to type in Hub dopo and then I'm going to pass in uh HW Chase at 17 so actually that is the organization space of Harrison chains the creator of Lang chain uh and then specifically we want to access the react prompt template and again you're seeing the squigglies here for a second so one of the things I'm going to need to do is put in from Lang chain uh import uh Hub and then that will give me access to this prompt template we'll take a look at that in a second now the next thing I want to able to do is I want to specify the tools that it can use so in this case we're not going to provide any tools just now so I'm just going to set the tools as being an empty array later on we will put in that system time tool that we defined a little bit earlier so once I've done that I can then construct my react agent so I'm going to say agent is equal to uh create react uh a agent and as you can see we've got some squigglies in there so I'm going to have to import uh my create react agent so to do that I am going to put a from Lang chain. agents and then I am going to specifically import uh create react uh agent and I'm also going to import uh agent executor cuz I'm going to need that a little bit uh later on so I'm going to say create react agent and then I'm going to pass in the llm which is going to be gp4 just as I did before I am going to pass in my tools and then I'm going to pass in my prompt template which at the moment is going to be the prompt template that we're going to pull from Lang chain Hub and now what I'm going to do is I'm going to get rid of this output parser in this chain for a second and now I'm just going to declare an agent executor and I'm going to set that being equal to agent executor and again I'm going to pass agent is going to be equal to agent so this is the react agent that I just created a second ago um again similarly I'm going to set the tools is going to be equal to tools and then I'm going to I'm just going to mark it as verbose as equal to true for just now because I want to be able to see the output once I've done that I can just get rid of this result chain uh invoke and then I can say uh agent uncore executor uh. invoke we can get rid of the print because I put uh verbose mode in in input is going to remain the same and I'm going to set the query so if we just really look at what we've done here for a second I'm loading my environment variables as I did before so it's got my open API key I'm setting my llm being to gp4 so it's the exact same as I did earlier um the query is what is the current time that is going to work in combination with the prompt template I will show you what this prompt template looks like and that prompt template is being pulled from Lang chain Hub and it's specifically in the hwj 7 at react repository there that's where it's coming from I've got no tools at the moment and then when I create this react agent so it's a special type of agent I'm telling it to use the GPT 4 uh llm which I just defined I'm saying how use the empty tools and use the prompt template here and then I'm going to uh pass this into this agent executor which is going to execute the agent that I specified and then finally I'm going to do the invoke so I'm saying take that agent executor that we've just defined go invoke at the uh the agent and then pass in this query what is the current time and that will get munched together with this prompt template that we talked about here so if I just run this obviously it is going to fail because we haven't got a tool there just now so if I run my python hello chain. py one more time you can see now is entering that new executor chain exactly what we did at the beginning of this it says I need to check the current time in my current location very good action use a tool device that can dat displayed to current time such as a smartphone so it started to hallucinate cuz it Doesn't I haven't given it a tool it doesn't know what to do so it's just hallucinating then finally says I'm sorry I can't provide the current time because it doesn't know any tools it doesn't know what to execute so it just can't answer the question so let's fix that let's give it a tool that it can actually Implement so let's come back into VSS code for a second I'm going to create a new folder and we're going to call this at tools and then I'm going to create a new file and we're going to call this system uncore time _ tool dop so to create a tool which Returns the time we will just use the system time from the datetime library in Python so to do that I'll just import dat time and then I am going to create a new function called check system time and then I'm am going to expect a variable called format because I want the llm to be able to send me different formats um you know I could want it to just uh get the time or the DAT or whatever but I will H give that a default format so in this case it will return the full at year month uh and time there so once we have done that uh we can then just Define our time so I'm going to get the current time I'm going to put it in a variable called current time and we will just call date time do datetime do now and that will get the current uh system date time now to format it I will just have a thing called format it time time we will set that equal to current time dot uh stir uh F time and then we'll set that to the format that is being passed in so if the llm decides not to pass in a format it will use our default format there but otherwise it's going to use the formatting that we have and then we are just going to return formatted time now the problem that we've got here is of course I have got this wonderful check system time but Lang chain won't know what to do with that so what we need to do is actually uh decorate that up a little bit so I am just going to pass in a uh decorator called tool and to be able to use that I'm going to put in from Lang chain uh do agents and we will do import tool now there's a little bit of magic that goes on with this tool decorator and that magic is it will essentially pass out in a nice formatted form the name of the function which is check system time it will allow it to have access to the uh the argument type so in this case formats but also it will look at the the description of the function so actually I just need to provide a nice little description here so I'll say Returns the current uh date and time in the specified format and this is really important because that is going to be made available to the LL M to be able to figure out what this tool actually does so a really good description here is going to help your llm out so now that we've defined that I can come back into my original Lang chain uh file and we can type in from tools remember it was in my tools folder system time tool and we're going to import check system time uh just to save a little bit of space I'm going to remove this stuff that we don't need anymore from the top there and then what I can do now is I can just pass in check system time into tools and that will now be passed through to the uh react agent that I'm creating and it will also be passed through to the agent executor so now if I come back into my terminal and we run our hello Lang chain one more time then it's going to have access to the tool so as you can see here it says I need to use the check system time function to get the current time actually check system time there is the format so it's aware of the arguments that we've seen passes through um the uh the time so it's obviously executed the tool pass the time through and then it's been able to come back with the answer so essentially what this is done is use this system time tool it's understood this description it's understood these arguments it's understood the name and then it's been able to use this tool because what we have said is we said here are tools again remember agents are autonomous here are tools that you can use to solve this problem and in this case I wanted to solve the problem of what is the current time and it's figured out it can use that check system time tool that we just created to solve that problem now if I wanted to I could do some variations I don't need to just ask it what is the current time I could actually be a little bit more fun in what we ask so I can say what is the current time just must show the time and not the date now what that will do is if we execute this one more time is it will then allow the llm a little bit of options and hopefully we should get a response that isn't got the date there but actually Returns the time so you see the final answer is the current time is 1645 so if we look at this I need to use check system time function to get the current time however the default returns both the data and the time I'll need to adjust the format to only show the time this is really cool it uses the action check system time but because it understands the format right it knows there is an argument which is the format um argument and then rather than passing through allowing the default to run through it's actually passing its own format hm and S so it's actually when it makes that call to our system time tool it's telling it the format to use and then it's coming back with the answer 1645 so you see rather than day and time and then it says I know the final answer and it says the current time is 1645 really cool so this is how these react agents work very very cool so let's give it a another example so rather than returning the time I think what we will do is uh get it to tell us the time in New York so we'll say what is the current time in New York uh now of course it won't no where it is just now um we haven't returned that in our system time so we're just going to tell it you are in London so it knows when it calls the system time piece that it's getting it from London so now if we run this one more time uh hopefully it should be able to do the calculation uh manually and figure out from its own knowledge base that New York is 5 hours uh ahead of London and then come back with the answer so you can see here um I need to chck the current Sy some time in London it's reason and then converted to New York time since London is generally 5 hours ahead I'll need to subtract 5 hours from London time so it's cold check system time yeah it's passed in the time here again it doesn't need the date current time is 1647 to get New York I subtract 5 hours so action so rather than using a tool it's just going to do it itself and then it's going to come back with the answer but as you can see there it's failed it to come back with the answer I cannot Prov the time conversion is terrible you know again maybe if I run it again it will uh do this uh let's see if running it a second time we'll come back with the answer um the nice thing is we could just introduce another tool which is could be a calculator and then it could do that calculation for it so rather than uh you know it having to try and do the calculation itself it could just use a tool and there you go this time second time of running has suddenly got more confident in its abilities to do calculations and it's Talent is the time in New York is 1148 and again that's a great de demonstration of why tools become important because if I gave it a calculator tool rather than it trying to figure it out itself it could use the calculator tool as well as the system time tool so okay let's start breaking this down a little bit more this isn't just about executing agents this is actually about understanding how this works so what we are going to do here is we are going to take a look at what this magic prompt template actually is we understand prompt templates from before we created one with the who is Katy Perry one we will go to smith. Lang L chain uh.com Hub that is Lang chain Hub and then we'll paste in hwj 17 react so we're just we are going to see The Prompt template so if we look at this answer the following question as best as you can you have access to the following tools you see that just what we showed earlier there is the substitution variable which is tools so we're passing in tools remember what we did over here we're passed in uh tools so in this case tools is check system time use the following format question the input question you must answer thought so it's actually telling it how to do reasoning action action input observation so this is the format so we're telling it how to reason and then it says come back with the final answer and then you know it's telling it to kick off passing in the input so in this case what is the current time and then thought it's using uh this thing called an agent scratch Pad so if it wants to sort of write some notes the l l wants to write some notes it can do that now if I wanted to what I could do is run rather than pulling the prompt template from The Hub I could actually just have that hardcoded locally and actually there's good reasons to do that I'm not a big fan of having external dependencies that could change from underneath me so I actually uh you know bringing that prompt template locally into my uh own uh you know project is maybe not a bad thing to do and then I'm in control of my own dependencies so to do that I'm just going to uh copy this and then what we will do is I am going to create a new uh file we will call this uh react template uh dop and we'll do a from uh Lang chain _ core. prompts and then we'll import prompt uh template and we'll create a new function we'll call it get uh react prompt template and then I'm just going to do a return prompt template do from template exactly as we did with the Katy Perry example and then I'm just going to put in uh three quotes here I'm going to paste all of this in and then we will just quote it off there and as you can see it's just going to return that entire prompt template now if I wanted to I can now just remove the L chain import Hub uh so that is now gone and I can now just do a from uh react template import uh uh and we call that get react prompt template and I can get rid of the hub. pool and I can replace that with my get react prompt template and now rather than going to the hub I'm just pulling my react template in this hardcoded format from here and of course if I wanted to I could just rerun that one more time and of course it's going to continue to work because I'm using the react template from my local machine as opposed to using the pull from The Hub so there you go it's coming back with final answer to current time is New York 11:54 and 55 seconds so I'm no longer pulling the prompt template from The Hub I am just using my local hardcoded version of that template and it's coming back with the answer but I think we can go further than this I don't think we necessarily need to trust what's going on with the agent if we want to we could come back to just executing this as default prompt templates and see how this works let me show you what I mean so we're back to hello Lang chain what we are going to do now is we're just going to comment out the uh this lot here uh we'll come back to that in a second and we're going to put back in the same format for executing uh prompt templates as we did before so what I'll now do is just paste in the code that we wrote out earlier which is using the chains and the output parser so I'm setting output parser is equal to stir output passer I'm setting the chain is equal passing in the prompt template which is going to be the uh the same uh template that we had before this react prompt template here I'm going to pass in the llm which is gp4 and then I'm going to go back to my store output parser and then I'm going to invoke the chain now you see the squiggly is on stir output power sir so I'm just going to need to uh repport that back in now of course if we look at the react template there's a few substitution variables that we don't have so you see it's expecting tools it's expecting tool names it's expecting input and it's expecting agent scratch Pad so what we are going to do just now is just let's manually set these in just now I'm going to not put any values in at this point I'm just going to put in uh things like tool names I'm going to set it to blank value so just empty strings for just now um but I Just Want to Have All the Right variables in place so I'm going to put agent scratch pad and we will set that to equal to empty string so now if I run this one more time we come back into our terminal we'll clear this rather than running the agent we are now just running a standard llm query here so let's run python hello Lang chain and see what happens so as you can see there obviously I am not using uh the agent executor at this point but you start to see the remnants of the react template so is doing the reasoning itself I'm just doing a single uh prompt here so you see I need to find out the current time in London then calculate the time in New York based on the time difference so that's actually coming that's just a pure gp4 llm call using that react prompt template action so it's learn this format use a tool or app to check the current time in London it doesn't know a tool action input na observation so again it comes back with final answer to current time New York is insert calculated time so it's not what we want right but you can see that the reasoning is coming from the llm right but we can shape that further because actually what we haven't done at the moment is we haven't given it access to the tools so let's do that so what we now need to do is inject the tools into that prompt template in the same way as the agent was doing so we're going to do that manually so to do that we need something that's going to give us um the definition of those tools remember in that promp template here it's got two variables one is tools itself which is essentially going to have the the name of the function the arguments in the description and then it needs a list of the tool names that it needs to provide so we need to pass through the tool descriptions and the tool names into this prompt template we need to inject it in so I need something that can describe this tool now Lang chain does have a very useful module that does that so if we import from Lang chain tools uh do render if we import specifically a thing called render text description then that's going to allow us to describe those tools so underneath here now what I'm going to do is I am going to uh have something that actually lists out those tools so I'm going to create something called uh tools list uh and we are going to set that equal to render text description and then what we will then do is we will uh pass in list uh tools so we're going to pass in a list of our tools and then obviously I want the tool names as well so I'm going to pass in tool names and we're going to essentially just do uh a list of the names so we are just going to join uh together uh t. name and we'll do a 4T in tools and that will essentially just Loop through all the names in the tools we'll do a join and it'll comma separate that now if I take that here we'll put that before the result now before we go and invoke this what we want to do is just actually look at what comes back from this tools list and these tools names and how it looks within the context of the prompt template so I am just going to print out I'm going to call the format method off of The Prompt template I'm going to pass in the substitution variables so it was expecting input tools tool names and agent scratchpad I'm going to say agent scratchpad is equal to uh blank just now but we're going to pass in those tool names and we're going to pass in that tools list we're going to pass in that query what is the current time and we're just going to see what that actually looks like so I'm just going to run a python hello Lang chain and you can see here again I'm not invoking just now but what's actually happening in the prompt temp it says answer the following questions best as you can you have access to the following tools check system time the name check system time format so it's actually accessed all the arguments of our function and then it says Returns the current date time in the specified format so if we come back into our uh tool our system time tool you see it Returns the current date and time in the specified format so that's what this tool decorator is doing it's giving access to this render text description it's describing what this function actually does so if I changed one of the values in here if I said for mati and then we just run that again you're going to see that it now comes back with in the list returns in the specified form Matti so this is really important this comment over here but we'll just set it back to being format so now now that we understand what's going on here I can get rid of this print statement and then we can invoke this chain and print out the result and what we are going to do now is we're going to pass in the tools names to the tool names variable and I'm going to pass the tools list uh here as well so now I'm going to invoke the change just as I did before and I'm going to pass in the query the tools the tools names the agent scratch Pad but of course I'm not running as an execute I'm just doing a onetime execution so let's clear this run this one more time and there we go you can see it's coming back I need to find the current time however I'm in London system will time reflect the London's time zone blah blah blah blah so it's doing that reasoning and then it's using the action check system time action input is this and then observation 230 now as you see in my clock there it's 10 5 so the big thing that's happened here it's got as far as this and then it starts to hallucinate so it's hallucinated the time cuz it's not executed the tool I've given a list of tools it can use I can given a list of the tool name names but we haven't actually executed the tool right because the agent framework in line chain is actually executing the tools we would actually have to manually execute the tool we'd have to have something that does this but regardless it thinks it's got the time at the moment so it's hallucinated 230 and then it's done the thought and then it's went around a a couple of times and then it's came back and said the time is 9:30 so it's done 5us 14:30 so you start to see how the agent is working now right it's got as far as here then it would have executed this once it's put the right observation in then it can continue on from over here and come back with the final answer so we obviously in order to stop it hallucinating we actually need to stop it at this point so that it can execute the tool let's stop it on that observation right so to do that I'm going to come back to my llm here and you can model is equal to G pt4 and what I want to be able to do is tell the model to stop whenever it sees the text observation so to do that I just need to pass in model quars and then I'm just going to set at stop is going to be equal to our new line observation we save this and what should happen if we come back into uh our terminal when it does the action input here if it sees the text observation this token here it should stop so let's clear this and then run this one more time and you can see it says I need to check the current system time adjust it in New York's time zone New York is typically five hours behind London action check system time action input and because it's stopped at that point what we can now do is execute the tool and then insert that in and that's how these agents are working so it actually puts a stop as soon as it sees observation and then it can go and execute the tool and then place the observation manually into the prompt template and then let it continue on so let's do that so what we will now do is I'm going to take the prompt template which remember is going to get injected I'm going to set it equal to the prompt template and then I'm going to inject some results within here here so if we come back here for a second so if you remember what we are going to do now is I'm going to insert all of this in here so this is simulating what the agent actually does so it's just taking the history and then it's injecting it in and now I could just do uh observation so it will take the remember when uh it's run this it knows what the format of the tool is so you see see action you know input is h m ands and it says action check system time so I could just execute the tool so I could go find my check system time tool pass in the input uh execute it and then what I will do is pass in the uh time so in this case it is uh 17 uh 20 so we'll just say it in 0 seconds um and then finally what I want to do to help it along its way is I want it if you remember it's Fen this format of question thought observation thought so once it's done its observation I want it to do a thought so I am just going to pass in here a new line and then I'm going to pass in the word thought there and then I'm just going to let it continue on where it left off so if we just run that one more time you can see it's going to execute off and it's essentially going to continue on where it left off so it's saying here the current time is 1720 New York is 5 hours behind London so I need to subtract five RS from this time then Final Answer 1220 and then it continues so all you really need to do is contate the beginning to the end so if we think about really what agents are doing they're going through a loop they're calling the first piece if they see observation they're going to stop they're going to take the inputs the action they're going to execute the action they're going to concatenate the history put it back through the llm in a loop and then keep doing that until it gets an answer and then eventually when it gets the final answer is going to Output it to you and that is exactly how react agents work
Info
Channel: Chris Hay
Views: 3,627
Rating: undefined out of 5
Keywords: chris hay, chrishayuk, gpt-4, chatgpt, llm, ai, generative ai, ai agent, langchain, react agent
Id: W7TZwB-KErw
Channel Id: undefined
Length: 43min 33sec (2613 seconds)
Published: Mon Apr 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.