WEBVTT

00:00.090 --> 00:01.650
-: All right, let's start having a look

00:01.650 --> 00:03.510
at some of the different types of functionality

00:03.510 --> 00:04.800
that we can use in LangGraph.

00:04.800 --> 00:05.910
Just before we get into that,

00:05.910 --> 00:07.530
I just want to make a highlight and say,

00:07.530 --> 00:11.040
if you haven't already, download the GitHub repository link

00:11.040 --> 00:12.360
inside of this course.

00:12.360 --> 00:13.920
We provide lots of different notebooks

00:13.920 --> 00:15.720
for working specifically with LangGraph,

00:15.720 --> 00:18.360
and we've also got some more advanced guides there.

00:18.360 --> 00:20.220
Okay, so the first one we're gonna do

00:20.220 --> 00:21.780
is install a couple of different packages.

00:21.780 --> 00:23.566
So we'll be installing LangGraph, LangSmith,

00:23.566 --> 00:25.080
and LangChain OpenAI.

00:25.080 --> 00:27.090
And we've got some functionality here,

00:27.090 --> 00:31.410
which just checks to see, do you have a OpenAI API Key.

00:31.410 --> 00:33.180
So I'm obviously gonna run all these.

00:33.180 --> 00:36.360
And the important point here is you can use

00:36.360 --> 00:39.540
one of LangChain's products called LangSmith,

00:39.540 --> 00:42.210
which will give you some of the observability

00:42.210 --> 00:43.470
to see what's happening inside

00:43.470 --> 00:45.360
of these LangGraph state machines.

00:45.360 --> 00:47.700
I'm probably not gonna do it in this scenario,

00:47.700 --> 00:49.860
but I'll leave it by size and point.

00:49.860 --> 00:51.750
Now we're gonna build this chatbot over time.

00:51.750 --> 00:54.510
So we'll start by just building a very simple chatbot.

00:54.510 --> 00:56.820
And then over the next couple of coming lessons,

00:56.820 --> 00:59.250
we'll start adding more and more functionality

00:59.250 --> 01:01.200
into this state machine.

01:01.200 --> 01:02.340
The first thing we're gonna do

01:02.340 --> 01:05.190
is we're gonna just have a look at some very simple things

01:05.190 --> 01:06.090
inside of LangGraph.

01:06.090 --> 01:09.390
So we've got this idea of a StateGraph,

01:09.390 --> 01:12.420
and we are using something called a TypedDict.

01:12.420 --> 01:14.070
Now TypedDict is quite interesting

01:14.070 --> 01:17.910
because it allows us to have some type, typing functionality

01:17.910 --> 01:20.940
over, specifically over a variable.

01:20.940 --> 01:22.920
And what we have in this scenario is

01:22.920 --> 01:27.210
we have a list of messages, which is an annotated list,

01:27.210 --> 01:30.210
and we have this add messages annotation.

01:30.210 --> 01:34.170
And in this annotation it is actually how

01:34.170 --> 01:36.000
the update happens to that.

01:36.000 --> 01:39.840
And so you can see LangGraph provides a add messages one,

01:39.840 --> 01:42.750
which will basically merge two lists of messages,

01:42.750 --> 01:46.860
and it updates the existing messages by a specific ID.

01:46.860 --> 01:48.600
So what's happening is,

01:48.600 --> 01:50.580
imagine you've got these two messages,

01:50.580 --> 01:53.670
like messages and messages.

01:53.670 --> 01:55.380
The add messages will make sure

01:55.380 --> 01:57.753
that these two lists are merged, right?

01:58.980 --> 02:01.320
So we've got our StateGraph,

02:01.320 --> 02:03.570
and you can have other keys in here,

02:03.570 --> 02:04.620
which we'll see later on.

02:04.620 --> 02:06.840
So you could say like, is human, right?

02:06.840 --> 02:08.130
Which is the type of string.

02:08.130 --> 02:10.170
Or we could say like, you know,

02:10.170 --> 02:13.440
has completed workflow, right?

02:13.440 --> 02:14.670
We can have a Boolean.

02:14.670 --> 02:17.640
So I want you to think about these as types

02:17.640 --> 02:19.890
which will be used inside of your state machine

02:19.890 --> 02:22.020
to hold data, okay?

02:22.020 --> 02:25.533
For now, we're just gonna use the messages key.

02:26.940 --> 02:29.340
We set up our graph with the graph builder.

02:29.340 --> 02:30.960
Now if we actually have a look at what's happening

02:30.960 --> 02:33.810
inside of this graph, it has a StateGraph,

02:33.810 --> 02:35.370
and it has a lot of different functionalities.

02:35.370 --> 02:39.060
So you can have these nodes, we can have edges,

02:39.060 --> 02:41.040
we can have branches,

02:41.040 --> 02:44.730
we can also do things like set the entry point,

02:44.730 --> 02:46.560
set the finish point.

02:46.560 --> 02:48.390
We have various different things,

02:48.390 --> 02:51.810
and we've also got some for persistence as well.

02:51.810 --> 02:54.300
Now, the most important ones that you'll use quite a lot

02:54.300 --> 02:57.870
is the add node, the add edge, the add conditional edges.

02:57.870 --> 02:59.910
So we have our piece of state,

02:59.910 --> 03:01.260
which we can see as a TypedDict

03:01.260 --> 03:03.030
with that single messages key.

03:03.030 --> 03:06.360
And it's annotated with the ad messages function.

03:06.360 --> 03:07.950
And then now what we need to do

03:07.950 --> 03:09.990
is we need to have every node,

03:09.990 --> 03:13.470
which will basically receive the current state as the input

03:13.470 --> 03:16.140
and return a value that updates that state.

03:16.140 --> 03:17.130
The second thing we're gonna do

03:17.130 --> 03:19.620
is the messages will be appended to the current list

03:19.620 --> 03:21.420
rather than directly overwritten.

03:21.420 --> 03:23.310
And that's kind of how we're using that

03:23.310 --> 03:25.320
with this add messages function.

03:25.320 --> 03:27.900
So we're gonna load up the chat OpenAI model.

03:27.900 --> 03:29.640
We're gonna use GPT-4o.

03:29.640 --> 03:31.380
And then we have a node here,

03:31.380 --> 03:32.880
which we're gonna make into a node

03:32.880 --> 03:35.850
where we take in the state, right?

03:35.850 --> 03:38.100
And on the state what we're gonna say is

03:38.100 --> 03:40.950
return messages, right?

03:40.950 --> 03:43.920
And also then we've got the llm.invoke,

03:43.920 --> 03:45.570
which takes in the messages,

03:45.570 --> 03:48.360
and it invokes the chat model with those messages.

03:48.360 --> 03:50.490
And notice how we're wrapping this in the list

03:50.490 --> 03:53.190
so that it actually gets added on back.

03:53.190 --> 03:56.100
Now, once you've got your Python function set up

03:56.100 --> 03:58.350
to take in the LangGraph state,

03:58.350 --> 04:01.590
and to manipulate that state, you can then add the node.

04:01.590 --> 04:02.820
And so that's what you've got here

04:02.820 --> 04:04.140
where we've got that graph builder,

04:04.140 --> 04:06.030
and we're adding the node of chatbot

04:06.030 --> 04:07.413
to this Python function.

04:09.390 --> 04:11.280
Now we also have to decide

04:11.280 --> 04:13.590
where do we start inside of the graph?

04:13.590 --> 04:15.360
Where do we start in our state machine?

04:15.360 --> 04:17.610
So as we've only got one node in the graph,

04:17.610 --> 04:19.953
we're gonna set the entry point to chatbot.

04:20.790 --> 04:21.960
And also as well as that,

04:21.960 --> 04:24.360
we can set the finish point, right?

04:24.360 --> 04:26.610
So the finish point can be chatbot.

04:26.610 --> 04:28.860
Now we can compile the graph,

04:28.860 --> 04:31.200
and this will create a compile graph type.

04:31.200 --> 04:32.880
So you'll see if we actually look at this,

04:32.880 --> 04:34.800
it's a compiled StateGraph.

04:34.800 --> 04:36.060
And we can look at things like,

04:36.060 --> 04:37.710
we can look at the nodes that there are.

04:37.710 --> 04:39.660
You can see this is the starting node,

04:39.660 --> 04:41.460
and we've got other ones as well.

04:41.460 --> 04:46.460
So what we can now do is we can use the get graph function

04:46.740 --> 04:49.170
on the compiled graph to actually see

04:49.170 --> 04:51.420
what that state machine looks like.

04:51.420 --> 04:52.950
So you can see we've got the start,

04:52.950 --> 04:55.440
which goes into chatbot, which then ends.

04:55.440 --> 04:56.400
So let's try and run it.

04:56.400 --> 04:58.590
So we've got a while loop here.

04:58.590 --> 05:02.070
And while this is running, we are getting the user's input.

05:02.070 --> 05:06.150
And if the input is not quit, exit, or q,

05:06.150 --> 05:07.980
then we're gonna run this bit of code.

05:07.980 --> 05:11.130
But if it is, then we're gonna run this blocker code here.

05:11.130 --> 05:12.600
So that'll be printing goodbye

05:12.600 --> 05:14.280
and breaking out the while loop.

05:14.280 --> 05:15.113
So let's have a look.

05:15.113 --> 05:16.920
So I can say, hello.

05:16.920 --> 05:19.140
My name is James.

05:19.140 --> 05:20.160
I'm just typing this.

05:20.160 --> 05:22.774
And then it says, how can I assist you today?

05:22.774 --> 05:24.783
I'll say, what is going on.

05:25.920 --> 05:26.753
Right?

05:27.990 --> 05:29.460
And then it's giving me a response

05:29.460 --> 05:32.103
and I can say, let's say quit.

05:32.970 --> 05:35.850
Now what's going on here is we've just quit out of the loop,

05:35.850 --> 05:37.740
but you can see how we call this.

05:37.740 --> 05:40.080
So we do a graph.stream,

05:40.080 --> 05:43.470
and the input is the first argument, okay?

05:43.470 --> 05:48.210
And the input here is our Python dictionary

05:48.210 --> 05:49.200
with the state.

05:49.200 --> 05:52.170
So we're almost saying to the state,

05:52.170 --> 05:53.850
if we scroll back up here

05:53.850 --> 05:56.730
and we have a look, we have messages here.

05:56.730 --> 06:00.120
Well, we can actually set the inputs for that

06:00.120 --> 06:04.020
specifically when we do the .stream functionality.

06:04.020 --> 06:06.120
The final thing I just want to make you aware of

06:06.120 --> 06:09.330
is that you can also call a graph using other metrics

06:09.330 --> 06:14.310
and functions such as the .invoke function.

06:14.310 --> 06:16.650
So if we don't want to stream the nodes

06:16.650 --> 06:19.560
and the events that are happening inside of the graph,

06:19.560 --> 06:21.450
we could just call the .invoke.

06:21.450 --> 06:23.730
And so you can see here we've got the messages,

06:23.730 --> 06:25.380
and we've said, hello world,

06:25.380 --> 06:29.430
and the AI has responded with, How can I assist you today?

06:29.430 --> 06:30.900
In the next lesson, what we're gonna look at

06:30.900 --> 06:32.880
is how you can add memory

06:32.880 --> 06:35.970
to your specific LangGraph state machine,

06:35.970 --> 06:40.350
and also how you can save that state machine to disc

06:40.350 --> 06:43.020
and then load that state machine as well.

06:43.020 --> 06:44.913
So we'll be looking at persistence.
