WEBVTT

00:00.040 --> 00:03.640
So in this video we're going to implement the react py file.

00:03.640 --> 00:08.360
And this is going to hold all the reasoning logic which we're going to use in our graph.

00:08.520 --> 00:14.080
So the TLDR for this is that we're going to use an average function calling that is going to be the

00:14.080 --> 00:17.000
reasoning engine which decides which tool to call.

00:17.920 --> 00:26.440
Let's start with the imports and lets me import from env the load function so I can load the environment

00:26.440 --> 00:31.960
variables and all the API keys I want to import from link chain core.

00:32.360 --> 00:35.800
I want to import the tool decorator.

00:35.960 --> 00:40.680
So this will help us to create a tool from our functions that we define.

00:41.160 --> 00:45.240
I want to also import from link chain OpenAI the chat OpenAI.

00:45.480 --> 00:48.000
So we can make LLM calls to GPT.

00:48.600 --> 00:57.680
We want to import from link chain Tavileh the search object which is a pre-made search tool.

00:57.680 --> 01:02.000
So we can use and plug it in to our llms to our agent.

01:02.680 --> 01:06.900
And now let's go and load our environment variables.

01:07.300 --> 01:09.500
And this is time to begin the coding.

01:09.740 --> 01:12.220
And we want now to implement the tools.

01:12.500 --> 01:20.220
So for this example we'll be implementing the triple tool which is a function which takes in as an input

01:20.260 --> 01:22.100
an integer a float.

01:22.220 --> 01:25.580
And it's going to return us this float triple.

01:25.860 --> 01:30.220
Now notice here all that I'm doing is writing the prefix here of the function.

01:30.220 --> 01:32.620
And I'm going to be auto completed by cursor.

01:32.780 --> 01:36.620
This is simply, by the way, mind blowing how coding has changed over the years.

01:36.620 --> 01:42.500
And with LMS how easy it is, um, and how convenient it is to to implement those kind of things.

01:42.820 --> 01:45.220
So we can see here an example implementation.

01:45.220 --> 01:47.660
Let me remove this unnecessary code here.

01:47.940 --> 01:54.380
And we have here the triple function we have here the description of this function which is going to

01:54.380 --> 01:56.700
be propagated eventually to the LM.

01:56.700 --> 02:00.500
And the LM is going to decide whether to use this function or not.

02:01.220 --> 02:03.780
Anyways so this is the triple function.

02:03.820 --> 02:11.030
Now if we want to turn it into a link chain tool, All we need to do is to use the decorator of tool

02:11.070 --> 02:16.110
here that we imported, and we get a link chain tool that we can plug in.

02:16.870 --> 02:17.670
Alrighty.

02:17.710 --> 02:26.070
So now we want to equip our agent with not just the triple tool, but we also want to give it the search

02:26.070 --> 02:28.310
tool as a tool that it can use.

02:28.710 --> 02:32.110
So in order to do that let's define a new variable.

02:32.110 --> 02:33.510
It's called tools.

02:33.510 --> 02:40.870
And tools is going to be a list which is going to be an object of the search prebuilt tool and the triple

02:40.910 --> 02:42.670
tool that we just implemented.

02:43.630 --> 02:49.750
So the first element of this list is this object of this class that we search initialized with the max

02:49.750 --> 02:51.190
results equals to one.

02:51.470 --> 02:56.870
So in this example we're simply limiting the search to fetches only one search result.

02:57.190 --> 03:03.750
And by the way this search tool has already the description prebuilt in case you were wondering.

03:03.910 --> 03:10.590
So the package maintainers implemented the search description.

03:10.590 --> 03:13.890
And this is going to be propagated eventually to the LM.

03:16.650 --> 03:20.090
And the second element of that list is the triple tool.

03:20.610 --> 03:21.210
All right.

03:21.210 --> 03:27.690
So now we want to tell the LM somehow that it has access to this kind of tools.

03:29.290 --> 03:29.650
All right.

03:29.650 --> 03:34.010
So now I want to discuss the reasoning capabilities of Dlrm.

03:34.210 --> 03:38.090
So how does the LM decide which tool to call.

03:38.530 --> 03:44.930
So by now it should be familiar with the algorithm and the react prompt.

03:45.170 --> 03:53.770
And this is a very fancy prompt that is a derivative from the react paper which is helping to leverage

03:53.770 --> 03:56.250
the reasoning capabilities of the LM.

03:56.530 --> 03:58.530
So this is how everything started.

03:58.530 --> 04:02.290
And in the early days of agents this is how it was used.

04:02.290 --> 04:07.730
People used this special prompt in order to leverage Llms to decide which tool to call.

04:08.330 --> 04:14.380
And by now things have evolved and we have better ways to do things which, by the way, all derive

04:14.380 --> 04:16.300
from this very special prompt.

04:16.340 --> 04:23.020
What I am referring to right now is function calling, and function calling is a feature in most modern

04:23.020 --> 04:30.740
llms today that allow us to give the LLM when we initialize it, the definition and the instructions

04:30.740 --> 04:33.180
and the details of the tools that we want to give.

04:33.220 --> 04:40.300
The LLM and the LLM is going to return us in the response, whether we need to call those functions

04:40.300 --> 04:43.020
with which arguments if it needs to.

04:43.060 --> 04:49.220
And I elaborate on function calling in this course, in this video and in this video, and we are actually

04:49.220 --> 04:53.700
not exposed to the implementation of function calling in LLM vendors.

04:53.700 --> 04:57.260
So each LLM vendor has its own way of doing things.

04:57.300 --> 05:05.700
The implementation is probably a special system prompt, which is similar to the react prompt and helps

05:05.700 --> 05:11.620
the LLM to decide whether it needs to choose a tool or not, and returns the answer in the correct format.

05:11.900 --> 05:20.240
However, now the LLM vendor is responsible to parse the the LLM response and to arrange everything

05:20.240 --> 05:27.240
nicely in the response we're getting and putting the function call in the function call key in the response

05:27.240 --> 05:28.200
from the LLM.

05:28.240 --> 05:31.040
And again, don't worry if you don't understand this in full.

05:31.360 --> 05:36.920
All you should know and all you should take right over here that this is the modern way to choose tools.

05:36.920 --> 05:42.720
And if we're going to use function calling, we're basically offloading the task of choosing the correct

05:42.720 --> 05:44.840
tool to the LLM vendor.

05:44.840 --> 05:51.720
And we don't need to handle this parsing like we did from before, because the LLM does it for us.

05:51.720 --> 05:56.560
So for us, it's a very big win because it's less code that we need to write.

05:56.560 --> 06:02.640
And actually the results and the performance of this is only improving over time because all the LLM

06:02.640 --> 06:07.920
vendors, they have dedicated engineers that are defining this and are making sure that we get quality

06:07.920 --> 06:11.760
results and the correct tools to be called and the correct arguments.

06:12.120 --> 06:12.720
All right.

06:12.720 --> 06:18.800
So right now we want to implement the reasoning capabilities of our agent.

06:19.200 --> 06:23.740
And unlike we did before, we're not going to use the react prompt.

06:23.780 --> 06:27.060
We have something which is better and this is function calling.

06:27.180 --> 06:31.020
So we're going to initialize an LM that supports function calling.

06:31.060 --> 06:34.500
And we're going to give that LM the tools that we defined.

06:34.540 --> 06:41.700
Now the search tool and the triple tool both are link chain tools which have the descriptions of which

06:41.740 --> 06:43.060
arguments do they receive.

06:43.660 --> 06:47.260
So I'm going to initialize a chat open AI.

06:47.620 --> 06:54.340
And I'm going to use the bind tools method and supply the tools that we already wrote.

06:54.740 --> 07:02.900
And link chain is going to take the tool descriptions and it's going to send that to the LM.

07:02.980 --> 07:05.580
Every request that we're going to make.

07:05.940 --> 07:12.660
So the LM can now return us this function call or tool calling field with the correct function to be

07:12.660 --> 07:13.140
called.

07:13.580 --> 07:19.180
And we don't need to handle any parsing because the LM vendor handles the parsing for us.

07:19.180 --> 07:22.900
And if there is a function call we get it in a special key in the response.

07:23.420 --> 07:28.430
And for more information about the implementations of function calling, please check out those videos

07:28.430 --> 07:29.070
over here.

07:29.910 --> 07:30.350
Cool.

07:30.350 --> 07:32.150
So this is it for this video.

07:32.270 --> 07:36.910
And I want to run this, um, script right over here.

07:37.110 --> 07:41.670
Let's see that everything runs even though we're not running anything right now, just to see we don't

07:41.670 --> 07:44.990
get an error and we can see it's working.

07:44.990 --> 07:48.270
And let me go and commit the code where you can find it.

07:50.510 --> 07:52.750
So let me write git add.

07:54.230 --> 07:57.990
And we have here this file that was added right now.

07:58.550 --> 08:00.150
So let's go and commit it.

08:01.310 --> 08:05.790
And I'll call the commit function calling reasoning.

08:06.950 --> 08:08.270
And let me push it.

08:09.790 --> 08:11.390
And let me show you the repo.

08:11.910 --> 08:17.870
And if you'll go to the commits you can see you have this commit right over here where you can see the

08:17.870 --> 08:18.430
code.

08:18.630 --> 08:22.390
And in the next video we're going to implement our lane graph nodes.

08:22.390 --> 08:27.270
And those are the executables which are going to run during our execution.
