Welcome to this video on The Art of AI Self-Improvement: Building Reflection Agents.
In this video, you'll explore the concept of basic reflection agents, understanding
how they iteratively improve AI outputs and how to set up generator and reflector roles
for self-correction.
You will then learn to apply both LengChain and LengGraph for agent development, ultimately
building and running a complete reflection agent that refines its own content.
Imagine an AI improving by learning from mistakes.
This is the core of reflection agents, designed to analyze performance and enhance strategies
critically.
Reflection agents primarily fall into three types, basic reflection agent, reflexion agent,
and language agent tree search, or LATS.
This video focuses on basic reflection agents.
Consider the user prompt, how do I look cool?
This activates two LLM roles.
The generator suggests an initial idea - "Wear a fedora."
The reflector evaluates and critiques it.
Fedoras are outdated and often stereotyped negatively.
This loop runs for a set number of steps, refining the response before returning the
final answer.
For the second iteration, the generator creates an improved response.
Wear well-fitted clothes, have good posture, and be confident.
Authenticity beats trying to look cool.
The reflector evaluates the improved response.
Good advice focused on authenticity rather than specific items.
Could add something about personal style.
The generator provides the final output.
Find clothes that match your personal style, maintain good posture, and be confident.
True coolness comes from authenticity, not following trends.
So a crisis has been averted.
No more fedora, just flawless vibes!
You will use reflection to build a LinkedIn post optimization agent.
The agent will generate a post, critique its own output, and refine content iteratively.
You will build a system with a post-generation phase, followed by a reflection, or AI review
phase.
This cycle ensures higher quality content.
Initially, the generator accesses the HumanMessage and produces an output.
The reflector uses both the generator's output and the original message.
Both the generator and the reflector accumulate access to previous outputs, building memory
over iterations.
Start by initializing the LLM agent that will generate and critique content.
In this example, you'll use IBM's Granite model via LangChain.
To guide the generator LLM, use LangChain's chat prompt template.
A SystemMessage defines the LLM's roles.
Message's placeholder serves as memory, maintaining user inputs for the generator.
Using the pipe operator, connect the structured prompt to the LLM, creating generate_chain.
Similarly, create a prompt for the reflection LLM where the assistant evaluates the generated
LinkedIn post.
Define a reflection prompt using chat prompt template which includes a SystemMessage that
frames the model as a professional LinkedIn content strategist.
Include a message's placeholder to inject the post to be critiqued.
Finally, chain the prompt to the LLM using pipe operator.
Use LangGraph to build a conversational workflow by defining an agent state, a structure that
tracks the evolving context.
LangGraph simplifies this with MessageGraph, a special graph type whose state holds only
an array of messages, such as HumanMessage, AIMessage, SystemMessage.
Think of MessageGraph as a specialized state graph that accumulates different message types.
Each user turn adds a HumanMessage followed by an AIMessage.
To build a generate node, import BaseMessage, HumanMessage, and AIMessage for message
types.
Import list and sequence for type-ins.
The tool takes and returns a list-like object of BaseMessages.
The generate node takes the state variable as input including the original HumanMessage,
make me look cool on LinkedIn.
The input HumanMessage is passed to the invoke function as a state.
The chain uses that context to generate a response, "Wear a fedora and sunglasses in
your profile pic."
Finally, the function returns the output, wrapped in an AIMessage, and ready for the
next stage in the workflow.
The state variable contains the input message as a list.
When the node returns a response, it implicitly updates the state variable, appending the
AIMessage.
LangGraph handles this with an optimized merging mechanism.
The reflection_node function improves the AI generated response from generation_node.
It acts as a critique mechanism, analyzing conversation context and returning feedback
to refine the output.
It takes in messages, a sequence of BaseMessage objects, and passes them into the reflect_chain.
The chain returns a thoughtful critique of the last AI output.
This critique is wrapped in a HumanMessage because generation_node expects
human input;
returning an AIMessage would break the feedback loop.
By using HumanMessage, the reflection agent speaks to the generation node as a user, requesting
refinement of the existing output.
Next, add the generate node to the graph using the add_node method, providing
a unique name, generate, and the function to execute generation_node.
Add the reflect node in the same way.
Next, define the flow of execution between nodes using the add_edge function,
which creates a one-way connection from reflection back to generation.
Set the workflow's entry point using set_entry_point, specifying
the generate node.
This starts with the initial response based on user input or history.
Add a router node to control the graph's flow.
Import the end node, which marks workflow termination.
The should_continue function checks message history.
If messages exceed 6, it ends the workflow.
Otherwise, it routes to reflect.
More advanced setups can use the LLM for decisions.
The add_conditional_edges method links generate to reflect or
end based on should_continues output.
The last step is compiling the workflow using the compile function.
To test, define the initial user input with a HumanMessage.
Write a LinkedIn post on getting a software developer job at IBM under 160 characters.
This message kicks off the generation phase.
Finally, run the workflow using the invoke function, triggering the full loop until a
final output.
Here is an example of the reflection agent in action.
When you invoke the workflow, the response is a list of messages.
The input HumanMessage goes to the generation node, producing a first draft of the LinkedIn
post.
This AIMessage and original prompt pass to the reflection node, which critiques and suggests
improvements, wrapped as a HumanMessage.
That feedback routes back to the generation node, producing a revised post AIMessage.
This process repeats, adding critiques and responses to the state, continuously improving.
The final AIMessage is your refined post.
In this video, you learned that reflection agents iteratively improve AI outputs by critically
analyzing their performance through a feedback loop.
The generator produces content, while the reflector provides critical feedback.
Prompt Engineering with LengChain guides LLMs in content generation and structure reflection
via dynamic chat prompt templates and message placeholders.
Agent state in LengGraph is defined via MessageGraph.
It tracks conversation, accumulating messages, and context across iterations.
Graph construction involves defining nodes, connecting them with edges, setting an entry
point, and using router nodes for dynamic decision-making and iterative loops.