Welcome to this video on Getting Started with LangGraph 101.
In this video, you will explore how LangGraph uses graphs to represent agent workflows.
You'll utilize powerful capabilities, such as looping and conditional branching, essential
for dynamic workflows. You will also build a LangGraph application and visualize LangGraph
workflow execution and state progression.
LangGraph is a powerful framework for stateful multi-agent applications. You'll build a
counter to understand key components, starting at 1, incrementing its value, generating a
random letter, and printing results until 13.
With LangGraph, you can construct and manage complex, multidimensional state structures.
For example, a counter. It includes an integer and a random letter. State is commonly defined
with TypedDict, but can be lists, nested structures, or message sequences, holding
all graph inputs, intermediate values, and outputs.
In LangGraph, you can define state variables, such as 'n' and 'letter', using a TypedDict class.
Use the typing module to specify variable types.
This class, a TypedDict subtype, acts as a dictionary with typed information. Within
this class, define 'n' as integer and 'letter' as a string. Create a chain state object,
which is essentially a typed dictionary. This represents a row of your table and supports
complex types.
LangGraph nodes link to functions that process state. This example function takes a chain
state, which is a dictionary with typed keys and and letter as input. Inside, it generates
a random lowercase letter. The return statement uses the double star to unpack the original
dictionary, increments 'n' by 1, and assigns the new letter to the letter field.
The result is a chain state object.
You don't always need to unpack the entire state object, but returned keys and values
are critical.
LangGraph supports various graph types, each with distinct rules for state updates, whether
it requires full returns, automatically merges updates, or drops missing fields. Always consult
documentation for your specific graph type.
This function takes a chain state object as input and prints 'n' and 'letter' values.
It returns the state unchanged, used solely for side effects or printing, rather than
data modification. Typically, nodes are designed to modify data.
The first step is to create a state graph object with your chain state class. LangGraph
workflows consist of nodes, which contain logic to process state, and edges, which define
transitions between nodes based on conditions.
Another state is the end state, an object that signals to LangGraph that the workflow
is complete.
First, incorporate the add function into the state graph using the add node method. The
method takes two arguments, the node's name and the function.
Next, add the print node to input and print the state variable.
Node name may differ from function name. This is because node names are identifiers and
can differ from function names.
Next, add an edge between add and print_out nodes using the add_edge method. The first
parameter specifies the starting node and the second parameter defines the destination
node. After add node increments state, the updated state automatically passes to print.
Special edges control next node processing. These are directed by functions evaluating
the current state. For example, checking if 'n' is greater than or equal to 13 and returning
true or false.
You can insert a conditional edge using the add_conditional_edges method.
The first argument is the node name before condition evaluation. At that point,
the state has a value such as 'n' equals 10 and letter equals 's'.
The second argument is the function that determines which node to go to next, in this case,
stop_condition. This function takes the input state from the node named print
and returns a boolean that directs the graph's flow.
The next argument is a dictionary where keys are stop_condition outputs. Values
are destination nodes. If stop_condition returns true, the graph transitions
to end. If false, it continues to add.
Using a dictionary to map values is useful. For example, routing a node's integer output
1 to node_1, 2 to node_2, and 3 to end.
Alternatively, design the conditional node to directly return the next node name based
on state. For should_continue, if 'n' is greater than 13 from print node, return
end. Otherwise, route back to add. This is interchangeable with dictionary mapping.
Indicate the first processing node with set_entry_point method.
Name specifies the graph's starting node for state processing. This start is visually represented.
After connecting all nodes, call compile to build the runnable app. Use this app by passing
a starting state. It will execute the workflow. Invoke the app object by calling invoke with
an initial state dictionary. For example, 'n' equals 1 and an empty string for letter.
As the workflow runs, the state moves through the graph, and the final state is returned
into the result variable.
State is first passed to the add node, where it is processed by incrementing 'n' and generating
a random letter. The updated state then passes to the print node. In the print node, the
state variables are printed.
The state passes to the stop_condition function, checking if 'n' is greater than or
equal to 13. If false, the state goes to the add node. The add node updates 'n' and generates
a letter. The updated state then passes to the print node, where the state variables
are printed.
After iterations, 'n' is incremented, a new letter is generated, and results are printed.
When stop_condition evaluates true, the end condition triggers completing the
workflow.
The result variable stores the final state value after workflow completion.
In this video, you learned that
State in LangGraph is a complex, evolving memory that contains all inputs, intermediate values,
and outputs.
Nodes are functions that process the current state. Some nodes modify the state, while
others are used for side effects.
Edges define how the execution flows between nodes, passing the updated state from one
step to the next.
Conditional edges allow the workflow to make dynamic decisions, routing the state to different
nodes.
Running a LangGraph application involves creating a state graph object, incorporating nodes,
connecting them, setting an entry point, and then compiling the graph into a runnable application.
Running a LangGraph workflow is done by invoking the compiled application with an initial state.
Workflow visualization helps to understand the execution flow and how state progresses
through different nodes.