-: All right, let's start having a look
at some of the different types of functionality
that we can use in LangGraph.
Just before we get into that,
I just want to make a highlight and say,
if you haven't already, download the GitHub repository link
inside of this course.
We provide lots of different notebooks
for working specifically with LangGraph,
and we've also got some more advanced guides there.
Okay, so the first one we're gonna do
is install a couple of different packages.
So we'll be installing LangGraph, LangSmith,
and LangChain OpenAI.
And we've got some functionality here,
which just checks to see, do you have a OpenAI API Key.
So I'm obviously gonna run all these.
And the important point here is you can use
one of LangChain's products called LangSmith,
which will give you some of the observability
to see what's happening inside
of these LangGraph state machines.
I'm probably not gonna do it in this scenario,
but I'll leave it by size and point.
Now we're gonna build this chatbot over time.
So we'll start by just building a very simple chatbot.
And then over the next couple of coming lessons,
we'll start adding more and more functionality
into this state machine.
The first thing we're gonna do
is we're gonna just have a look at some very simple things
inside of LangGraph.
So we've got this idea of a StateGraph,
and we are using something called a TypedDict.
Now TypedDict is quite interesting
because it allows us to have some type, typing functionality
over, specifically over a variable.
And what we have in this scenario is
we have a list of messages, which is an annotated list,
and we have this add messages annotation.
And in this annotation it is actually how
the update happens to that.
And so you can see LangGraph provides a add messages one,
which will basically merge two lists of messages,
and it updates the existing messages by a specific ID.
So what's happening is,
imagine you've got these two messages,
like messages and messages.
The add messages will make sure
that these two lists are merged, right?
So we've got our StateGraph,
and you can have other keys in here,
which we'll see later on.
So you could say like, is human, right?
Which is the type of string.
Or we could say like, you know,
has completed workflow, right?
We can have a Boolean.
So I want you to think about these as types
which will be used inside of your state machine
to hold data, okay?
For now, we're just gonna use the messages key.
We set up our graph with the graph builder.
Now if we actually have a look at what's happening
inside of this graph, it has a StateGraph,
and it has a lot of different functionalities.
So you can have these nodes, we can have edges,
we can have branches,
we can also do things like set the entry point,
set the finish point.
We have various different things,
and we've also got some for persistence as well.
Now, the most important ones that you'll use quite a lot
is the add node, the add edge, the add conditional edges.
So we have our piece of state,
which we can see as a TypedDict
with that single messages key.
And it's annotated with the ad messages function.
And then now what we need to do
is we need to have every node,
which will basically receive the current state as the input
and return a value that updates that state.
The second thing we're gonna do
is the messages will be appended to the current list
rather than directly overwritten.
And that's kind of how we're using that
with this add messages function.
So we're gonna load up the chat OpenAI model.
We're gonna use GPT-4o.
And then we have a node here,
which we're gonna make into a node
where we take in the state, right?
And on the state what we're gonna say is
return messages, right?
And also then we've got the llm.invoke,
which takes in the messages,
and it invokes the chat model with those messages.
And notice how we're wrapping this in the list
so that it actually gets added on back.
Now, once you've got your Python function set up
to take in the LangGraph state,
and to manipulate that state, you can then add the node.
And so that's what you've got here
where we've got that graph builder,
and we're adding the node of chatbot
to this Python function.
Now we also have to decide
where do we start inside of the graph?
Where do we start in our state machine?
So as we've only got one node in the graph,
we're gonna set the entry point to chatbot.
And also as well as that,
we can set the finish point, right?
So the finish point can be chatbot.
Now we can compile the graph,
and this will create a compile graph type.
So you'll see if we actually look at this,
it's a compiled StateGraph.
And we can look at things like,
we can look at the nodes that there are.
You can see this is the starting node,
and we've got other ones as well.
So what we can now do is we can use the get graph function
on the compiled graph to actually see
what that state machine looks like.
So you can see we've got the start,
which goes into chatbot, which then ends.
So let's try and run it.
So we've got a while loop here.
And while this is running, we are getting the user's input.
And if the input is not quit, exit, or q,
then we're gonna run this bit of code.
But if it is, then we're gonna run this blocker code here.
So that'll be printing goodbye
and breaking out the while loop.
So let's have a look.
So I can say, hello.
My name is James.
I'm just typing this.
And then it says, how can I assist you today?
I'll say, what is going on.
Right?
And then it's giving me a response
and I can say, let's say quit.
Now what's going on here is we've just quit out of the loop,
but you can see how we call this.
So we do a graph.stream,
and the input is the first argument, okay?
And the input here is our Python dictionary
with the state.
So we're almost saying to the state,
if we scroll back up here
and we have a look, we have messages here.
Well, we can actually set the inputs for that
specifically when we do the .stream functionality.
The final thing I just want to make you aware of
is that you can also call a graph using other metrics
and functions such as the .invoke function.
So if we don't want to stream the nodes
and the events that are happening inside of the graph,
we could just call the .invoke.
And so you can see here we've got the messages,
and we've said, hello world,
and the AI has responded with, How can I assist you today?
In the next lesson, what we're gonna look at
is how you can add memory
to your specific LangGraph state machine,
and also how you can save that state machine to disc
and then load that state machine as well.
So we'll be looking at persistence.