WEBVTT

00:00.160 --> 00:02.400
So this next part is the most important part.

00:02.440 --> 00:04.400
It's also probably the most complex.

00:04.920 --> 00:11.960
Now you're comfortable with the fact that we are going to be sending this JSON to the LLM and giving

00:11.960 --> 00:15.760
it the option to reply when it when it generates its response.

00:15.920 --> 00:20.640
It can opt to say that it wants to run one of these tools.

00:20.640 --> 00:25.760
It wants to run this tool or it wants to run this tool.

00:26.480 --> 00:33.120
So the next thing that we've got to do is write a function which is going to handle what happens when

00:33.120 --> 00:36.480
the LLM responds and says, yes, I do want to run this tool.

00:36.520 --> 00:39.840
Please run it and provide me with the output.

00:40.320 --> 00:45.960
So that is the purpose of this function that I've written right here called handle tool calls.

00:46.400 --> 00:50.120
And it's a function which takes a list of tool calls.

00:50.400 --> 00:51.520
And it will run them.

00:52.080 --> 00:56.560
And it will add the responses to the results that it returns.

00:57.160 --> 00:58.320
So it has a loop.

00:58.320 --> 01:00.040
It loops through these tool calls.

01:00.080 --> 01:03.040
Typically a model will only call one tool at a time.

01:03.040 --> 01:06.880
But actually the construct supports multiple tools being run.

01:06.880 --> 01:08.680
And so we want to support that here.

01:08.680 --> 01:09.840
So so we do that.

01:10.280 --> 01:12.600
So we loop through all of the tool calls.

01:12.800 --> 01:13.920
We look at this.

01:13.960 --> 01:15.680
What's come back in this this tool call.

01:15.680 --> 01:19.360
And I should say this is actually it's a structured output.

01:19.400 --> 01:24.200
It's JSON coming back from the LM that's been put into an object.

01:24.200 --> 01:28.040
And so this is one of the reasons why I said it's very analogous to structured outputs.

01:28.040 --> 01:32.240
So back has come this JSON object is in the form of of an object tool calls.

01:32.280 --> 01:37.640
And we are going to look at the function name that it's providing back in that response.

01:38.120 --> 01:40.320
And we are going to be calling that the tool name.

01:40.320 --> 01:43.000
And that is the name of the function it wants to call.

01:43.200 --> 01:48.000
And it also provides us with the parameters, the arguments that should be put into that function.

01:48.000 --> 01:50.160
And we can we can strip them out like this.

01:50.560 --> 01:53.120
And so I'm then just going to print we're calling this tool.

01:53.840 --> 01:56.880
And what comes next is an if statement.

01:56.880 --> 01:58.240
Just like I promised you.

01:58.600 --> 02:02.880
We say if the tool name is record user details.

02:02.880 --> 02:09.880
If it wants to to call this tool, then we call the function that's called record user details.

02:09.880 --> 02:11.520
And we pass in the arguments.

02:12.000 --> 02:18.840
But conversely if the tool is this tool record unknown question right here in the JSON.

02:19.200 --> 02:21.400
Then we call the function called record.

02:21.440 --> 02:22.120
Unknown question.

02:22.120 --> 02:23.600
And we pass in the parameters.

02:24.000 --> 02:31.160
And then we add in whatever came back from that function call into the list of messages.

02:31.160 --> 02:32.440
And that's what we return.

02:33.240 --> 02:38.560
So that is the the extent of the handle tool calls function.

02:38.560 --> 02:41.600
And as I told you it's got this if statement.

02:42.480 --> 02:45.200
So hopefully this more concrete for you.

02:45.200 --> 02:46.280
You see that coming together.

02:46.280 --> 02:49.720
And if not come and look through this and and get comfortable with it.

02:50.160 --> 02:50.600
Okay.

02:50.640 --> 02:54.440
And what I've got next is something a bit sneaky.

02:54.680 --> 02:58.240
When you look at this you might think to yourself, this is a bit dumb.

02:58.440 --> 03:03.320
We've got this thing that looks for a string and then calls a function with the same name and looks

03:03.320 --> 03:05.840
for a different string, and calls a function with the same name.

03:05.880 --> 03:08.760
Now we know that Python is full of magic stuff.

03:09.080 --> 03:14.480
Isn't there a way in Python to do a sort of reflection thing, and just call that function with the

03:14.480 --> 03:16.560
same name as that string?

03:16.600 --> 03:18.280
And the answer is yes, of course there is.

03:18.440 --> 03:23.120
And this is this is one way of doing it that works in this particular situation.

03:23.120 --> 03:29.610
There is this thing called globals which gives you a dictionary which you can use to look up any function

03:29.610 --> 03:31.570
which is in the global scope.

03:31.570 --> 03:35.610
And so I can use that to look up a function called record.

03:35.650 --> 03:37.090
Unknown question.

03:37.290 --> 03:40.690
And that will give me the actual function record.

03:40.730 --> 03:41.410
Unknown question.

03:41.410 --> 03:43.810
And I can then call it like that.

03:43.810 --> 03:45.210
This is a really hard question.

03:45.250 --> 03:49.050
So if I run this it should send a push notification to my phone.

03:49.050 --> 03:49.690
Let's see.

03:51.490 --> 03:52.810
Yep, that seemed to work.

03:53.010 --> 03:55.170
I see recording this is a really hard question.

03:55.170 --> 03:55.890
So that works.

03:55.890 --> 03:56.770
Let's do it again.

03:57.610 --> 03:59.010
Ah, it makes it worth it.

03:59.050 --> 04:00.370
Pinging when I get emails.

04:00.370 --> 04:01.370
So, uh.

04:01.370 --> 04:01.690
Yeah.

04:01.730 --> 04:04.210
There you see that that this trickery works.

04:04.210 --> 04:11.330
And of course, that means that I can rewrite the handle tool calls function and do it this way so that

04:11.330 --> 04:14.250
now basically there's no more if statement.

04:14.250 --> 04:21.490
I've got that cunning piece of logic in there that plucks out the right function and then dynamically

04:21.490 --> 04:23.970
calls that function like that.

04:24.690 --> 04:29.570
And I can see that it would be better to do this for sure.

04:30.250 --> 04:37.530
So, um, yeah, the, the this obviously gives us a workaround to having the if statement that we had

04:37.530 --> 04:38.050
before.

04:38.050 --> 04:43.250
But I don't want you to think for one minute that that means that this isn't just a glorified if statement,

04:43.250 --> 04:44.570
because that's all it is.

04:44.610 --> 04:50.170
Sure, we've got some trickery to use a dictionary to look up that has got around having to list things

04:50.170 --> 04:50.570
out.

04:50.770 --> 04:52.490
But at the end of the day, it's still the same thing.

04:52.490 --> 04:57.410
We're just taking some text and we're using that to map to a function name, and we're calling that

04:57.410 --> 04:58.130
function.

04:58.130 --> 05:01.010
It's a glorified if statement, nothing more.

05:01.250 --> 05:03.690
So that's how this works.

05:03.690 --> 05:07.650
And I hope that this now gives you a bit of perspective of what's going on.

05:07.690 --> 05:12.730
The good news is you're never going to have to do this again after week one, because all of the other

05:12.730 --> 05:14.970
frameworks take care of this kind of stuff for you.

05:15.010 --> 05:19.690
You don't need to futz around with with plucking out JSON and turning it into function calls.

05:19.690 --> 05:21.850
That's exactly what these frameworks did.

05:21.850 --> 05:24.610
It's people that wrote stuff like this and thought, you know what?

05:24.610 --> 05:28.650
It would be easy just to put this into a nice little framework so that people don't have to worry about

05:28.650 --> 05:29.330
this anymore.

05:29.450 --> 05:32.370
So you won't have to worry about it anymore after this.

05:32.370 --> 05:38.250
But I wanted this to give you a real perspective on how it works, what's going on so that you've got

05:38.250 --> 05:43.450
that foundation, and when you actually get to use the frameworks, you know exactly what it's actually

05:43.450 --> 05:43.850
doing.
