WEBVTT

00:00.200 --> 00:06.040
Remember in the previous videos when I wanted to call the agent executor, I wanted to call it the chain

00:06.040 --> 00:08.160
and I will say it would be clearer later.

00:08.160 --> 00:11.160
So now is the time we're going to shed some light on this.

00:11.520 --> 00:18.880
So theoretically we can simply take the result and we'll extract the output key in that result.

00:18.880 --> 00:20.400
Dictionary reminder.

00:20.400 --> 00:21.840
It also has an input key.

00:22.080 --> 00:24.040
And we can take that string.

00:24.200 --> 00:30.360
And we can use the output parser parse method in order to turn it into a Pydantic object.

00:30.360 --> 00:33.120
And this is pretty much what we did in debugging.

00:33.320 --> 00:40.560
However, that is not really pythonic and we actually can use the LCL, the link chain expression language

00:40.560 --> 00:44.080
to get the same result, but more elegantly.

00:44.400 --> 00:47.480
And for that we'll be using the runnable lambda.

00:47.840 --> 00:56.240
Now, the runnable lambda in the Chain expression language is a lightweight wrapper that turns any Python

00:56.240 --> 01:04.110
function, usually a lambda function or simply callable function, into a runnable, an object that

01:04.110 --> 01:05.630
implements the link chains.

01:05.630 --> 01:06.910
Runnable interface.

01:07.030 --> 01:12.870
So this interface standard arises how components like chains, like models, are invoked in batched

01:12.870 --> 01:14.950
and streamed in link chain.

01:15.190 --> 01:22.350
So the runnable lambda is going to allow us to easily integrate custom logic or simple transformations.

01:22.590 --> 01:25.870
And basically it's going to give us the invoke interface.

01:25.870 --> 01:28.910
So it's going to turn everything to be invocable.

01:29.550 --> 01:32.950
And let me bring everything down to earth with the first example.

01:33.270 --> 01:41.710
So I want to implement the extract output variable, which is going to be a runnable because the runnable

01:41.710 --> 01:44.790
lambda class implements the runnable interface.

01:45.230 --> 01:49.070
And here we have the transformation here it's a very simple transformation.

01:49.070 --> 01:54.510
So it's going to be this lambda function over here where it's going to take the input and it's going

01:54.510 --> 01:57.910
to extract from that input which is referred to as x.

01:57.910 --> 02:02.590
It's going to take x at the output key okay.

02:02.660 --> 02:07.940
so if the input is going to be a dictionary, it's going to look up for the output key.

02:08.060 --> 02:13.220
And then this is going to be what the invoke method is going to return us here.

02:13.540 --> 02:19.860
Now this is going to be piped right after we execute the agent executor with our input.

02:19.980 --> 02:23.260
So this is going to take the agent executors output.

02:23.260 --> 02:26.740
And it's going to extract the output key of it.

02:26.780 --> 02:31.980
Yeah I know the the wording here um was double wording but this is what it's going to do.

02:32.300 --> 02:34.860
And now we want to parse it with the output parser.

02:35.020 --> 02:39.700
So I'll create another runnable lambda which is called parse output.

02:39.980 --> 02:46.700
And the implementation of it is going to be a function, a lambda function that is going to use the

02:46.740 --> 02:49.820
output parser.parse function on the input it gets.

02:50.140 --> 02:58.140
And the input this runnable is going to get is going to be the output of the extract output runnable

02:58.220 --> 02:58.500
okay.

02:58.540 --> 03:05.080
So if we're going to pipe everything together and compose everything together with the agent executor.

03:05.080 --> 03:06.400
With the pipe operator.

03:06.800 --> 03:12.360
Then first we are going to give the input is going to be passed to the agent executor.

03:12.560 --> 03:16.080
And when we invoke it, it's going to run the react agent.

03:16.080 --> 03:21.960
It's going to use the given tools and it's going to output a the response of the agent.

03:22.120 --> 03:24.320
So the output is going to be a dictionary.

03:24.480 --> 03:31.840
Now this dictionary is going to be passed to the extract output runnable which is going to extract the

03:31.840 --> 03:32.800
output field.

03:33.120 --> 03:40.200
And then the extracted string is going to be passed to the parse output which is going to parse it into

03:40.200 --> 03:42.040
the structured Pydantic model.

03:42.080 --> 03:49.400
So at the end when we're going to do chain dot invoke the, it's going to be running the compose chain

03:49.600 --> 03:55.120
on the input query, and it's going to return us a parsed structured response.

03:55.160 --> 03:57.800
Now I know it's a lot to digest.

03:57.800 --> 04:03.880
And trust me, I swear for me to learn how to use an expression language took me forever.

04:04.000 --> 04:09.430
So I want you to take a deep breath, and hopefully it will be clearer when we're going to review the

04:09.430 --> 04:12.390
traces in Lamb-smith very, very soon.

04:12.710 --> 04:12.910
Okay.

04:12.950 --> 04:15.950
So let's first run all this and let's see what we get.

04:17.390 --> 04:20.150
Let's go now and fast forward everything.

04:27.190 --> 04:29.030
And we can see we hit the breakpoint.

04:29.030 --> 04:33.870
And here we can see now the result is of type agent response here.

04:34.510 --> 04:36.110
And this is our pedantic object.

04:36.110 --> 04:39.470
And we can see the answer attribute with the answer.

04:39.670 --> 04:47.430
And we also have now the sources attribute which is going to be a list of pedantic source objects.

04:47.630 --> 04:49.590
So this is the structured output.

04:49.590 --> 04:51.950
And from here you know we can serialize it.

04:51.990 --> 04:53.550
We can do a lot of cool things here.

04:54.270 --> 04:54.630
All right.

04:54.630 --> 04:56.230
Let me show you now the trace.

04:57.070 --> 05:01.790
And in the trace you can see now the two runnable lambdas we saw from before.

05:03.030 --> 05:05.700
And let's see an example of the first one.

05:05.700 --> 05:09.740
And this is going to be the extract output runnable lambda.

05:10.300 --> 05:17.420
And here we can see that the input when we invoke this runnable is going to be what the agent executor

05:17.420 --> 05:18.100
outputted us.

05:18.100 --> 05:21.820
So it's going to be a dictionary with the input key and the output key.

05:22.260 --> 05:29.020
And the output of this runnable is going to be the content of the output key here.

05:29.020 --> 05:34.420
So it's going to be the JSON string of our object that we want to parse now.

05:34.620 --> 05:41.060
And this JSON string this is what we're going to be propagating to the output parsing lambda.

05:41.060 --> 05:42.100
So let's go here.

05:42.580 --> 05:48.380
And here we can see now that the input for this runnable is going to be the JSON string.

05:48.820 --> 05:52.300
And the output is going to be the Pydantic class.

05:52.820 --> 06:00.380
So it's going to have the answer and it's going to have the sources attributes and the overall chain.

06:00.620 --> 06:07.770
Let me now show you the overall chain is going to get the inputs and it's going to output us this pedantic

06:07.770 --> 06:08.490
object.

06:08.770 --> 06:12.010
So this was done with the link chain expression language.

06:12.290 --> 06:12.770
All right.

06:12.810 --> 06:18.130
Now I remind you that I still owe you an explanation why I did not use GPT five.

06:18.170 --> 06:24.450
Here, you see, I'm using GPT four here and not GPT five, even though it's the latest and greatest

06:24.450 --> 06:24.810
here.

06:25.210 --> 06:27.130
Now, the reason for it.

06:27.130 --> 06:31.250
Let me just show you what's going to happen if I'm going to be using GPT five.

06:31.650 --> 06:35.050
So let's go and let's go and switch it.

06:35.490 --> 06:37.370
Let me now run everything.

06:39.610 --> 06:41.770
And you can see now I get an error here.

06:42.290 --> 06:47.330
Now this is a 400 code error with unsupported parameter.

06:47.370 --> 06:49.610
Stop is not supported for this model.

06:49.930 --> 06:50.490
All right.

06:50.490 --> 06:54.090
And it's not that GPT five is a bad model.

06:54.210 --> 06:56.730
It's actually the most advanced and most capable.

06:56.930 --> 07:00.570
The reason is that this model does not support the stop argument.

07:00.690 --> 07:06.210
And in the next section, we'll be elaborating on this stop argument in the react algorithm.

07:06.210 --> 07:07.870
And why do we need it?

07:08.190 --> 07:13.990
But in fact, if we're going to be using the tool calling agent, which is going to be leveraging function

07:13.990 --> 07:20.390
calling, it should work just as fine and will be elaborating all of that in the course about function

07:20.390 --> 07:22.070
calling, about tool calling.

07:22.190 --> 07:25.190
And this is the next evolution of the agents.

07:25.230 --> 07:30.150
But what we just witnessed here, what we just saw here, this is the basic for everything.

07:30.350 --> 07:33.310
If you understand that, you will understand everything.

07:33.870 --> 07:35.310
All right let's finish it up.

07:35.310 --> 07:40.790
Let me go in format everything and let me add and commit everything here.

07:41.070 --> 07:43.550
So those are all the files that changed.

07:43.950 --> 07:46.190
And let me go and commit it.

07:46.310 --> 07:51.510
So I'll call the message here added output parsing.

07:52.150 --> 07:53.790
And let me push that.

07:54.270 --> 08:00.750
Now if you want to get the code you can simply go to the branch project slash search agent.

08:00.950 --> 08:03.590
And you can check out the commits there.

08:03.830 --> 08:08.150
And yeah, you can compare the code and even clone it and use it.
