-: Hi, I'm gonna walk you through
how to do the ReAct pattern, or the reason and act.
This is the very first agentic framework
and so, it's really important to know.
Simon Wilson has a really great writeup of this
and I think that the code
that he uses here just explains it really well.
So, I'm just gonna walk through this code
rather than type it out together.
And it's just gonna help you understand
how every agent is created
because this is the basic pattern
that all of them were implemented with, right?
So, I've updated the code for the latest modules.
So, we're just gonna install openai,
and we need to get an OpenAI API key.
So, just gonna go to the OpenAI account,
and then you can create a new key,
testing udemy,
call it that.
Create a key.
I'm just gonna copy that.
Now, you don't wanna share this key with anyone
because it's really important, it's a password,
and you could lose all of your money
if somebody else gets access to it
'cause they could make calls to your GPT account.
So, I'm just gonna submit that in there
and that's where the OpenAI key goes in,
we're using get getpass here to do this.
And then, we're gonna instantiate the OpenAI client
and we're gonna create a ChatBot.
So we have the system message, it's gonna go in here,
and then the user message,
and then we're just gonna run the ChatBot
and then get the responses.
We're using gpt-4o for this one.
And now, that's not really that important.
The most important thing is the prompt
and it's the prompt where all of the magic happens.
This is how it works.
It's asking the agent to run in a loop
of Thought, Action, PAUSE, Observation.
So. it's gonna have a think about what to do.
It's gonna decide on an action
and I'll show you what that means in a second.
Then, it's gonna pause
and wait for you to send it something back,
and that thing that it gets back is an observation.
And then at the end of the loop,
it's gonna output an answer.
So, it's saying to use Thought to describe your thoughts
about the question you've been asked.
There's a chain of Thought being implemented there.
And then, use Action
to run one of the actions available to you,
so this is function calling
or tool calling depending on how you wanna talk about it.
And then, Observation is just a result
of running those actions.
So, this is actually before tool calling
and we got us working through a lot of cajoling,
but (laughs) we've given it a couple of actions here.
Calculate and then also search Wikipedia,
and we just said always look up things on Wikipedia
if you have the opportunity to do.
So, the question we're gonna ask here
is what is the capital of France?
And then, the thinking
is that I should look up France in Wikipedia,
and then it's gonna take an action.
and then pause,
and then you'll be again called with this.
Observation, France is a country, the capital is Paris.
Then, you'll output the answer is Paris.
So, this is a few short example,
so showing it how to use these tools that it has in place
and how to run through the loop that we talked about.
Cool, so then, we're just gonna use Regex
to pull out the Action at the end
and that gives us our answer.
Now we have that,
we have a function that instantiates the ChatBot
and then does a number of turns.
So, here is five turns,
but it's just gonna keep running
until the ChatBot decides it's done and gives an answer.
And if there's an Action detected,
so the Action here,
if there's an Action in the prompt,
then it's gonna do that Action.
And if not, then it's just gonna return.
So when we do an Action,
we actually have a function that we do,
so we have written a function to call Wikipedia.
We just take the query that ChatGPT gives us
and then send it to Wikipedia.
And then, we have a function to eval.
So, this is like a Python function to evaluate a formula
so it can run calculations.
Okay, so I'm just gonna run all of this.
Oh yeah, and we need to set the API key.
So if you run into that error,
then you just need to set that here.
Okay, and takes...
Oh, yeah.
Okay, so that's working,
and now we can see it in action, hopefully.
So, what is the capital of England?
It thinks, I should look up England on Wikipedia.
It does the action, it pauses,
and then we send it back this, right?
So, we sent back England is a country
that's part of the United Kingdom,
is located on the island of Great Britain,
which covers 60%.
So, this is from the Wikipedia call that it made
and then it can give the answer, right?
So, we could also test what is two plus two
and it should run the evaluation.
Yeah, here it's saying, calculate two plus two.
So, we're gonna pass in the two plus two
and we're gonna evaluate that.
So now, it has access to the calculator.
And when you string these two things together,
we could give it more tools.
Like you could give it a tool with access to your database
to save data or to run queries.
You could give it a tool access to an API
that you have access to,
really whatever it is you need.
Like the weather API, right?
"What is the weather tomorrow?" you could ask it
and it could do that.
There's a lot of potential
in just giving access to the AI to specific tools
and then telling it what it needs to do
in order to get there.
So, you can give it a longer running query.
You could maybe ask it, let's just say,
query what is the population
of London times...
Oh, we've already done London, let's do Paris.
What is the population of Paris times the population,
or what is the population of Paris times two
plus the population
of London?
You know, whatever it's you want to do.
But this should do a couple of hops now.
So, it's looking up the Paris and London,
population of Paris first.
It gets the population there,
then it looks up the population of London,
and then it comes through and runs into an error. (laughs)
So, it's that you can only concatenate tuple
and not int, right?
What it's done here is it's run into some sort of error
when doing this calculation,
and this is pretty common with AI.
We could run it again and maybe it won't get stuck,
but when you're using an agent,
they do tend to make errors, they tend to get stuck.
And here, you can see, this time,
you know, didn't put the commas in
and therefore, it performed accurately, right?
So, that's something you could put in your prompt and say,
"Don't put commas when you're calculating."
But whatever it is that you do in terms of observability
and identifying when it gets stuck
and training it how to do better things,
that is an exercise left to the reader.
But you can see here that population of Paris times two
plus the population of London is this,
and we can check whether that's correct.
Yeah, hopefully that gives you a good intuition
for how agents work,
and you'll be using this pattern in the future.