Self-Reflecting AI Agents Using LangChain
In our last article, we saw how an AI agent “thinks” before taking the next step. The thinking process continues till it decides it has completed the task and the flow of thinking stops.
In this article, we will look at enabling self-reflection inn AI Agents. We have given AI agents the power to “think” and “act”. Now, we will see how it can be made to self-reflect on its own action, and then decide on the next action.
Self-Reflection
To understand this better, let’s understand the concept of self-reflection a little more. As humans, when we self-reflect, we reflect on our action, critique it, learn from it, and then formulate our next step and action. We continue to self-reflect on the next actions till we feel we have accomplished the task as best as it can be done.
Thinking is a linear process, whereas self-reflection is a cyclical process. In thinking, we think and do an action, and think again and do the next action and so on. The next thought is not taken after reviewing the previous thought. But, in self-reflection, we think, do an action, think about the action we did, and use the feedback to think about the next action and this cycle of feedback continues till we decide that we have achieved the task to the best of our abilities.
Thinking and Self-Reflection
Thus, in order to get an AI agent to self-reflect, we need to create a cycle of input where the thought and action of the AI agent is fed back to the AI agent to be reflected upon, before generating the next thought and action.
This can be illustrated as below:
LangGraph
This is achieved in LangChain through LangGraph. LangGraph is a library for building stateful, multi-actor applications. A state can be a list of chat messages. And, the actor is an LLM chain. Thus, LangGraph has the ability to coordinate multiple LLMs across multiple steps of computations in a cyclical manner. A simple example is shown below with an LLM that acts as a generator and as a reflector, each connnected to a state represented by the messages.
The main use of LangGraph is for adding cycles to an LLM application. Cycles are important for agent-like behaviours, where you call an LLM in a loop, asking it what action to take next. And, that is precisely what is needed for self-reflection.
Creating a Self-Reflection Loop
Let’s use LangGraph to create a self-reflection loop which will loop 6 times before outputting the final response. This simple type of reflection can improve performance by giving the LLM multiple attempts at refining its output. The same LLM is used as a generator and a reflector by adopting a different persona while critiquing the output.
First, we need to install langgraph from the terminal.
pip install langgraph
Next, we import MessageGraph to create the cyclical loop, and END to end the cyclical loop.
from langgraph.graph import END, MessageGraph
We then create a self-reflection loop that will consist of the actors (llm chains) and states (list of messages). Each actor is called a node.
Generation Node
First, we create the generation_node. The generation_node invokes a generate chain comprising of an llm and a generation prompt. We will create the generate chain later.
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage
from typing import List, Sequence
async def generation_node(state: Sequence[BaseMessage]):
return await generate.ainvoke({"messages": state})
Reflection Node
Next, we create the reflection_node. The reflection_node invokes a reflect chain comprising of an llm and a reflection prompt. We will create the reflect chain later. It then returns the output of the reflect chain as a Human Message back to the generator. The generator will then respond as if the message from the reflection node is a user input.
async def reflection_node(messages: Sequence[BaseMessage]) -> List[BaseMessage]:
# Other messages we need to adjust
cls_map = {"ai": HumanMessage, "human": AIMessage}
# First message is the original user request. We hold it the same for all nodes
translated = [messages[0]] + [
cls_map[msg.type](content=msg.content) for msg in messages[1:]
]
res = await reflect.ainvoke({"messages": translated})
# We treat the output of this as human feedback for the generator
return HumanMessage(content=res.content)
Self-reflection Loop Graph
Now, we create a builder which is a MessageGraph from LangGraph to which we add the above two nodes, naming them as “reflect” and “generate”. The builder is essentially a self-reflection loop. We also set a entry point which will start the loop. For a self-reflection loop, the entry point will be set as “generator”.
builder = MessageGraph()
builder.add_node("generate",generation_node)
builder.add_node("reflect",reflection_node)
builder.set_entry_point("generate")
We define a function should_continue that will end the loop after it has run 6 times.
from typing import List, Sequence
def should_continue(state: List[BaseMessage]):
if len(state) > 6:
return END
return "reflect"
To create a condition when the loop stops, we create a conditional edge containing the “generate” node along with the should_continue function.
builder.add_conditional_edges("generate",should_continue)
Now, we create the loop by connecting the “reflect” with the “generate”.
builder.add_edge("reflect","generate")
Finally, we create a graph by compiling the builder.
graph = builder.compile()
So, all that we need to do now is to create the generate chain to be invoked by the “generator” and the reflect chain to be invoked by the “reflector”.
Generate Chain
The generate chain will generate the output for a task. So, we create a generation_prompt with the task in it.
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
generation_prompt = ChatPromptTemplate.from_messages(
[
(
"system", "You are an essay assistant tasked with writing excellent 5-paragraph essays."
"Generate the best essay possible for the user's request."
" If the user provides critique, respond with a revised version of your previous attempts.",
),
MessagesPlaceholder(variable_name="messages"),
]
)
We create an llm
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(api_key="<Your API Key here>")
We then create the generate chain by chaining the prompt with the llm. This will be invoked by the “generator” node and provide the tasks for the reflector.
generate = generation_prompt|llm
Reflect Chain
Next, we create the reflection_prompt for the reflect chain.
reflection_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
"You are a teacher grading an essay submission. Generate critique and recommendations for the user's submission."
" Provide detailed recommendations, including requests for length, depth, style, etc.",
),
MessagesPlaceholder(variable_name="messages"),
]
)
We then create the reflect chain by chaining the reflection_prompt with the llm. The reflect chain will be invoked by the “reflector” node.
reflect = reflection_prompt | llm
The reflect chain will review the output of the generator and provide suggestions to improve the output. This will be send as a Human Input to the generator, essentially making it work on its own input based on the suggestions given. That’s how self-reflection happens.
Invoking the Self-Reflecting AI Agent
We are done creating the self-reflecting AI agent. To test it, let’s insert a message into the graph to start the self-reflection loop and see the output.
async for event in graph.astream(
[
HumanMessage(content="Generate an essay on the topicality of The Little Prince and its message in modern life"
)
],
):
print(event)
print("________")
The output is as given below:
Generator Output 1:
{'generate': AIMessage(content='"The Little Prince" by Antoine de Saint-Exupéry continues to be a timeless classic that holds significant relevance in today\\'s fast-paced and materialistic world. The story follows a young prince who travels from planet to planet, meeting various characters and learning essential life lessons along the way. Through its whimsical narrative, the novella conveys profound messages about love, friendship, and the importance of seeing beyond the surface to discover the true essence of things.\\n\\nIn modern life, where technology and busyness often dominate our daily routines, the message of slowing down and cherishing the simple things in life resonates deeply. The Little Prince\\'s interactions with the fox, who imparts wisdom about the nature of relationships and the importance of investing time and care into them, serve as a poignant reminder of the value of meaningful connections in an increasingly disconnected world. In a society that prioritizes productivity and efficiency, the story encourages readers to pause, reflect, and appreciate the beauty of the present moment.\\n\\nFurthermore, The Little Prince\\'s encounters with characters like the vain rose and the businessman highlight the pitfalls of materialism and the emptiness of pursuing wealth and status at the expense of human connection and emotional fulfillment. In a culture that often equates success with possessions and external achievements, the novella\\'s emphasis on the intangible qualities of love, empathy, and understanding serves as a powerful counter-narrative that challenges prevailing societal norms.\\n\\nThe Little Prince\\'s journey to Earth and his interactions with the narrator also underscore the universal themes of loneliness, longing, and the search for meaning in a vast and sometimes indifferent world. As individuals grapple with feelings of isolation and existential questions in today\\'s complex and uncertain landscape, the novella\\'s exploration of these existential themes offers solace and a sense of shared humanity that transcends time and cultural boundaries.\\n\\nIn conclusion, "The Little Prince" continues to captivate readers of all ages with its enduring message of compassion, connection, and the pursuit of deeper truths in a world often preoccupied with superficiality and material gain. By embracing the wisdom imparted by the little prince and his companions, readers can cultivate a greater appreciation for the beauty and complexity of life, fostering a more compassionate and fulfilling existence in the process. As we navigate the challenges of modern life, the lessons of The Little Prince serve as a guiding light, reminding us of the importance of love, friendship, and authenticity in a world that often demands conformity and compromise.', response_metadata={'token_usage': {'completion_tokens': 492, 'prompt_tokens': 69, 'total_tokens': 561}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, id='run-306f954e-d4ed-4331-82ad-c4079b3032d8-0')}
Reflector Output 1:
{'reflect': HumanMessage(content='Your essay on the topicality of "The Little Prince" and its message in modern life is insightful and well-structured. You have effectively highlighted the timeless themes of the novella and their relevance in today\\'s society. However, here are some recommendations to further enhance your essay:\\n\\n1. **Depth and Analysis**: While you touch upon various themes such as the importance of meaningful connections, the pitfalls of materialism, and the search for meaning, consider delving deeper into each theme. Provide examples from the novella or real-life situations to illustrate how these themes manifest in modern society. Analyze the impact of these themes on individuals and communities today.\\n\\n2. **Engagement with Counterarguments**: To add depth to your analysis, consider addressing potential counterarguments. For instance, some might argue that the novella\\'s messages are idealistic and impractical in today\\'s fast-paced world. Acknowledge such perspectives and provide a nuanced response, perhaps by offering practical ways in which the lessons of "The Little Prince" can be applied in contemporary life.\\n\\n3. **Literary Analysis**: Incorporate a more detailed literary analysis of Saint-Exupéry\\'s writing style, symbolism, and use of allegory in conveying the novella\\'s messages. Explore how these literary techniques contribute to the enduring appeal of "The Little Prince" and its ability to resonate with readers across generations.\\n\\n4. **Personal Reflection**: Consider integrating personal reflections or anecdotes to connect the themes of the novella to your own experiences or observations. Sharing personal insights can enhance the authenticity and impact of your essay.\\n\\n5. **Conclusion**: While your conclusion effectively sums up the key points discussed in the essay, consider ending with a thought-provoking statement or a call to action that encourages readers to reflect on how they can apply the lessons of "The Little Prince" in their own lives.\\n\\n6. **Length**: Expand on each point by providing more detailed explanations and examples. Aim for a word count that allows for a thorough exploration of the themes and their relevance in modern life.\\n\\nBy incorporating these recommendations, you can further enrich your essay and offer a more comprehensive analysis of the enduring message of "The Little Prince" in today\\'s world.', id='2f512037-a482-487c-8236-ac3877413976')}
Generator Output 2:
{'generate': AIMessage(content='"The Little Prince" by Antoine de Saint-Exupéry remains a poignant and thought-provoking tale that continues to resonate with readers across generations, offering timeless lessons on love, friendship, and the complexities of human nature. In today\\'s fast-paced and materialistic society, the novella\\'s messages hold particular relevance, urging individuals to pause, reflect, and seek deeper connections amidst the distractions of modern life.\\n\\nOne of the central themes of "The Little Prince" is the importance of meaningful connections and relationships. Through the little prince\\'s interactions with characters like the fox, who imparts the wisdom of taming and cultivating relationships, readers are reminded of the value of investing time and care into building genuine connections. In a world where surface-level interactions often prevail, the novella serves as a gentle reminder to prioritize quality over quantity in our relationships, fostering deeper bonds and emotional fulfillment.\\n\\nMoreover, Saint-Exupéry\\'s exploration of the pitfalls of materialism and the emptiness of pursuing wealth and status at the expense of human connection remains strikingly relevant in today\\'s consumer-driven culture. The encounters with characters like the vain rose and the businessman serve as cautionary tales, highlighting the hollowness of prioritizing material possessions over the intangible but invaluable qualities of love, empathy, and understanding. In a society that often equates success with external achievements, the novella challenges readers to look beyond material wealth and status symbols to find true fulfillment in authentic relationships and emotional connections.\\n\\nThe search for meaning and a sense of belonging in a vast and indifferent world is another theme that resonates deeply in "The Little Prince." As individuals grapple with feelings of loneliness, isolation, and existential questions in today\\'s complex and fast-paced world, the novella\\'s exploration of these universal themes offers solace and a sense of shared humanity. The little prince\\'s journey to Earth and his interactions with the narrator underscore the common human experience of longing for connection, understanding, and a sense of purpose in a world that can often feel isolating and overwhelming.\\n\\nIn conclusion, "The Little Prince" continues to captivate readers with its enduring message of compassion, connection, and the pursuit of deeper truths in a world that can often seem shallow and superficial. By embracing the lessons of the novella and reflecting on its themes of love, friendship, and authenticity, readers can cultivate a greater appreciation for the beauty and complexity of life, fostering a more meaningful and fulfilling existence in the process. As we navigate the challenges of modern life, the wisdom of "The Little Prince" serves as a guiding light, reminding us to cherish the simple joys, prioritize genuine connections, and seek out the deeper truths that give meaning to our lives.', response_metadata={'token_usage': {'completion_tokens': 543, 'prompt_tokens': 1010, 'total_tokens': 1553}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, id='run-548df4b8-afcc-47ad-a588-157c3377801b-0')}
Reflector Output 2:
{'reflect': HumanMessage(content='Your essay on the topicality of "The Little Prince" and its message in modern life is well-crafted and effectively conveys the enduring relevance of the novella\\'s themes. Here are some suggestions for further enhancing your essay:\\n\\n1. **Elaboration and Examples**: While you discuss the themes of meaningful connections, materialism, and the search for meaning, consider providing more detailed examples or anecdotes from the novella to support your points. For instance, you could delve deeper into specific scenes or dialogues that exemplify these themes and analyze their significance in the context of modern society.\\n\\n2. **Contemporary Relevance**: To strengthen the connection between the novella and modern life, consider incorporating real-world examples or current events that illustrate how the themes of "The Little Prince" manifest in today\\'s society. This can help readers better understand the practical applications of the novella\\'s messages in the context of contemporary challenges and experiences.\\n\\n3. **Critical Analysis**: Offer a more critical analysis of the themes presented in "The Little Prince" and their implications for individuals and society. Consider exploring potential contradictions or complexities within the novella\\'s messages and discussing how they intersect with contemporary values, beliefs, and behaviors.\\n\\n4. **Cultural Context**: Discuss how cultural factors influence the interpretation and application of the novella\\'s themes in different societies. Consider examining how cultural norms, values, and traditions shape individuals\\' understanding of concepts such as love, friendship, and materialism, and how these factors impact the relevance of "The Little Prince" in diverse cultural contexts.\\n\\n5. **Language and Style**: Pay attention to the language and style of your essay to ensure clarity and coherence. Consider varying your sentence structure and incorporating transitions to improve the flow of your arguments. Additionally, aim to use descriptive language that engages the reader and evokes a deeper emotional response to the themes discussed.\\n\\n6. **Conclusion**: While your conclusion effectively summarizes the key points of your essay, you may consider ending with a compelling statement that reinforces the enduring significance of "The Little Prince" and encourages readers to reflect on how they can apply its lessons in their own lives.\\n\\nBy incorporating these recommendations, you can further enrich your essay and provide a more comprehensive analysis of the timeless messages of "The Little Prince" and their relevance in contemporary society.', id='80cf3efa-1a98-444f-a86e-03721d4c0ac6')}
Generator Output 3:
{'generate': AIMessage(content='"The Little Prince" by Antoine de Saint-Exupéry remains a timeless masterpiece that continues to offer profound insights into the complexities of human relationships and the search for meaning in a fast-paced and materialistic world. Through its whimsical narrative and allegorical storytelling, the novella imparts enduring lessons on love, friendship, and the importance of looking beyond the superficial to discover the true essence of life.\\n\\nOne of the central themes of "The Little Prince" is the significance of meaningful connections and relationships. The little prince\\'s interactions with the fox, who imparts the wisdom of taming and nurturing relationships, serve as a poignant reminder of the value of investing time and care into fostering genuine connections. For example, the fox\\'s advice to the little prince about seeing with the heart rather than the eyes underscores the importance of emotional depth and understanding in relationships, a message that resonates deeply in a society often preoccupied with appearances and instant gratification.\\n\\nIn today\\'s materialistic culture, where possessions and status symbols often take precedence over authentic human connections, the novella\\'s critique of materialism remains as relevant as ever. The encounters with characters like the vain rose and the businessman underscore the emptiness of pursuing wealth and possessions at the expense of emotional fulfillment and empathy. By highlighting the fleeting nature of material wealth and the lasting impact of love and compassion, "The Little Prince" challenges readers to reevaluate their priorities and seek value in the intangible qualities that enrich their lives.\\n\\nFurthermore, the novella\\'s exploration of the search for meaning and belonging in a vast and seemingly indifferent world strikes a universal chord in contemporary society. As individuals navigate feelings of isolation, existential questions, and the pursuit of purpose in a rapidly changing world, the little prince\\'s quest for understanding and connection resonates as a timeless reminder of the human experience. The story\\'s themes of loneliness, longing, and the universal need for companionship and purpose offer solace and reflection in a world that can often feel fragmented and chaotic.\\n\\nIn conclusion, "The Little Prince" endures as a literary classic that transcends time and culture, offering profound insights into the human condition and the enduring values of love, friendship, and authenticity. By embracing the lessons of the novella and reflecting on its timeless themes, readers can cultivate a deeper appreciation for the beauty and complexity of life, fostering meaningful connections and a sense of purpose in an increasingly fast-paced and materialistic world. As we navigate the challenges of modern life, the wisdom of "The Little Prince" serves as a guiding beacon, reminding us to cherish the simple joys, prioritize genuine relationships, and seek out the deeper truths that give meaning to our existence.', response_metadata={'token_usage': {'completion_tokens': 534, 'prompt_tokens': 2022, 'total_tokens': 2556}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, id='run-6ec5b8a3-0e60-4fda-b197-9096770a6cd6-0')}
Reflector Output 3:
{'reflect': HumanMessage(content='Your essay on the topicality of "The Little Prince" and its message in modern life is well-written and engaging. You have effectively explored the central themes of the novella and their relevance in today\\'s society. Here are some suggestions to further enhance your essay:\\n\\n1. **In-depth Analysis**: While you touch upon the themes of meaningful connections, materialism, and the search for meaning, consider delving deeper into each theme. Provide more specific examples from the novella to illustrate these concepts and analyze how they resonate with contemporary experiences. Offering a detailed analysis will enrich your essay and provide a deeper understanding of the novella\\'s enduring messages.\\n\\n2. **Comparative Analysis**: Consider comparing and contrasting the themes presented in "The Little Prince" with other literary works or cultural artifacts that address similar topics. Exploring how different narratives approach themes of love, friendship, and materialism can offer a broader perspective on the universality of these concepts and their interpretations across different contexts.\\n\\n3. **Interdisciplinary Insights**: Incorporate insights from other disciplines, such as psychology, sociology, or philosophy, to enrich your analysis of the novella\\'s themes. Drawing connections between the psychological aspects of relationships, societal influences on materialism, or existential theories on the search for meaning can provide a multidimensional perspective on the themes explored in "The Little Prince."\\n\\n4. **Personal Reflection**: Consider sharing personal reflections or anecdotes that relate to the themes of the novella. Connecting the lessons of "The Little Prince" to your own experiences or observations can add a personal touch to your essay and create a more intimate connection with the reader.\\n\\n5. **Language and Style**: Pay attention to the language used in your essay to ensure clarity and coherence. Consider varying your sentence structures, using descriptive language to evoke imagery, and incorporating literary devices to enhance the eloquence of your writing. Strive for a balance between academic analysis and vivid storytelling to captivate the reader\\'s attention.\\n\\n6. **Conclusion**: While your conclusion effectively summarizes the key points of your essay, consider ending with a thought-provoking statement or a call to action that prompts readers to reflect on the enduring lessons of "The Little Prince" and consider how they can apply them in their own lives.\\n\\nBy incorporating these recommendations, you can further elevate your essay and provide a comprehensive exploration of the timeless messages of "The Little Prince" and their significance in contemporary society.', id='96f11d9d-487f-425d-aa97-a97b4507cb5b')}
Generator Output 4:
{'generate': AIMessage(content='"The Little Prince" by Antoine de Saint-Exupéry stands as a literary gem that continues to offer profound insights into the complexities of human relationships and the pursuit of meaning in a modern, fast-paced world. Through its allegorical storytelling and timeless themes, the novella imparts enduring lessons on love, friendship, and the intrinsic value of looking beyond the surface to uncover the essence of life.\\n\\nCentral to the narrative of "The Little Prince" is the theme of meaningful connections and relationships. The interactions between the little prince and characters like the fox exemplify the importance of investing time and care into nurturing genuine connections. For instance, the fox\\'s wisdom about taming and bonding captures the essence of emotional depth and understanding in relationships, echoing a poignant message in a society often fixated on superficial interactions and fleeting connections.\\n\\nIn today\\'s materialistic culture, where the pursuit of wealth and possessions often overshadows the significance of human connection, the novella\\'s critique of materialism remains as pertinent as ever. The encounters with characters like the vain rose and the avaricious businessman serve as cautionary tales, underscoring the emptiness of prioritizing material wealth over empathy and emotional fulfillment. By shedding light on the transient nature of material possessions and the enduring impact of love and compassion, "The Little Prince" challenges readers to reassess their values and seek richness in the intangible qualities that enrich their lives.\\n\\nFurthermore, the novella\\'s exploration of existential themes such as loneliness, longing, and the quest for purpose resonates deeply in contemporary society. As individuals grapple with feelings of isolation and the search for meaning in a world marked by rapid change and uncertainty, the little prince\\'s journey serves as a universal reminder of the human experience. The story\\'s themes of connection, introspection, and the pursuit of authenticity provide solace and contemplation in a world often characterized by noise and distraction.\\n\\nIn conclusion, "The Little Prince" endures as a timeless masterpiece that transcends generations, offering profound insights into the human spirit and the enduring values of love, friendship, and authenticity. By internalizing the lessons of the novella and reflecting on its universal themes, readers can foster a deeper appreciation for the intricacies of life, nurturing genuine connections and seeking out the profound truths that give meaning to existence. As we navigate the complexities of modern life, the wisdom of "The Little Prince" serves as a guiding compass, prompting us to cherish the simple joys, prioritize authentic relationships, and embark on a journey of self-discovery and connection in a world that beckons for depth and sincerity.', response_metadata={'token_usage': {'completion_tokens': 518, 'prompt_tokens': 3045, 'total_tokens': 3563}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_3b956da36b', 'finish_reason': 'stop', 'logprobs': None}, id='run-1fbf3665-3f03-42a3-97d5-83385882680b-0')}
The generator output is reviewed by the reflector which gives suggestions and based on the suggestions, the generator improves the work. Congratulations! You have build a self-reflecting AI agent that reviews its own work and improves it.
Previous Articles:
Beginner’s Guide To Retrieval Chain From LangChain
Beginner’s Guide to Conversational Retrieval Chain From LangChain