WEBVTT

00:00.080 --> 00:00.400
Okay.

00:00.440 --> 00:03.360
So now we're going to build the same graph that we did last time.

00:03.360 --> 00:05.080
But we're going to add in some tools.

00:05.440 --> 00:11.880
Uh, so um, one small change I'm going to make though is this time for the state object.

00:11.880 --> 00:13.800
We're not going to use a Pydantic object.

00:13.800 --> 00:15.640
We're going to use a typed dict.

00:15.800 --> 00:17.720
And it looks almost identical.

00:17.720 --> 00:22.560
It's just a it's a subclass of typed dict instead of pydantic base model.

00:22.760 --> 00:24.360
Um, but otherwise the same thing.

00:24.400 --> 00:25.600
Messages field.

00:25.640 --> 00:26.760
It's annotated.

00:26.760 --> 00:27.680
It's a list.

00:27.680 --> 00:34.280
And this is telling Landgraf that we're going to use the Add Messages reducer as a way of reducing.

00:34.320 --> 00:35.720
Otherwise it's just the same.

00:35.720 --> 00:40.400
So you can stick with Pydantic if you'd prefer, but this is a different way of doing it.

00:40.680 --> 00:44.640
Uh, so then we start the graph builder right there.

00:45.280 --> 00:47.120
And now we have something different.

00:47.120 --> 00:52.920
So it's worth keeping in mind, in your mind that when when we work with tools, there's actually two

00:52.920 --> 00:56.920
different places in our code that typically we have to worry about tools.

00:56.960 --> 01:03.480
And I've explained this up here first of all, when we're, when we're, uh, making the call to the

01:03.650 --> 01:04.650
model, to OpenAI.

01:05.010 --> 01:10.050
That's the point at which we have to look at our tools and create all of that JSON nonsense.

01:10.090 --> 01:14.730
All of the stuff to describe what the tool is, so that when we prompt the model, it knows what it

01:14.730 --> 01:15.410
can do.

01:15.530 --> 01:18.170
So that's one piece of work that has to happen.

01:18.610 --> 01:22.930
And then another piece of work is when the model then responds.

01:22.930 --> 01:27.650
We have to test to see whether is the finish reason tool calls.

01:27.770 --> 01:34.010
And if so then we have to do that stuff, which was the quite long winded handle tool call uh, method

01:34.010 --> 01:38.970
in the first week where we unpacked the tool call and then we did some clever stuff.

01:38.970 --> 01:43.890
You can either have just an if statement, or you can have something clever that goes and finds the

01:43.890 --> 01:45.370
function and calls it.

01:45.650 --> 01:50.970
But that whole piece of the puzzle, the, uh, receiving the message to call a tool from the model,

01:50.970 --> 01:53.450
is like another place where you have to do all of your coding.

01:53.490 --> 01:58.410
Those are the two points, and we'll see both those points separately in how we have to handle this

01:58.410 --> 01:59.330
with land graph.

01:59.850 --> 02:01.450
So this is the first one.

02:01.690 --> 02:09.300
When we create the model this is the chat OpenAI object which is the long chain wrapper around calling

02:09.300 --> 02:09.900
OpenAI.

02:11.060 --> 02:17.780
We also, once we've created it, we make another version of LLM, which is LLM with tools, which is

02:17.780 --> 02:21.140
after calling Bind tools to the LLM.

02:21.340 --> 02:27.260
And this is just some nice sort of magic, which is going to then figure out what are the tools that

02:27.260 --> 02:33.620
it can call and make sure that whenever we call that model, whenever we call this version of the model,

02:33.620 --> 02:36.820
it's going to to provide all of the tools it can do.

02:37.340 --> 02:38.460
So it's one of those examples.

02:38.460 --> 02:39.740
This is really powerful.

02:39.740 --> 02:40.900
It's lang chain here.

02:40.900 --> 02:47.460
And it's lang chain being really nice, making it simple to package away all of that JSON stuff and

02:47.460 --> 02:53.980
do it for us by abstracting around an LLM to have an LLM with tools which whenever you call it, it's

02:53.980 --> 02:56.580
going to automatically pass in all the tools it can call.

02:56.940 --> 03:01.900
The flip side of that is that it's it's sort of hiding from us some of the implementation and some of

03:01.900 --> 03:06.500
what's going on which which can be which can make it hard to debug and things.

03:06.500 --> 03:10.150
But but you certainly I mean hopefully you see the positives very much.

03:10.190 --> 03:11.470
It's really easy.

03:11.710 --> 03:12.910
That is done that now.

03:12.910 --> 03:18.710
Whenever we call liquid tools, it's going to automatically handle the the building of that JSON and

03:18.710 --> 03:19.710
the parsing it in.

03:20.230 --> 03:21.990
And so look look how simple this is.

03:21.990 --> 03:23.790
This is our chat bot function.

03:23.790 --> 03:29.990
This is our node our function for the chat bot which just as before we return messages.

03:30.110 --> 03:36.670
And then what we've got here is we just call invoke but not on the LM but on the LM with tools that

03:36.670 --> 03:38.110
knows already about those tools.

03:38.110 --> 03:42.310
It will build the JSON, it will parse the JSON in as the tools.

03:42.550 --> 03:43.910
So it's really clever.

03:44.110 --> 03:46.510
And then we add that node.

03:46.950 --> 03:48.950
And then there's this line here.

03:49.190 --> 03:50.550
This is part two.

03:50.590 --> 03:51.790
This is the other thing.

03:51.790 --> 03:53.990
This is handling the results back.

03:54.270 --> 03:58.110
So what's happening here is that we added that chatbot node.

03:58.110 --> 04:01.950
We're adding a second node called tools.

04:02.190 --> 04:04.470
And it is a special type of node.

04:04.470 --> 04:05.910
It's a tool node.

04:06.190 --> 04:09.750
And when you create a tool node you have to tell it okay.

04:09.790 --> 04:10.790
What are these tools.

04:10.830 --> 04:12.240
What is it that you do?

04:12.800 --> 04:20.760
And this tool node is basically like a canned node that when it's invoked, it's going to see whether

04:20.760 --> 04:26.960
any of the message is asking to call one of the tools that it's been assigned, that it was passed to

04:26.960 --> 04:28.240
it when it was created.

04:28.240 --> 04:31.640
That's the job of the tool node, and if so, it will run them.

04:32.120 --> 04:38.040
So one more time, this piece of code here is handling the request to the LM and packaging up the tools

04:38.040 --> 04:38.720
JSON.

04:39.000 --> 04:47.040
And this node here is handling what happens if a if there is a tool request, it's handling the unbundling

04:47.040 --> 04:51.800
of the request and the actual calling of the tool function.

04:51.800 --> 04:57.120
And if any of that didn't make sense, I think it hopefully will do very shortly.

04:57.320 --> 05:02.800
We're going to create the edges now and again a little bit more complicated than before.

05:03.280 --> 05:10.880
So there is an edge which is going from the chat bot to the tools from this node, which is the actual

05:10.880 --> 05:16.970
chat bot, the LM with tools to the thing that calls tools to the node that actually runs the different

05:16.970 --> 05:18.890
tools we need to connect those two.

05:19.410 --> 05:23.730
But that connection, it's a connection which which has like an if attached to it.

05:23.770 --> 05:25.450
We don't want to always call tools.

05:25.450 --> 05:33.050
We only want to call those tools if the model has returned a response that the finish reason is tool

05:33.090 --> 05:33.650
calls.

05:33.650 --> 05:36.290
We had to like code that ourselves in week one.

05:36.290 --> 05:42.290
If finish reason is tool calls so that if has to be included in here because it's only if that's true.

05:42.290 --> 05:47.330
It's only if the model returns that it wants to call tools that we actually want to to call this node.

05:47.650 --> 05:53.810
And so and so that is why when you look at this edge, it's not a normal edge.

05:53.810 --> 05:59.170
It's called a conditional edge, which means it's an edge that's only triggered in certain conditions.

05:59.210 --> 06:06.170
And like an if statement and you give it the node name, you give it the condition and you give it the

06:06.250 --> 06:07.970
the node that would then be called.

06:08.330 --> 06:14.290
And that condition called tools condition is again pre-canned with Landgraf.

06:14.290 --> 06:17.530
And of course it looks to see if the finish reason is handle tools.

06:17.930 --> 06:21.410
Uh, so, uh, sorry, the finish reason is tool calls.

06:21.570 --> 06:22.930
And so that is what.

06:22.970 --> 06:24.650
That's exactly what that does.

06:24.810 --> 06:29.890
So when I used to say that, that, uh, at the end of the day, tool calling is just an if statement.

06:29.890 --> 06:31.370
This is the if statement.

06:31.370 --> 06:34.010
This thing here is handling that if statement.

06:34.010 --> 06:38.930
And if the if statement is true then then it's calling a node called tools.

06:39.850 --> 06:40.610
Uh all right.

06:40.610 --> 06:47.330
And then the, the uh there's one more quite subtle point here, which is that you also have to make

06:47.330 --> 06:49.530
an edge between tools.

06:49.850 --> 06:56.570
Back to chatbot again, because the result of this, the result of running the tool, the output needs

06:56.570 --> 07:01.490
to get fed back into the chatbot and it needs to continue processing from there.

07:01.850 --> 07:07.210
So this is subtle and, and, uh, you know, once, once I've said it, it's probably obvious, but

07:07.210 --> 07:11.370
it's one of those things that if I hadn't said it, it's not something that would naturally have occurred

07:11.370 --> 07:11.530
to you.

07:11.530 --> 07:14.930
And if you don't do it, then things will go wrong in strange ways.

07:15.050 --> 07:16.290
So it's one of those times.

07:16.330 --> 07:18.170
Again, it's elegant, it's neat.

07:18.170 --> 07:22.620
It's great that Landgraf does that, but it can make it a bit harder to debug.

07:23.180 --> 07:23.460
All right.

07:23.460 --> 07:25.380
And then we're going to add that start edge.

07:25.660 --> 07:26.580
And then we're going to.

07:26.620 --> 07:28.140
Compile and picture it.

07:28.140 --> 07:31.220
And this will hopefully bring this together for you.

07:31.260 --> 07:32.820
Start goes to chat bot.

07:33.100 --> 07:35.540
This is a conditional line.

07:35.900 --> 07:43.620
If if the finish reason is is a tool call then it will come here because the tools condition will be

07:43.620 --> 07:44.060
true.

07:44.580 --> 07:46.340
Then it will it will be responsible.

07:46.340 --> 07:50.580
This node is a canned node that's responsible for calling the tools that are relevant.

07:50.940 --> 07:52.500
And then the results comes back.

07:52.500 --> 07:56.940
And this is a solid line because if it's got here it should always come back.

07:57.460 --> 08:02.620
And this is a dotted line because only in the event that this didn't happen then it will come to end

08:02.660 --> 08:03.300
naturally.

08:03.660 --> 08:09.100
Landgraf automatically adds an end node for any any unresolved condition like that.

08:09.660 --> 08:11.220
And so that is our graph.

08:11.220 --> 08:14.340
And I hope that seeing that graph has made it come together for you.

08:14.340 --> 08:17.060
So the two places where tools are incorporated.

08:17.060 --> 08:18.380
One is the chatbot itself.

08:18.380 --> 08:25.070
We're using the LM with tools and the other is in the conditional branch and this tools node itself.

08:25.350 --> 08:26.470
Well, I think it's enough chit chat.

08:26.510 --> 08:28.070
We should get on and run this thing.

08:28.150 --> 08:28.910
So here we go.

08:28.950 --> 08:30.110
We're going to.

08:30.310 --> 08:32.390
Oh, I think I'll make this consistent with how we've done it before.

08:32.430 --> 08:34.230
We'll call this chat not invoke graph.

08:34.230 --> 08:35.270
But it is.

08:35.310 --> 08:37.350
It is how we should think of it really.

08:37.470 --> 08:44.470
So we're going to um, we're going to, to bring up a gradient screen and we're just going to call it

08:44.470 --> 08:45.590
let's see if this works.

08:45.790 --> 08:46.230
Okay.

08:46.270 --> 08:47.430
So I'm going to say hi there.

08:50.070 --> 08:50.430
Hello.

08:50.430 --> 08:51.670
How can I assist you today.

08:51.950 --> 08:54.510
Well I'm going to do something fun here.

08:54.750 --> 09:07.830
I'm going to say uh, please send me a push notification with the current US dollar to great British

09:07.830 --> 09:09.510
pound exchange rate.

09:11.270 --> 09:17.510
So I'm hoping that it's going to look up the exchange rate, uh, by doing a web search, find it out,

09:17.550 --> 09:19.230
and then send me a push notification.

09:19.230 --> 09:20.390
This is ambitious.

09:20.390 --> 09:24.030
2 to 2 tool calls in one shot.

09:24.190 --> 09:26.480
Get that out of me and we'll see.

09:26.640 --> 09:27.960
Uh, we'll see how it does.

09:29.680 --> 09:30.640
So it's going.

09:30.680 --> 09:32.040
It's off, it's pausing.

09:32.240 --> 09:34.920
It's as you heard it certainly sent me something.

09:35.240 --> 09:40.520
The current USD GBP exchange rate is 0.78.

09:40.880 --> 09:43.680
Well, I happen to have a Google screen sitting over here.

09:43.680 --> 09:45.000
Let's bring this into view.

09:45.360 --> 09:53.040
There you will find the current US dollar GBP exchange rate is indeed 0.78.

09:53.400 --> 09:55.400
So that's cool.

09:55.400 --> 09:56.040
That's cool.

09:56.040 --> 09:56.880
I hope you agree.

09:56.920 --> 09:57.160
Sorry.

09:57.160 --> 09:58.080
Get that out of your way.

09:58.160 --> 09:59.200
I hope you agree.

09:59.440 --> 10:03.720
Uh, it's interesting to see that this has happened and that it's achieved this.

10:04.040 --> 10:10.360
And what we can see here is that we can come into Lange Smith and see the outcome of this.

10:10.360 --> 10:14.680
I'm interested that it's put that in red the four seconds that it took us.

10:14.920 --> 10:17.840
So let's go into this and see more.

10:18.200 --> 10:22.200
Please send me a push notification with the current USD GBP exchange rate.

10:22.520 --> 10:27.320
So we can see that that you can see the whole graph playing out here.

10:27.320 --> 10:32.370
And it's fascinating to see, uh, the, the, um, let's scroll down.

10:32.370 --> 10:33.970
It's doing a search right here.

10:33.970 --> 10:37.090
Click here current USD to GBP exchange rate.

10:37.210 --> 10:40.170
And that's the the result that came back.

10:40.690 --> 10:45.290
Um, this was from, from from doing the search just as we expected.

10:45.490 --> 10:49.970
And then it came back to a tools condition to a send push notification.

10:50.290 --> 10:54.610
Uh, and uh, back came the uh, the, the answer.

10:54.610 --> 10:57.250
And then that was the, uh, the conclusion.

10:57.570 --> 10:59.170
Um, there we go.

10:59.170 --> 11:01.290
And that's how it responded.

11:01.290 --> 11:09.850
So there we see the full, uh, the full chain of, of, uh, discussion happening within Lange Smith

11:09.850 --> 11:15.170
and how you can come in and debug this and see what's going on and see, indeed convince yourself that

11:15.170 --> 11:17.530
it did call the couple of tools.

11:17.650 --> 11:20.130
And this was great fun, and I hope you'll do the same thing.

11:20.170 --> 11:22.370
Come in now and run this and let it cool.

11:22.370 --> 11:28.250
Both tools go into Lange Smith and Trace through to make sure that you can also see all of these calls

11:28.250 --> 11:31.010
happening, uh, just just as we did right here.
