WEBVTT

00:00.160 --> 00:03.960
So now it's time to cast this function into a long chain tool.

00:04.200 --> 00:09.560
And we're going to later equipped our agent to have the ability of using this tool.

00:09.880 --> 00:15.280
So in the previous video, we defined our tool manually by using the tool class of chain.

00:15.560 --> 00:23.160
But we can also use a tool decorator that will take a function and convert it into a long chain tool.

00:23.400 --> 00:27.360
So for that I'm going to import the tool function.

00:28.360 --> 00:33.240
And I'm simply going to use this decorator above this function.

00:35.680 --> 00:41.960
The tool decorator is a long chain utility function that will take this function and create a chain

00:41.960 --> 00:42.800
tool from it.

00:42.840 --> 00:48.360
It's going to plug in the name of the function, what it receives as arguments, what it returns, its

00:48.360 --> 00:51.840
description, and populate it in the chain tool class.

00:52.960 --> 01:01.880
So if I'm going to debug this code and put a breakpoint right over here and let's now inspect this function,

01:01.880 --> 01:02.760
get text length.

01:02.760 --> 01:12.720
We can see now it's a structured tool and we have the name and we can see and have its description which

01:12.760 --> 01:17.800
includes the function name, its arguments, what it returns and the description that we wrote.

01:17.840 --> 01:23.680
This will be used by the LM reasoning engine to decide whether to use this tool, and we'll soon see

01:23.680 --> 01:24.640
how it works.

01:25.880 --> 01:29.880
Just one minor change we need to make to our getText function.

01:29.880 --> 01:37.400
So sometimes the LM may pass as our text some unnecessary characters which are not alphabetical.

01:37.400 --> 01:41.440
So for example backslash n or some single quote.

01:41.640 --> 01:44.840
So here I'm simply removing those and cleaning it up.

01:45.000 --> 01:50.680
And all the code for each video in this section is available in the videos resources.

01:51.120 --> 01:56.320
Now, I highly suggest that you use this implementation of the function in the video, even though I

01:56.320 --> 01:57.320
forgot to add it.

01:59.520 --> 02:00.040
Cool.

02:00.040 --> 02:07.590
So now I want now to stop it and let's create a new variable and let's go and call it tools.

02:08.030 --> 02:13.790
It's going to be a list of long chain tools, which now is going to be populated only with get text

02:13.830 --> 02:14.430
length.

02:14.710 --> 02:18.950
Now this list of tools we're going to supply to our react agent.

02:20.030 --> 02:25.390
So remember when previously the agent selected the correct tool to use from our prompt.

02:25.630 --> 02:27.830
And it was sort of like magic.

02:28.190 --> 02:34.510
So now we're going to dive deep and to see how it's actually implemented and what's happening under

02:34.510 --> 02:34.990
the hood.

02:36.350 --> 02:36.870
Okay.

02:36.910 --> 02:39.670
Now it's time to go and see this in action.

02:40.030 --> 02:44.470
And all of this tool selection basically is going to happen from a very special prompt.

02:44.510 --> 02:45.670
We're going to be writing.

02:45.870 --> 02:48.270
So I'm going to write here the template variable.

02:48.270 --> 02:54.190
And now I'm going to attach here a very smart prompt that will make our tool selections.

02:54.910 --> 02:57.990
So you can find this prompt in the source code.

02:57.990 --> 02:59.870
And we'll do it later in this video.

03:00.350 --> 03:05.310
However right now I want to go to the Lang Chain Hub, which is a part of Lang Smith, and the Lang

03:05.310 --> 03:08.110
Chain Hub is a marketplace for prompts.

03:08.150 --> 03:13.990
It's all free, and people share their very well crafted prompts, including the prompts for this react

03:13.990 --> 03:14.630
agent.

03:14.790 --> 03:20.830
Now, this prompt was created by Harrison Chase, the creator of Link Chain, but everyone can submit

03:20.830 --> 03:23.350
prompts and share them on the chain hub.

03:23.870 --> 03:29.070
Anyways, I'm going to open my browser and I'm going to search for Lang Chain Hub.

03:29.350 --> 03:32.830
I'm going to go to the second link, which is a part of Lang Smith.

03:33.190 --> 03:35.030
And right over here we have the list of prompts.

03:35.030 --> 03:38.670
And I'm going to filter by agents and I'm going to write here react.

03:39.110 --> 03:43.350
So right here we have a prompt that was crafted by Harrison Chase.

03:43.350 --> 03:44.470
You can see it right here.

03:44.790 --> 03:47.350
And we can see the prompt right now.

03:47.590 --> 03:50.790
So on the right side you can see when it was last committed.

03:50.790 --> 03:52.070
When was the last change.

03:52.310 --> 03:58.390
You can see what kind of type this prompt is and the number of downloads it has, how many followers

03:58.390 --> 03:58.830
it has.

03:58.830 --> 04:05.230
Because prompts is something that is dynamic and people always refine and refactor their prompts.

04:05.510 --> 04:10.830
And right now we're simply going to copy and paste it into our IDE.

04:12.310 --> 04:15.910
So I'm going to click the copy button and going to paste it.

04:16.590 --> 04:18.830
Let's just format it a bit more nicely.

04:18.830 --> 04:24.430
I'm going to take all of those lines over here and going to indent it in the react algorithm.

04:24.430 --> 04:26.870
This prompt is going to be sent to the LM.

04:26.870 --> 04:32.310
And this is going to generate the thought of the LM which is going to help us select the correct tool.

04:32.830 --> 04:34.990
So let's analyze this prompt.

04:35.030 --> 04:40.830
This prompt is in my opinion, the most important prompt in LM application development.

04:41.350 --> 04:45.190
And it's an implementation of the react paper.

04:45.510 --> 04:51.070
And if you want to find more about the theory behind this prompt, I highly suggest you going over the

04:51.070 --> 04:57.070
theory section where I analyze prompt engineering techniques from the theoretical point of view and

04:57.070 --> 04:58.670
analyze their research papers.

04:59.070 --> 05:01.630
And you can find a video about react.

05:01.670 --> 05:03.750
Anyways, let's analyze this prompt.

05:03.910 --> 05:08.550
Answer the following questions as best as you can, and you have access to the following tools where

05:08.550 --> 05:10.750
we have a placeholder for tools.

05:11.020 --> 05:15.300
And here we're going to plug in our list of tools and the tool descriptions.

05:15.900 --> 05:17.300
And it continues.

05:17.300 --> 05:18.700
Use the following format.

05:18.700 --> 05:20.340
Question the input question.

05:20.340 --> 05:21.980
You must answer thought.

05:21.980 --> 05:24.100
You should always think about what to do.

05:24.500 --> 05:27.340
And then we have action and action input.

05:27.340 --> 05:32.020
And the action is going to be the action to take and should be one of the tool names.

05:32.020 --> 05:34.540
And here we're going to plug all of our tool names.

05:34.860 --> 05:38.540
And the action input is going to be the input for that action.

05:39.020 --> 05:42.820
So in our case our action is going to be get text length.

05:42.820 --> 05:46.820
And our input for it is going to be the text that we want to count the length of.

05:47.260 --> 05:52.420
And after that we have an observation which is going to be the result of the action.

05:52.660 --> 05:57.340
So this is going to be the result of the tool we ran with the tools input.

05:57.820 --> 06:03.300
Now we can see here that this thought action action input observation can repeat n times.

06:03.540 --> 06:08.980
And this is referencing the iterations of the agent when it's deciding which tool to use.

06:09.140 --> 06:13.060
Uses that tool then keeps going and doing this over and over again.

06:14.300 --> 06:17.900
So the final answer would be derived from the following format.

06:17.940 --> 06:21.980
Thought I know the final answer and final answer column.

06:22.020 --> 06:24.540
The final answer to the original input question.

06:24.820 --> 06:28.220
So here we have the output indicator begin.

06:28.580 --> 06:33.100
And our question which is an input placeholder.

06:33.340 --> 06:35.660
And then thought agent scratchpad.

06:35.940 --> 06:40.340
So in the thought agent scratchpad we're going to discuss later in this video.

06:40.660 --> 06:45.780
And from a quick analysis of this prompt we can classify this prompt as a chain of thought prompt.

06:45.980 --> 06:51.860
Because we are asking the LM to tell us how it's thinking and how it's coming up with its answer.

06:52.140 --> 06:58.460
It's also a few shot prompt because we're supplying examples and telling them, how do we want our format

06:58.500 --> 06:59.700
output to be with.

07:00.260 --> 07:04.660
And this is an implementation of the react paper Reasoning in Acting.

07:04.660 --> 07:09.420
And again, if you were interested in the theory of all of this, you can check out the theory section

07:09.420 --> 07:10.980
where I dive deep into this.

07:11.900 --> 07:12.380
Cool.

07:12.860 --> 07:17.690
So since we'll be dealing with the Agent Scratchpad only later in this video.

07:17.730 --> 07:19.210
I'm going to remove it.

07:21.810 --> 07:25.290
And this is our prompt now that we're going to use.

07:25.290 --> 07:29.170
And this is how our agent is going to select the correct tool to use.

07:30.570 --> 07:34.650
So from our template we want to create a blank chain prompt template.

07:34.890 --> 07:36.850
So I'm going to write a prompt variable.

07:36.850 --> 07:39.770
And it's going to be an instance of prompt template.

07:39.770 --> 07:42.250
And I'm going to import this link chain class.

07:42.970 --> 07:47.010
And this prompt template is going to be initialized from template function.

07:47.010 --> 07:50.490
And here I'm going to supply it and plug in the template.

07:51.170 --> 07:53.490
Now I'm going to use the partial method.

07:53.690 --> 08:02.210
And this method will populate and plug in the values of the placeholders that we already have, because

08:02.210 --> 08:04.370
we know we have the tools and the tools name.

08:04.370 --> 08:06.730
So we can plug it in into our prompt right now.

08:07.050 --> 08:12.610
And the input variable placeholder will come dynamically when we run this chain.

08:12.610 --> 08:14.610
So it's going to be from the user.

08:14.610 --> 08:19.810
So because we already know what we want to plug in in the tool names and list of tools.

08:20.130 --> 08:23.530
Then let's do it and initialize it with this partial method.

08:24.130 --> 08:27.450
So we want to initialize the tools placeholder.

08:27.730 --> 08:31.610
And here we're going to put the list of tools that we had already created.

08:31.930 --> 08:39.330
And we want also to plug in the value of tool names and tool names is going to be a comma separated

08:39.410 --> 08:40.650
tool names list.

08:40.650 --> 08:44.770
So I'm going to use the join method with the comma separator.

08:45.050 --> 08:48.370
And I'm simply going to iterate over all the tools.

08:48.370 --> 08:49.890
So for t in tools.

08:49.890 --> 08:52.170
And I'm going to access the tool name.

08:52.410 --> 08:59.450
So this eventually will print a nice string of the tool names concatenated together separated by commas.

08:59.770 --> 09:02.010
Now we're almost done with the prompt template.

09:02.010 --> 09:08.530
But in the tools keyword I can just plug in the list of tools that we gave, because this is a list

09:08.530 --> 09:16.250
of Lang tool objects, and we want here to supply a string which will represent the tools description

09:16.250 --> 09:19.530
because Lmns receive only text as input.

09:20.650 --> 09:27.970
So luckily for us, Lang Chain already implemented this logic that converts a tool list of tool objects

09:27.970 --> 09:31.050
into a string that's representing their description.

09:31.050 --> 09:36.930
So instead of passing tools, we'll call the function render text description.

09:37.770 --> 09:39.690
And let's now import it from Lang chain.

09:40.090 --> 09:43.290
And we're going to plug here the list of tools.

09:43.490 --> 09:48.970
So if we'll examine this Lang chain utility function we can see all it's doing is iterating through

09:49.010 --> 09:50.210
the tools we gave it.

09:50.410 --> 09:55.650
It's formatting nicely into a string the tool names and tool descriptions.

09:55.650 --> 09:56.370
That's it.

09:56.610 --> 10:01.610
So we're going to eventually supply a string into our LLM.

10:02.130 --> 10:06.410
So let's run it for a sanity check to see that it also runs smoothly.

10:06.850 --> 10:09.050
And we can see it worked with no errors.

10:09.370 --> 10:14.450
And now we want to define our LLM which is going to be our reasoning engine.

10:14.690 --> 10:18.930
So I'm going to use chat OpenAI the chat version of GPT 3.5.

10:19.170 --> 10:24.600
We're going to be using temperature equals to zero because we don't want any creative answers.

10:25.080 --> 10:28.720
And now we're going to plug in this top argument.

10:28.720 --> 10:32.840
And here the values are going to be backslash n observation.

10:33.080 --> 10:40.600
And so this will tell the LM to stop generating words and to finish working once it's outputted the

10:40.600 --> 10:42.640
backslash n observation token.

10:43.440 --> 10:44.680
And why do we need it.

10:44.680 --> 10:50.240
Because if we want put this top token then the LM would continue to generate text.

10:50.240 --> 10:54.720
And it's going to guess one word after another observation.

10:54.720 --> 10:56.720
And the observation is the result of the tool.

10:56.720 --> 10:59.960
And this is something that will come from running our tool.

11:00.280 --> 11:04.640
And if it comes from the LM then this is simply an hallucination.

11:04.640 --> 11:08.160
And it's not really the real time result from running the tool.

11:08.800 --> 11:14.800
Now, I know that for some of you it may sound kind of abstract, but I promise you, in the series

11:14.800 --> 11:20.360
of videos we'll be seeing this with our own eyes and you'll understand the importance of using this

11:20.360 --> 11:22.640
stop token when we use the LM.
