WEBVTT

00:00.110 --> 00:02.040
Instructor: By the end of those couple of videos,

00:02.040 --> 00:03.870
we'll be implementing ourself

00:03.870 --> 00:05.970
the create react agent function,

00:05.970 --> 00:08.820
and specifically the agent executor.

00:08.820 --> 00:11.910
So what is the agent executor?

00:11.910 --> 00:16.560
It has a very fancy word and it sounds very, very cool

00:16.560 --> 00:20.850
and very complex, but essentially it's a while.

00:20.850 --> 00:24.780
So this is a fake implementation of the agent executor.

00:24.780 --> 00:26.700
Now this is just the pseudo code

00:26.700 --> 00:30.240
and it's essentially a while loop that keeps running,

00:30.240 --> 00:33.270
takes the user input, it takes this prompt

00:33.270 --> 00:35.880
and augments it with all the information we have

00:35.880 --> 00:38.730
about the tool for the LLMs disposal,

00:38.730 --> 00:41.363
and then sends everything to the LLM.

00:41.363 --> 00:44.040
Now the LLM returns as an answer,

00:44.040 --> 00:47.610
and if the answer contains that we need to run a tool.

00:47.610 --> 00:50.040
Then we figure out, and we'll talk about

00:50.040 --> 00:52.290
how this is done, which tool to run,

00:52.290 --> 00:56.760
we then run this tool, and then we go over and over again.

00:56.760 --> 00:58.170
So when do we stop?

00:58.170 --> 01:01.320
When the LLM decides that it has defined an answer

01:01.320 --> 01:04.950
or by default if we limit the number of iterations

01:04.950 --> 01:07.200
or number of messages we want to send.

01:07.200 --> 01:09.270
This is pretty much it, a fancy while loop

01:09.270 --> 01:11.820
where we are keep making LLM calls,

01:11.820 --> 01:15.120
then interpreting what the LLM has responded

01:15.120 --> 01:17.160
and then choosing the correct tool to use,

01:17.160 --> 01:19.983
running it, then running it over and over again.

01:23.790 --> 01:26.400
Now if we think about it a little bit deeper,

01:26.400 --> 01:28.800
we'll figure out there is a little more stuff

01:28.800 --> 01:31.770
that we need to perform during this entire process.

01:31.770 --> 01:34.620
So we first take the user query to the agent,

01:34.620 --> 01:37.500
and we then make this special LLM call.

01:37.500 --> 01:40.290
Now let's say the LLM is able to determine

01:40.290 --> 01:43.590
that we want to use the Get Text Length tool,

01:43.590 --> 01:45.450
so it gives us an answer back,

01:45.450 --> 01:47.460
but the LLM gives us text,

01:47.460 --> 01:51.330
so we need to parse this text in order to work with it.

01:51.330 --> 01:54.330
After we do that, we choose the correct tool

01:54.330 --> 01:57.840
the LLM chose for us, and we execute this tool

01:57.840 --> 01:59.580
and we get a response.

01:59.580 --> 02:02.790
Then we take this response alongside with our query

02:02.790 --> 02:04.710
and we run another iteration.

02:04.710 --> 02:06.810
So now the agent can decide whether

02:06.810 --> 02:09.660
to use the tool or another tool,

02:09.660 --> 02:13.080
or whether it has enough information to return the answer.

02:13.080 --> 02:14.400
So that's pretty much it,

02:14.400 --> 02:16.290
what's happening over and over again.

02:16.290 --> 02:18.780
And in the next couple of videos we'll be implementing

02:18.780 --> 02:20.640
all of this ourselves.

02:20.640 --> 02:22.380
I'm going to head up to desktop

02:22.380 --> 02:24.150
and I'm going to create a new directory

02:24.150 --> 02:26.670
called React Langchain.

02:26.670 --> 02:28.770
I'm going to cd into this directory

02:28.770 --> 02:31.170
and I'm going to create a new virtual environment

02:31.170 --> 02:33.210
by writing pipenv shell.

02:33.210 --> 02:34.620
The environment is created,

02:34.620 --> 02:38.266
and I'm going to install with pipenv langchain,

02:38.266 --> 02:41.490
OpenAI since we'll be using GPT-3.5,

02:41.490 --> 02:43.800
I'm going to install black formatting tool

02:43.800 --> 02:47.910
and Python.n package to help us load environment variables.

02:47.910 --> 02:48.743
Amazing.

02:48.743 --> 02:51.600
So now that we have all the packages installed,

02:51.600 --> 02:53.220
let's go and head up to PyCharm

02:53.220 --> 02:55.170
and you can use VS Code if you want.

02:55.170 --> 02:56.550
And I'm simply going to open the

02:56.550 --> 02:58.173
directory that we just created.

02:59.370 --> 03:01.470
So notice that PyCharm automatically

03:01.470 --> 03:03.480
detected our pipenv environment.

03:03.480 --> 03:04.803
So we're all good.

03:07.950 --> 03:11.610
And now let's create a new file and we'll call it .env

03:11.610 --> 03:14.610
and it's going to hold our environment variables,

03:14.610 --> 03:18.453
which we're going to store it with the OpenAI API key.

03:20.640 --> 03:21.473
Cool.

03:21.473 --> 03:23.550
So now that we've done that, let's go

03:23.550 --> 03:27.030
and create a new file and we'll call it main.py

03:27.030 --> 03:29.910
and here we'll be writing all of our code.

03:29.910 --> 03:33.450
And we want to start by loading our environment variables,

03:33.450 --> 03:36.390
which right now is the open AI API key

03:36.390 --> 03:38.820
that are stored in our .env file

03:38.820 --> 03:40.770
into our Python environment.

03:40.770 --> 03:43.830
So for that we're going to be used the Python dotenv package

03:43.830 --> 03:45.360
that we installed before.

03:45.360 --> 03:48.090
And we're going to do it with two simple commands.

03:48.090 --> 03:50.730
We're going to first import from dotenv

03:50.730 --> 03:54.060
the function load dotenv and we're going to call it

03:54.060 --> 03:56.670
and it's going to take all the values from the dotenv

03:56.670 --> 03:59.190
and load it into our environment variables.

03:59.190 --> 04:01.410
So now let's run some boilerplate code.

04:01.410 --> 04:04.620
I'm going to write if name equals main.

04:04.620 --> 04:07.770
And for our beginning print I'm going

04:07.770 --> 04:12.770
to write, print("Hello ReAct LangChain!")

04:14.670 --> 04:16.920
I'll go and click the green play button,

04:16.920 --> 04:19.170
which is currently configured on the current file,

04:19.170 --> 04:21.180
which is our main.py file.

04:21.180 --> 04:23.490
And this will run our script.

04:23.490 --> 04:25.560
So let's run it for a sanity check

04:25.560 --> 04:27.750
to see if that everything is working,

04:27.750 --> 04:31.590
and we can see the print, and we can now start coding.

04:31.590 --> 04:34.710
We'll first begin by writing our first tool,

04:34.710 --> 04:37.230
and for that we're going to be defining a function,

04:37.230 --> 04:40.050
which is called Get Text Length.

04:40.050 --> 04:42.600
And this function is going to receive text,

04:42.600 --> 04:45.480
which is a string, and it's going to return an integer.

04:45.480 --> 04:47.790
Now we'll start by defining

04:47.790 --> 04:49.920
what this function is going to do.

04:49.920 --> 04:52.680
So the description here is very important

04:52.680 --> 04:56.190
because this is going to help the LLM decide

04:56.190 --> 04:58.650
if it's going to use this tool or not,

04:58.650 --> 05:00.180
in its reasoning engine.

05:00.180 --> 05:02.880
And we'll soon see how this actually works.

05:02.880 --> 05:05.610
So I'm going to write the description of this function.

05:05.610 --> 05:09.390
I'm going to write that it returns the length of a text

05:09.390 --> 05:12.990
by characters, and the implementation is very simple,

05:12.990 --> 05:15.450
it's simply returning the len function

05:15.450 --> 05:18.300
when we inputed the text.

05:18.300 --> 05:20.610
So it will count the number of characters.

05:20.610 --> 05:22.890
For sanity checks, let's simply run it,

05:22.890 --> 05:27.633
and let's put it on a simple input of text equals to dog.

05:29.520 --> 05:33.120
And we can see that dog indeed has three characters in it.

05:33.120 --> 05:35.043
So we can see the function is working.
