[MUSIC]
Welcome to this video on understanding Reflexion agents.
In this video, you'll analyze how Reflexion agents improve AI responses
through iterative self-critique and tool use.
You'll apply generator and reflector roles within the Reflexion workflow,
integrate real-time information using external tools,
and structure outputs to include citations and references for greater transparency.
In this video on Reflexion agents with LangGraph, remember the difference isn't just the "X".
Reflexion agents build on the idea of reflection agents,
which iteratively review and refine outputs.
They go further by producing responses with citations, current information,
and verifiable claims rather than just improved opinions.
Here's a quick review of the reflection process.
A query like "I need more minerals in my diet" enters a cycle of generation and reflection.
The back-and-forth continues until a reasonable stopping point is reached.
Eventually, the system produces a response such as,
"To increase minerals in your diet, try eating foods like spinach (iron and magnesium),
almonds (magnesium), and dairy products (calcium)."
But what happens if new research comes out after the model is already trained?
This is where Reflexion comes in.
It allows the system to learn from new information, evaluate past responses,
and iteratively improve even post-training.
Here's why Reflexion is considered a powerful framework for building smarter agents.
At its core, Reflexion is designed to support self-improving agents.
These agents don't just reflect once.
They continually analyze their own performance,
learning and getting better with each iteration.
One of their key strengths is the ability to find and fix their own weaknesses.
After each run, the agent reflects on what went wrong
and adjusts its reasoning or strategy before trying again.
They also have the ability to incorporate external information.
By calling tools like web search or APIs,
Reflexion agents can bring in real-time data
to improve the relevance and accuracy of their next attempt.
And finally, Reflexion agents are able to support and justify their output.
Because of the reflection cycle, they can back up their responses with citations
or clearly explain the reasoning behind their answers.
Let's understand Reflexion, a method for improving LLM response.
Start with a query.
"I need more minerals in my diet."
A generator or responder LLM creates the initial response.
A system prompt sets the role.
For example, "You are a fitness coach".
Tell the LLM to critique its own output.
Provide a tool input, like a search query, to refine the response.
A structured system message guides the entire process.
To help the model distinguish between tool outputs and its own responses,
the output is clearly formatted.
Each part is labeled, including the response, critique, and query, to avoid ambiguity.
Instead of returning plain text,
the LLM outputs a structured object based on a defined schema or data model.
The user query is passed to the responder.
Instead of returning raw text, the LLM outputs a structured object.
This object follows a schema, which is represented as a table.
Each field, like response and query, maps to an attribute of the object.
This entire structure becomes an AI message.
The responder's output is passed to a search engine.
The tool extracts the search query from the responder's output.
Simultaneously, the HumanMessage and AIMessage from the responder
are saved to a list called response_list.
For each query, the tool will return information.
For example, it might include the title, content, and URL.
Here, one search result is shown, but this is a parameter you can decide.
You also append the tool call result to the response list.
The tool will pass this output to the revisor via the response list.
The revisor will use the response list, in particular the self-critique from the responder.
The revisor modifies the input from the responder using the tool outputs.
It then follows a set of instructions to revise the response,
incorporate citations from the tool, and add references for the citations.
Just like the generator, the revisor outputs a response.
For example, recommending mineral-rich food, but with added references.
Like the responder, it uses the same schema, represented as a table.
This includes the revised response, references,
self-critique, and the next set of search queries.
A key difference is that the response now includes refined references.
The revisor's output is passed to the tool, and the search queries are extracted.
The output is also stored in the tool response.
The tool response is processed by the revisor and added to the response list.
The response list also includes past revisor AI messages.
This process repeats itself iteratively.
The revisor passes its output back to the tool, which then updates the response.
This cycle continues for a predetermined number of iterations until you get an output.
In this video, you learned that:
Reflexion agents build on reflection agents by iteratively
improving responses using self-critiques, external tools, and citations.
The reflection process involves a loop of generation, critique,
and revision to enhance clarity, accuracy, and usefulness.
Reflexion agents can identify and fix their own weaknesses,
improving with each cycle by analyzing prior outputs.
They can incorporate real-time data by calling external tools
like web search APIs, enhancing the relevance of responses.
Structured schema-based output helps agents distinguish
between different components like response, critique, and tool query.
The responder produces an output with fields such as query and response,
which downstream components like the revisor can build on.
The revisor refines the response by revising it, integrating tool outputs,
and adding references to support the claims.
This entire process operates in an iterative cycle
with outputs and feedback passed through tools
and stored in a response list across runs.
[MUSIC]