WEBVTT

00:00.990 --> 00:02.640
-: Hi, I'm gonna walk you through

00:02.640 --> 00:06.450
how to do the ReAct pattern, or the reason and act.

00:06.450 --> 00:09.180
This is the very first agentic framework

00:09.180 --> 00:11.520
and so, it's really important to know.

00:11.520 --> 00:14.520
Simon Wilson has a really great writeup of this

00:14.520 --> 00:15.570
and I think that the code

00:15.570 --> 00:17.970
that he uses here just explains it really well.

00:17.970 --> 00:20.070
So, I'm just gonna walk through this code

00:20.070 --> 00:21.750
rather than type it out together.

00:21.750 --> 00:23.910
And it's just gonna help you understand

00:23.910 --> 00:25.680
how every agent is created

00:25.680 --> 00:27.570
because this is the basic pattern

00:27.570 --> 00:30.330
that all of them were implemented with, right?

00:30.330 --> 00:34.890
So, I've updated the code for the latest modules.

00:34.890 --> 00:37.767
So, we're just gonna install openai,

00:37.767 --> 00:41.370
and we need to get an OpenAI API key.

00:41.370 --> 00:44.190
So, just gonna go to the OpenAI account,

00:44.190 --> 00:46.470
and then you can create a new key,

00:46.470 --> 00:49.620
testing udemy,

00:49.620 --> 00:51.000
call it that.

00:51.000 --> 00:51.873
Create a key.

00:54.240 --> 00:55.410
I'm just gonna copy that.

00:55.410 --> 00:57.600
Now, you don't wanna share this key with anyone

00:57.600 --> 01:00.300
because it's really important, it's a password,

01:00.300 --> 01:02.490
and you could lose all of your money

01:02.490 --> 01:04.170
if somebody else gets access to it

01:04.170 --> 01:07.380
'cause they could make calls to your GPT account.

01:07.380 --> 01:09.330
So, I'm just gonna submit that in there

01:10.260 --> 01:12.750
and that's where the OpenAI key goes in,

01:12.750 --> 01:15.090
we're using get getpass here to do this.

01:15.090 --> 01:19.080
And then, we're gonna instantiate the OpenAI client

01:19.080 --> 01:20.850
and we're gonna create a ChatBot.

01:20.850 --> 01:24.240
So we have the system message, it's gonna go in here,

01:24.240 --> 01:25.920
and then the user message,

01:25.920 --> 01:29.340
and then we're just gonna run the ChatBot

01:29.340 --> 01:30.990
and then get the responses.

01:30.990 --> 01:33.720
We're using gpt-4o for this one.

01:33.720 --> 01:36.570
And now, that's not really that important.

01:36.570 --> 01:38.460
The most important thing is the prompt

01:38.460 --> 01:41.130
and it's the prompt where all of the magic happens.

01:41.130 --> 01:42.300
This is how it works.

01:42.300 --> 01:45.360
It's asking the agent to run in a loop

01:45.360 --> 01:48.150
of Thought, Action, PAUSE, Observation.

01:48.150 --> 01:50.970
So. it's gonna have a think about what to do.

01:50.970 --> 01:52.620
It's gonna decide on an action

01:52.620 --> 01:54.630
and I'll show you what that means in a second.

01:54.630 --> 01:55.463
Then, it's gonna pause

01:55.463 --> 01:57.960
and wait for you to send it something back,

01:57.960 --> 02:01.350
and that thing that it gets back is an observation.

02:01.350 --> 02:02.370
And then at the end of the loop,

02:02.370 --> 02:04.200
it's gonna output an answer.

02:04.200 --> 02:06.450
So, it's saying to use Thought to describe your thoughts

02:06.450 --> 02:07.470
about the question you've been asked.

02:07.470 --> 02:09.840
There's a chain of Thought being implemented there.

02:09.840 --> 02:10.740
And then, use Action

02:10.740 --> 02:12.750
to run one of the actions available to you,

02:12.750 --> 02:14.490
so this is function calling

02:14.490 --> 02:17.430
or tool calling depending on how you wanna talk about it.

02:17.430 --> 02:19.050
And then, Observation is just a result

02:19.050 --> 02:20.820
of running those actions.

02:20.820 --> 02:22.530
So, this is actually before tool calling

02:22.530 --> 02:24.780
and we got us working through a lot of cajoling,

02:24.780 --> 02:27.300
but (laughs) we've given it a couple of actions here.

02:27.300 --> 02:31.380
Calculate and then also search Wikipedia,

02:31.380 --> 02:33.960
and we just said always look up things on Wikipedia

02:33.960 --> 02:35.160
if you have the opportunity to do.

02:35.160 --> 02:36.810
So, the question we're gonna ask here

02:36.810 --> 02:38.520
is what is the capital of France?

02:38.520 --> 02:39.930
And then, the thinking

02:39.930 --> 02:42.090
is that I should look up France in Wikipedia,

02:42.090 --> 02:43.170
and then it's gonna take an action.

02:43.170 --> 02:44.220
and then pause,

02:44.220 --> 02:46.020
and then you'll be again called with this.

02:46.020 --> 02:48.390
Observation, France is a country, the capital is Paris.

02:48.390 --> 02:50.250
Then, you'll output the answer is Paris.

02:50.250 --> 02:52.320
So, this is a few short example,

02:52.320 --> 02:56.940
so showing it how to use these tools that it has in place

02:56.940 --> 02:59.790
and how to run through the loop that we talked about.

02:59.790 --> 03:02.010
Cool, so then, we're just gonna use Regex

03:02.010 --> 03:03.780
to pull out the Action at the end

03:03.780 --> 03:05.340
and that gives us our answer.

03:05.340 --> 03:06.173
Now we have that,

03:06.173 --> 03:08.700
we have a function that instantiates the ChatBot

03:08.700 --> 03:10.170
and then does a number of turns.

03:10.170 --> 03:12.240
So, here is five turns,

03:12.240 --> 03:13.560
but it's just gonna keep running

03:13.560 --> 03:15.930
until the ChatBot decides it's done and gives an answer.

03:15.930 --> 03:19.140
And if there's an Action detected,

03:19.140 --> 03:21.480
so the Action here,

03:21.480 --> 03:22.770
if there's an Action in the prompt,

03:22.770 --> 03:24.870
then it's gonna do that Action.

03:24.870 --> 03:28.650
And if not, then it's just gonna return.

03:28.650 --> 03:31.050
So when we do an Action,

03:31.050 --> 03:33.120
we actually have a function that we do,

03:33.120 --> 03:35.700
so we have written a function to call Wikipedia.

03:35.700 --> 03:38.340
We just take the query that ChatGPT gives us

03:38.340 --> 03:40.260
and then send it to Wikipedia.

03:40.260 --> 03:43.350
And then, we have a function to eval.

03:43.350 --> 03:47.010
So, this is like a Python function to evaluate a formula

03:47.010 --> 03:49.560
so it can run calculations.

03:49.560 --> 03:52.050
Okay, so I'm just gonna run all of this.

03:52.050 --> 03:54.690
Oh yeah, and we need to set the API key.

03:54.690 --> 03:57.030
So if you run into that error,

03:57.030 --> 03:59.253
then you just need to set that here.

04:04.740 --> 04:06.510
Okay, and takes...

04:06.510 --> 04:07.343
Oh, yeah.

04:16.180 --> 04:17.580
Okay, so that's working,

04:17.580 --> 04:20.220
and now we can see it in action, hopefully.

04:20.220 --> 04:22.020
So, what is the capital of England?

04:22.020 --> 04:24.600
It thinks, I should look up England on Wikipedia.

04:24.600 --> 04:26.820
It does the action, it pauses,

04:26.820 --> 04:28.860
and then we send it back this, right?

04:28.860 --> 04:31.440
So, we sent back England is a country

04:31.440 --> 04:32.520
that's part of the United Kingdom,

04:32.520 --> 04:33.960
is located on the island of Great Britain,

04:33.960 --> 04:35.640
which covers 60%.

04:35.640 --> 04:39.270
So, this is from the Wikipedia call that it made

04:39.270 --> 04:41.430
and then it can give the answer, right?

04:41.430 --> 04:43.750
So, we could also test what is two plus two

04:44.910 --> 04:47.070
and it should run the evaluation.

04:47.070 --> 04:49.170
Yeah, here it's saying, calculate two plus two.

04:49.170 --> 04:50.820
So, we're gonna pass in the two plus two

04:50.820 --> 04:52.170
and we're gonna evaluate that.

04:52.170 --> 04:54.540
So now, it has access to the calculator.

04:54.540 --> 04:56.820
And when you string these two things together,

04:56.820 --> 04:57.840
we could give it more tools.

04:57.840 --> 05:00.300
Like you could give it a tool with access to your database

05:00.300 --> 05:02.610
to save data or to run queries.

05:02.610 --> 05:05.700
You could give it a tool access to an API

05:05.700 --> 05:07.110
that you have access to,

05:07.110 --> 05:08.520
really whatever it is you need.

05:08.520 --> 05:09.607
Like the weather API, right?

05:09.607 --> 05:11.460
"What is the weather tomorrow?" you could ask it

05:11.460 --> 05:12.293
and it could do that.

05:12.293 --> 05:13.590
There's a lot of potential

05:13.590 --> 05:18.450
in just giving access to the AI to specific tools

05:18.450 --> 05:20.820
and then telling it what it needs to do

05:20.820 --> 05:21.930
in order to get there.

05:21.930 --> 05:24.990
So, you can give it a longer running query.

05:24.990 --> 05:28.440
You could maybe ask it, let's just say,

05:28.440 --> 05:31.270
query what is the population

05:32.160 --> 05:35.040
of London times...

05:35.040 --> 05:37.680
Oh, we've already done London, let's do Paris.

05:37.680 --> 05:42.650
What is the population of Paris times the population,

05:44.830 --> 05:48.510
or what is the population of Paris times two

05:48.510 --> 05:52.210
plus the population

05:55.920 --> 05:57.240
of London?

05:57.240 --> 05:58.950
You know, whatever it's you want to do.

05:58.950 --> 06:00.960
But this should do a couple of hops now.

06:00.960 --> 06:02.730
So, it's looking up the Paris and London,

06:02.730 --> 06:04.530
population of Paris first.

06:04.530 --> 06:06.150
It gets the population there,

06:06.150 --> 06:08.490
then it looks up the population of London,

06:08.490 --> 06:11.820
and then it comes through and runs into an error. (laughs)

06:11.820 --> 06:13.347
So, it's that you can only concatenate tuple

06:13.347 --> 06:14.820
and not int, right?

06:14.820 --> 06:17.520
What it's done here is it's run into some sort of error

06:17.520 --> 06:19.470
when doing this calculation,

06:19.470 --> 06:22.230
and this is pretty common with AI.

06:22.230 --> 06:24.120
We could run it again and maybe it won't get stuck,

06:24.120 --> 06:26.490
but when you're using an agent,

06:26.490 --> 06:29.520
they do tend to make errors, they tend to get stuck.

06:29.520 --> 06:30.720
And here, you can see, this time,

06:30.720 --> 06:32.400
you know, didn't put the commas in

06:32.400 --> 06:35.070
and therefore, it performed accurately, right?

06:35.070 --> 06:36.877
So, that's something you could put in your prompt and say,

06:36.877 --> 06:38.820
"Don't put commas when you're calculating."

06:38.820 --> 06:42.240
But whatever it is that you do in terms of observability

06:42.240 --> 06:44.130
and identifying when it gets stuck

06:44.130 --> 06:46.500
and training it how to do better things,

06:46.500 --> 06:48.330
that is an exercise left to the reader.

06:48.330 --> 06:50.850
But you can see here that population of Paris times two

06:50.850 --> 06:53.880
plus the population of London is this,

06:53.880 --> 06:55.260
and we can check whether that's correct.

06:55.260 --> 06:57.180
Yeah, hopefully that gives you a good intuition

06:57.180 --> 06:58.950
for how agents work,

06:58.950 --> 07:02.133
and you'll be using this pattern in the future.
