1
00:00:03,000 --> 00:00:07,920
Welcome to this video on Getting Started with LangGraph 101.

2
00:00:07,920 --> 00:00:12,920
In this video, you will explore how LangGraph uses graphs to represent agent workflows.

3
00:00:12,920 --> 00:00:17,979
You'll utilize powerful capabilities, such as looping and conditional branching, essential

4
00:00:17,979 --> 00:00:22,719
for dynamic workflows. You will also build a LangGraph application and visualize LangGraph

5
00:00:22,719 --> 00:00:26,920
workflow execution and state progression.

6
00:00:26,920 --> 00:00:31,200
LangGraph is a powerful framework for stateful multi-agent applications. You'll build a

7
00:00:31,200 --> 00:00:36,040
counter to understand key components, starting at 1, incrementing its value, generating a

8
00:00:36,040 --> 00:00:39,880
random letter, and printing results until 13.

9
00:00:39,880 --> 00:00:45,060
With LangGraph, you can construct and manage complex, multidimensional state structures.

10
00:00:45,060 --> 00:00:51,599
For example, a counter. It includes an integer and a random letter. State is commonly defined

11
00:00:51,599 --> 00:00:57,599
with TypedDict, but can be lists, nested structures, or message sequences, holding

12
00:00:57,599 --> 00:01:01,959
all graph inputs, intermediate values, and outputs.

13
00:01:01,959 --> 00:01:08,599
In LangGraph, you can define state variables, such as 'n' and 'letter', using a TypedDict class.

14
00:01:08,599 --> 00:01:11,919
Use the typing module to specify variable types.

15
00:01:11,919 --> 00:01:17,720
This class, a TypedDict subtype, acts as a dictionary with typed information. Within

16
00:01:17,720 --> 00:01:24,080
this class, define 'n' as integer and 'letter' as a string. Create a chain state object,

17
00:01:24,080 --> 00:01:28,959
which is essentially a typed dictionary. This represents a row of your table and supports

18
00:01:28,959 --> 00:01:31,959
complex types.

19
00:01:31,959 --> 00:01:36,480
LangGraph nodes link to functions that process state. This example function takes a chain

20
00:01:36,480 --> 00:01:42,819
state, which is a dictionary with typed keys and and letter as input. Inside, it generates

21
00:01:42,819 --> 00:01:47,620
a random lowercase letter. The return statement uses the double star to unpack the original

22
00:01:47,680 --> 00:01:53,059
dictionary, increments 'n' by 1, and assigns the new letter to the letter field.

23
00:01:53,059 --> 00:01:55,860
The result is a chain state object.

24
00:01:55,860 --> 00:02:00,099
You don't always need to unpack the entire state object, but returned keys and values

25
00:02:00,099 --> 00:02:01,779
are critical.

26
00:02:01,779 --> 00:02:06,120
LangGraph supports various graph types, each with distinct rules for state updates, whether

27
00:02:06,120 --> 00:02:12,160
it requires full returns, automatically merges updates, or drops missing fields. Always consult

28
00:02:12,160 --> 00:02:15,259
documentation for your specific graph type.

29
00:02:15,259 --> 00:02:20,080
This function takes a chain state object as input and prints 'n' and 'letter' values.

30
00:02:20,080 --> 00:02:24,399
It returns the state unchanged, used solely for side effects or printing, rather than

31
00:02:24,399 --> 00:02:29,520
data modification. Typically, nodes are designed to modify data.

32
00:02:29,520 --> 00:02:34,080
The first step is to create a state graph object with your chain state class. LangGraph

33
00:02:34,080 --> 00:02:39,839
workflows consist of nodes, which contain logic to process state, and edges, which define

34
00:02:39,839 --> 00:02:44,179
transitions between nodes based on conditions.

35
00:02:44,179 --> 00:02:48,199
Another state is the end state, an object that signals to LangGraph that the workflow

36
00:02:48,199 --> 00:02:49,960
is complete.

37
00:02:49,960 --> 00:02:55,000
First, incorporate the add function into the state graph using the add node method. The

38
00:02:55,000 --> 00:02:58,800
method takes two arguments, the node's name and the function.

39
00:02:58,800 --> 00:03:04,039
Next, add the print node to input and print the state variable.

40
00:03:04,039 --> 00:03:08,600
Node name may differ from function name. This is because node names are identifiers and

41
00:03:08,600 --> 00:03:11,080
can differ from function names.

42
00:03:11,339 --> 00:03:17,539
Next, add an edge between add and print_out nodes using the add_edge method. The first

43
00:03:17,539 --> 00:03:21,979
parameter specifies the starting node and the second parameter defines the destination

44
00:03:21,979 --> 00:03:29,380
node. After add node increments state, the updated state automatically passes to print.

45
00:03:29,380 --> 00:03:34,139
Special edges control next node processing. These are directed by functions evaluating

46
00:03:34,139 --> 00:03:39,779
the current state. For example, checking if 'n' is greater than or equal to 13 and returning

47
00:03:39,779 --> 00:03:42,100
true or false.

48
00:03:42,100 --> 00:03:47,000
You can insert a conditional edge using the add_conditional_edges method.

49
00:03:47,000 --> 00:03:53,279
The first argument is the node name before condition evaluation. At that point,

50
00:03:53,279 --> 00:03:57,800
the state has a value such as 'n' equals 10 and letter equals 's'.

51
00:03:57,800 --> 00:04:03,080
The second argument is the function that determines which node to go to next, in this case,

52
00:04:03,080 --> 00:04:07,639
stop_condition. This function takes the input state from the node named print

53
00:04:07,740 --> 00:04:11,100
and returns a boolean that directs the graph's flow.

54
00:04:11,100 --> 00:04:16,220
The next argument is a dictionary where keys are stop_condition outputs. Values

55
00:04:16,220 --> 00:04:21,619
are destination nodes. If stop_condition returns true, the graph transitions

56
00:04:21,619 --> 00:04:26,859
to end. If false, it continues to add.

57
00:04:26,859 --> 00:04:31,899
Using a dictionary to map values is useful. For example, routing a node's integer output

58
00:04:32,160 --> 00:04:38,359
1 to node_1, 2 to node_2, and 3 to end.

59
00:04:38,359 --> 00:04:43,440
Alternatively, design the conditional node to directly return the next node name based

60
00:04:43,440 --> 00:04:49,839
on state. For should_continue, if 'n' is greater than 13 from print node, return

61
00:04:49,839 --> 00:04:56,839
end. Otherwise, route back to add. This is interchangeable with dictionary mapping.

62
00:04:56,899 --> 00:05:02,100
Indicate the first processing node with set_entry_point method.

63
00:05:02,100 --> 00:05:08,739
Name specifies the graph's starting node for state processing. This start is visually represented.

64
00:05:08,739 --> 00:05:13,940
After connecting all nodes, call compile to build the runnable app. Use this app by passing

65
00:05:13,940 --> 00:05:20,220
a starting state. It will execute the workflow. Invoke the app object by calling invoke with

66
00:05:20,220 --> 00:05:26,339
an initial state dictionary. For example, 'n' equals 1 and an empty string for letter.

67
00:05:26,339 --> 00:05:30,959
As the workflow runs, the state moves through the graph, and the final state is returned

68
00:05:30,959 --> 00:05:34,179
into the result variable.

69
00:05:34,179 --> 00:05:38,760
State is first passed to the add node, where it is processed by incrementing 'n' and generating

70
00:05:38,760 --> 00:05:44,519
a random letter. The updated state then passes to the print node. In the print node, the

71
00:05:44,519 --> 00:05:46,880
state variables are printed.

72
00:05:46,880 --> 00:05:52,119
The state passes to the stop_condition function, checking if 'n' is greater than or

73
00:05:52,140 --> 00:05:58,660
equal to 13. If false, the state goes to the add node. The add node updates 'n' and generates

74
00:05:58,660 --> 00:06:02,980
a letter. The updated state then passes to the print node, where the state variables

75
00:06:02,980 --> 00:06:04,980
are printed.

76
00:06:04,980 --> 00:06:09,459
After iterations, 'n' is incremented, a new letter is generated, and results are printed.

77
00:06:09,459 --> 00:06:13,700
When stop_condition evaluates true, the end condition triggers completing the

78
00:06:13,700 --> 00:06:15,660
workflow.

79
00:06:15,660 --> 00:06:20,980
The result variable stores the final state value after workflow completion.

80
00:06:20,980 --> 00:06:22,880
In this video, you learned that

81
00:06:22,880 --> 00:06:28,559
State in LangGraph is a complex, evolving memory that contains all inputs, intermediate values,

82
00:06:28,559 --> 00:06:30,440
and outputs.

83
00:06:30,440 --> 00:06:35,119
Nodes are functions that process the current state. Some nodes modify the state, while

84
00:06:35,119 --> 00:06:37,839
others are used for side effects.

85
00:06:37,839 --> 00:06:42,239
Edges define how the execution flows between nodes, passing the updated state from one

86
00:06:42,239 --> 00:06:44,760
step to the next.

87
00:06:44,760 --> 00:06:49,320
Conditional edges allow the workflow to make dynamic decisions, routing the state to different

88
00:06:49,320 --> 00:06:50,679
nodes.

89
00:06:50,679 --> 00:06:55,839
Running a LangGraph application involves creating a state graph object, incorporating nodes,

90
00:06:55,839 --> 00:07:01,079
connecting them, setting an entry point, and then compiling the graph into a runnable application.

91
00:07:01,079 --> 00:07:06,160
Running a LangGraph workflow is done by invoking the compiled application with an initial state.

92
00:07:06,160 --> 00:07:10,579
Workflow visualization helps to understand the execution flow and how state progresses

93
00:07:10,579 --> 00:07:11,640
through different nodes.