1
00:00:03,000 --> 00:00:09,000
Welcome to this video on The Art of AI Self-Improvement: Building Reflection Agents.

2
00:00:09,000 --> 00:00:13,279
In this video, you'll explore the concept of basic reflection agents, understanding

3
00:00:13,279 --> 00:00:18,360
how they iteratively improve AI outputs and how to set up generator and reflector roles

4
00:00:18,360 --> 00:00:19,879
for self-correction.

5
00:00:19,879 --> 00:00:24,760
You will then learn to apply both LengChain and LengGraph for agent development, ultimately

6
00:00:24,760 --> 00:00:29,799
building and running a complete reflection agent that refines its own content.

7
00:00:29,799 --> 00:00:33,099
Imagine an AI improving by learning from mistakes.

8
00:00:33,099 --> 00:00:38,159
This is the core of reflection agents, designed to analyze performance and enhance strategies

9
00:00:38,159 --> 00:00:39,759
critically.

10
00:00:39,759 --> 00:00:45,439
Reflection agents primarily fall into three types, basic reflection agent, reflexion agent,

11
00:00:45,439 --> 00:00:49,599
and language agent tree search, or LATS.

12
00:00:49,599 --> 00:00:53,080
This video focuses on basic reflection agents.

13
00:00:53,080 --> 00:00:55,759
Consider the user prompt, how do I look cool?

14
00:00:55,759 --> 00:00:58,279
This activates two LLM roles.

15
00:00:58,279 --> 00:01:02,080
The generator suggests an initial idea - "Wear a fedora."

16
00:01:02,080 --> 00:01:04,639
The reflector evaluates and critiques it.

17
00:01:04,639 --> 00:01:08,680
Fedoras are outdated and often stereotyped negatively.

18
00:01:08,680 --> 00:01:12,760
This loop runs for a set number of steps, refining the response before returning the

19
00:01:12,760 --> 00:01:14,519
final answer.

20
00:01:14,519 --> 00:01:18,639
For the second iteration, the generator creates an improved response.

21
00:01:18,639 --> 00:01:22,120
Wear well-fitted clothes, have good posture, and be confident.

22
00:01:22,120 --> 00:01:24,760
Authenticity beats trying to look cool.

23
00:01:24,760 --> 00:01:27,680
The reflector evaluates the improved response.

24
00:01:27,680 --> 00:01:31,680
Good advice focused on authenticity rather than specific items.

25
00:01:31,680 --> 00:01:34,120
Could add something about personal style.

26
00:01:34,120 --> 00:01:36,879
The generator provides the final output.

27
00:01:36,879 --> 00:01:41,599
Find clothes that match your personal style, maintain good posture, and be confident.

28
00:01:41,599 --> 00:01:45,559
True coolness comes from authenticity, not following trends.

29
00:01:45,559 --> 00:01:47,860
So a crisis has been averted.

30
00:01:47,860 --> 00:01:50,760
No more fedora, just flawless vibes!

31
00:01:50,760 --> 00:01:54,680
You will use reflection to build a LinkedIn post optimization agent.

32
00:01:54,680 --> 00:02:00,160
The agent will generate a post, critique its own output, and refine content iteratively.

33
00:02:00,160 --> 00:02:05,199
You will build a system with a post-generation phase, followed by a reflection, or AI review

34
00:02:05,199 --> 00:02:06,559
phase.

35
00:02:06,559 --> 00:02:09,199
This cycle ensures higher quality content.

36
00:02:09,199 --> 00:02:14,199
Initially, the generator accesses the HumanMessage and produces an output.

37
00:02:14,199 --> 00:02:18,960
The reflector uses both the generator's output and the original message.

38
00:02:18,960 --> 00:02:23,279
Both the generator and the reflector accumulate access to previous outputs, building memory

39
00:02:23,360 --> 00:02:25,639
over iterations.

40
00:02:25,639 --> 00:02:29,800
Start by initializing the LLM agent that will generate and critique content.

41
00:02:29,800 --> 00:02:34,520
In this example, you'll use IBM's Granite model via LangChain.

42
00:02:34,520 --> 00:02:38,320
To guide the generator LLM, use LangChain's chat prompt template.

43
00:02:38,320 --> 00:02:41,800
A SystemMessage defines the LLM's roles.

44
00:02:41,800 --> 00:02:46,679
Message's placeholder serves as memory, maintaining user inputs for the generator.

45
00:02:46,679 --> 00:02:53,080
Using the pipe operator, connect the structured prompt to the LLM, creating generate_chain.

46
00:02:53,080 --> 00:02:58,000
Similarly, create a prompt for the reflection LLM where the assistant evaluates the generated

47
00:02:58,000 --> 00:02:59,800
LinkedIn post.

48
00:02:59,800 --> 00:03:04,520
Define a reflection prompt using chat prompt template which includes a SystemMessage that

49
00:03:04,520 --> 00:03:15,199
frames the model as a professional LinkedIn content strategist.

50
00:03:15,199 --> 00:03:18,639
Include a message's placeholder to inject the post to be critiqued.

51
00:03:18,800 --> 00:03:23,279
Finally, chain the prompt to the LLM using pipe operator.

52
00:03:23,279 --> 00:03:27,800
Use LangGraph to build a conversational workflow by defining an agent state, a structure that

53
00:03:27,800 --> 00:03:30,440
tracks the evolving context.

54
00:03:30,440 --> 00:03:35,059
LangGraph simplifies this with MessageGraph, a special graph type whose state holds only

55
00:03:35,059 --> 00:03:40,440
an array of messages, such as HumanMessage, AIMessage, SystemMessage.

56
00:03:40,440 --> 00:03:45,279
Think of MessageGraph as a specialized state graph that accumulates different message types.

57
00:03:45,279 --> 00:03:49,699
Each user turn adds a HumanMessage followed by an AIMessage.

58
00:03:49,699 --> 00:03:55,000
To build a generate node, import BaseMessage, HumanMessage, and AIMessage for message

59
00:03:55,000 --> 00:03:56,000
types.

60
00:03:56,000 --> 00:03:58,080
Import list and sequence for type-ins.

61
00:03:58,080 --> 00:04:02,240
The tool takes and returns a list-like object of BaseMessages.

62
00:04:02,240 --> 00:04:07,039
The generate node takes the state variable as input including the original HumanMessage,

63
00:04:07,039 --> 00:04:09,000
make me look cool on LinkedIn.

64
00:04:09,000 --> 00:04:13,279
The input HumanMessage is passed to the invoke function as a state.

65
00:04:13,279 --> 00:04:18,079
The chain uses that context to generate a response, "Wear a fedora and sunglasses in

66
00:04:18,079 --> 00:04:19,559
your profile pic."

67
00:04:19,559 --> 00:04:24,079
Finally, the function returns the output, wrapped in an AIMessage, and ready for the

68
00:04:24,079 --> 00:04:26,119
next stage in the workflow.

69
00:04:26,119 --> 00:04:29,480
The state variable contains the input message as a list.

70
00:04:29,480 --> 00:04:34,119
When the node returns a response, it implicitly updates the state variable, appending the

71
00:04:34,119 --> 00:04:35,920
AIMessage.

72
00:04:35,920 --> 00:04:38,980
LangGraph handles this with an optimized merging mechanism.

73
00:04:38,980 --> 00:04:45,059
The reflection_node function improves the AI generated response from generation_node.

74
00:04:45,059 --> 00:04:49,739
It acts as a critique mechanism, analyzing conversation context and returning feedback

75
00:04:49,739 --> 00:04:51,260
to refine the output.

76
00:04:51,260 --> 00:04:57,579
It takes in messages, a sequence of BaseMessage objects, and passes them into the reflect_chain.

77
00:04:57,579 --> 00:05:01,299
The chain returns a thoughtful critique of the last AI output.

78
00:05:01,299 --> 00:05:05,459
This critique is wrapped in a HumanMessage because generation_node expects

79
00:05:05,459 --> 00:05:07,220
human input;

80
00:05:07,220 --> 00:05:10,540
returning an AIMessage would break the feedback loop.

81
00:05:10,540 --> 00:05:16,700
By using HumanMessage, the reflection agent speaks to the generation node as a user, requesting

82
00:05:16,700 --> 00:05:19,260
refinement of the existing output.

83
00:05:19,260 --> 00:05:24,700
Next, add the generate node to the graph using the add_node method, providing

84
00:05:24,700 --> 00:05:30,660
a unique name, generate, and the function to execute generation_node.

85
00:05:30,660 --> 00:05:33,019
Add the reflect node in the same way.

86
00:05:33,339 --> 00:05:38,700
Next, define the flow of execution between nodes using the add_edge function,

87
00:05:38,700 --> 00:05:43,019
which creates a one-way connection from reflection back to generation.

88
00:05:43,019 --> 00:05:47,779
Set the workflow's entry point using set_entry_point, specifying

89
00:05:47,779 --> 00:05:49,760
the generate node.

90
00:05:49,760 --> 00:05:54,299
This starts with the initial response based on user input or history.

91
00:05:54,299 --> 00:05:57,299
Add a router node to control the graph's flow.

92
00:05:57,299 --> 00:06:00,779
Import the end node, which marks workflow termination.

93
00:06:00,779 --> 00:06:04,480
The should_continue function checks message history.

94
00:06:04,480 --> 00:06:07,459
If messages exceed 6, it ends the workflow.

95
00:06:07,459 --> 00:06:10,019
Otherwise, it routes to reflect.

96
00:06:10,019 --> 00:06:13,519
More advanced setups can use the LLM for decisions.

97
00:06:13,519 --> 00:06:18,500
The add_conditional_edges method links generate to reflect or

98
00:06:18,500 --> 00:06:22,500
end based on should_continues output.

99
00:06:22,500 --> 00:06:26,579
The last step is compiling the workflow using the compile function.

100
00:06:26,579 --> 00:06:30,700
To test, define the initial user input with a HumanMessage.

101
00:06:30,700 --> 00:06:36,660
Write a LinkedIn post on getting a software developer job at IBM under 160 characters.

102
00:06:36,660 --> 00:06:39,140
This message kicks off the generation phase.

103
00:06:39,140 --> 00:06:43,859
Finally, run the workflow using the invoke function, triggering the full loop until a

104
00:06:43,859 --> 00:06:45,619
final output.

105
00:06:45,619 --> 00:06:48,299
Here is an example of the reflection agent in action.

106
00:06:48,299 --> 00:06:52,059
When you invoke the workflow, the response is a list of messages.

107
00:06:52,059 --> 00:06:56,899
The input HumanMessage goes to the generation node, producing a first draft of the LinkedIn

108
00:06:56,899 --> 00:06:58,279
post.

109
00:06:58,279 --> 00:07:03,200
This AIMessage and original prompt pass to the reflection node, which critiques and suggests

110
00:07:03,200 --> 00:07:06,380
improvements, wrapped as a HumanMessage.

111
00:07:06,380 --> 00:07:12,119
That feedback routes back to the generation node, producing a revised post AIMessage.

112
00:07:12,119 --> 00:07:18,100
This process repeats, adding critiques and responses to the state, continuously improving.

113
00:07:18,100 --> 00:07:21,239
The final AIMessage is your refined post.

114
00:07:21,239 --> 00:07:26,440
In this video, you learned that reflection agents iteratively improve AI outputs by critically

115
00:07:26,440 --> 00:07:29,440
analyzing their performance through a feedback loop.

116
00:07:29,440 --> 00:07:34,119
The generator produces content, while the reflector provides critical feedback.

117
00:07:34,119 --> 00:07:38,959
Prompt Engineering with LengChain guides LLMs in content generation and structure reflection

118
00:07:38,959 --> 00:07:43,440
via dynamic chat prompt templates and message placeholders.

119
00:07:43,440 --> 00:07:46,739
Agent state in LengGraph is defined via MessageGraph.

120
00:07:46,739 --> 00:07:52,339
It tracks conversation, accumulating messages, and context across iterations.

121
00:07:52,339 --> 00:07:56,579
Graph construction involves defining nodes, connecting them with edges, setting an entry

122
00:07:56,579 --> 00:08:01,299
point, and using router nodes for dynamic decision-making and iterative loops.