Reflection¶
In the context of LLM agent building, reflection refers to the process of prompting an LLM to observe its past steps (along with potential observations from tools/the environment) to assess the quality of the chosen actions. This is then used downstream for things like re-planning, search, or evaluation.
This notebook demonstrates a very simple form of reflection in LangGraph.
Prerequisites¶
We will be using a basic agent with a search tool here.
import "dotenv/config";
Generate¶
For our example, we will create a "5 paragraph essay" generator. First, create the generator:
import { ChatFireworks } from "@langchain/community/chat_models/fireworks";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
const prompt = ChatPromptTemplate.fromMessages([
[
"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.`,
],
new MessagesPlaceholder("messages"),
]);
const llm = new ChatFireworks({
model: "accounts/fireworks/models/firefunction-v2",
temperature: 0,
modelKwargs: {
max_tokens: 32768,
},
});
const essayGenerationChain = prompt.pipe(llm);
import { AIMessage, BaseMessage, HumanMessage } from "@langchain/core/messages";
let essay = "";
const request = new HumanMessage({
content:
"Write an essay on why the little prince is relevant in modern childhood",
});
for await (
const chunk of await essayGenerationChain.stream({
messages: [request],
})
) {
console.log(chunk.content);
essay += chunk.content;
}
The Little Prince, a novella written by Antoine de Saint-Ex upéry in 1943 , has been a beloved classic for generations of children and adults alike. Despite being written over 80 years ago, the story remains remarkably relevant to modern childhood. This essay will explore the ways in which The Little Prince continues to resonate with children today, highlighting its timeless themes, rel atable characters, and poignant commentary on the human experience. One of the primary reasons The Little Prince remains relevant is its exploration of universal themes that transcend time and culture . The story delves into complex emotions such as loneliness, friendship, and the importance of human connection. These themes are just as relevant today as they were when the book was first published. Children today face many of the same challenges as the Little Prince, including feeling isolated and struggling to form meaningful relationships . The nov ella's exploration of these themes provides a rel atable and comforting narrative for young readers. The characters in The Little Prince are also remarkably rel atable to modern children. The Little Prince himself is a curious and adventurous young boy who embodies the sense of wonder and curiosity that defines childhood . His relationships with the various characters he encounters on his journey , including the fox and the rose, serve as powerful reminders of the importance of empathy , kindness, and understanding. These characters, along with the Little Prince, provide a diverse and inclusive cast that reflects the complexity of modern childhood. Furthermore , The Little Prince offers a poignant commentary on the human experience that is just as relevant today as it was when the book was first published. The novella 's exploration of the adult world, with its emphasis on material possessions and superficial relationships, serves as a powerful critique of modern society. The Little Prince 's observations on the adult world, including his famous line "You see, grown -ups never understand anything by themselves , and it is exhausting for children to be always and forever explaining things to them," remain a powerful commentary on the challenges of growing up and navigating the complexities of adulthood . In conclusion , The Little Prince remains a remarkably relevant and powerful work of children's literature . Its exploration of universal themes , relatable characters, and poignant commentary on the human experience make it a must-read for children today. As a work of literature, it continues to inspire and comfort young readers, providing a powerful reminder of the importance of empathy , kindness, and understanding . As a cultural artifact, it serves as a powerful commentary on the challenges of growing up and navigating the complexities of modern society.
Reflect¶
const reflectionPrompt = ChatPromptTemplate.fromMessages([
[
"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.`,
],
new MessagesPlaceholder("messages"),
]);
const reflect = reflectionPrompt.pipe(llm);
let reflection = "";
for await (
const chunk of await reflect.stream({
messages: [request, new HumanMessage({ content: essay })],
})
) {
console.log(chunk.content);
reflection += chunk.content;
}
The essay provides a good overview of the relevance of The Little Prince in modern childhood . However, there are some areas that need improvement. Firstly , the introduction could be stronger . Instead of simply stating that the book is a classic, try to provide a more nuanced explanation of its enduring popularity. Additionally, the body paragraphs could be more detailed and provide more specific examples from the text to support the arguments . In terms of style, the writing is clear and concise , but could benefit from more varied sentence structures and vocabulary. The use of transitions between paragraphs could also be improved to create a more cohesive flow. One area that could be explored further is the way in which The Little Prince reflects the experiences of modern children . While the essay touches on this, it could be developed more fully to provide a more nuanced understanding of how the book continues to resonate with young readers. Overall , the essay provides a good foundation for exploring the relevance of The Little Prince in modern childhood . With some revisions to address the areas mentioned above, it could be even stronger. Grade : B + Recommendations : * Rev ise the introduction to provide a more nuanced explanation of the book 's enduring popularity . * Provide more specific examples from the text to support the arguments in the body paragraphs . * V ary sentence structures and vocabulary to create a more engaging writing style . * Explore the way in which The Little Prince reflects the experiences of modern children in more detail. * Improve transitions between paragraphs to create a more cohesive flow. Length : The essay is a good length, but could be expanded to provide more detail and examples . Depth: The essay provides a good overview of the relevance of The Little Prince, but could delve deeper into the themes and characters to provide a more nuanced understanding. Style: The writing is clear and concise , but could benefit from more varied sentence structures and vocabulary . Overall , the essay provides a good foundation for exploring the relevance of The Little Prince in modern childhood . With some revisions to address the areas mentioned above, it could be even stronger.
Repeat¶
And... that's all there is too it! You can repeat in a loop for a fixed number of steps, or use an LLM (or other check) to decide when the finished product is good enough.
let stream = await essayGenerationChain.stream({
messages: [
request,
new AIMessage({ content: essay }),
new HumanMessage({ content: reflection }),
],
});
for await (const chunk of stream) {
console.log(chunk.content);
}
Here is a revised version of the essay that addresses the areas mentioned above: The Little Prince , a nov ella written by Antoine de Saint -Exup éry in 1943, has captivated the hearts of readers of all ages with its poignant and timeless tale of friendship , love, and the human experience. One of the primary reasons for its enduring popularity is its ability to tap into the universal emotions and experiences that define childhood . The story 's exploration of complex themes such as loneliness, friendship, and the importance of human connection reson ates deeply with children today, who face many of the same challenges as the Little Prince. One of the most relatable aspects of The Little Prince is its exploration of the complexities of human relationships . The Little Prince's journey to different planets , where he encounters various strange characters, serves as a powerful metaphor for the challenges of forming meaningful connections with others . For example , his relationship with the fox , who teaches him the importance of human connection and the value of friendship, is a powerful reminder of the importance of empathy and understanding in building strong relationships . This theme is particularly relevant in modern childhood , where children are often encouraged to focus on individual achievement and success, rather than building strong relationships with others . Furthermore, The Little Prince offers a powerful commentary on the adult world, which is just as relevant today as it was when the book was first published . The nov ella's exploration of the superficial ity of adult relationships, where people are often more concerned with material possessions and appearances than with genuine human connection , serves as a powerful critique of modern society . The Little Prince's observations on the adult world, including his famous line "You see , grown-ups never understand anything by themselves, and it is exhausting for children to be always and forever explaining things to them ," remain a powerful commentary on the challenges of growing up and navigating the complexities of adulthood. In addition to its exploration of universal themes and relatable characters , The Little Prince also reflects the experiences of modern children in a number of ways. For example, the Little Prince's sense of wonder and curiosity, as well as his desire for adventure and exploration , are all qualities that are highly valued in modern childhood. Furthermore, the novella's exploration of the importance of human connection and empathy is particularly relevant in modern childhood , where children are often encouraged to focus on individual achievement and success, rather than building strong relationships with others. In conclusion, The Little Prince remains a remarkably relevant and powerful work of children's literature . Its exploration of universal themes , relatable characters, and poignant commentary on the human experience make it a must-read for children today. As a work of literature , it continues to inspire and comfort young readers, providing a powerful reminder of the importance of empathy, kindness, and understanding. As a cultural artifact , it serves as a powerful commentary on the challenges of growing up and navigating the complexities of modern society.
Define graph¶
Now that we've shown each step in isolation, we can wire it up in a graph.
import { END, MemorySaver, StateGraph, START, Annotation } from "@langchain/langgraph";
// Define the top-level State interface
const State = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: (x, y) => x.concat(y),
})
})
const generationNode = async (state: typeof State.State) => {
const { messages } = state;
return {
messages: [await essayGenerationChain.invoke({ messages })],
};
};
const reflectionNode = async (state: typeof State.State) => {
const { messages } = state;
// Other messages we need to adjust
const clsMap: { [key: string]: new (content: string) => BaseMessage } = {
ai: HumanMessage,
human: AIMessage,
};
// First message is the original user request. We hold it the same for all nodes
const translated = [
messages[0],
...messages
.slice(1)
.map((msg) => new clsMap[msg._getType()](msg.content.toString())),
];
const res = await reflect.invoke({ messages: translated });
// We treat the output of this as human feedback for the generator
return {
messages: [new HumanMessage({ content: res.content })],
};
};
// Define the graph
const workflow = new StateGraph(State)
.addNode("generate", generationNode)
.addNode("reflect", reflectionNode)
.addEdge(START, "generate");
const shouldContinue = (state: typeof State.State) => {
const { messages } = state;
if (messages.length > 6) {
// End state after 3 iterations
return END;
}
return "reflect";
};
workflow
.addConditionalEdges("generate", shouldContinue)
.addEdge("reflect", "generate");
const app = workflow.compile({ checkpointer: new MemorySaver() });
const checkpointConfig = { configurable: { thread_id: "my-thread" } };
stream = await app.stream(
{
messages: [
new HumanMessage({
content:
"Generate an essay on the topicality of The Little Prince and its message in modern life",
}),
]
},
checkpointConfig,
);
for await (const event of stream) {
for (const [key, _value] of Object.entries(event)) {
console.log(`Event: ${key}`);
// Uncomment to see the result of each step.
// console.log(value.map((msg) => msg.content).join("\n"));
console.log("\n------\n");
}
}
Event: generate ------ Event: reflect ------ Event: generate ------ Event: reflect ------ Event: generate ------ Event: reflect ------ Event: generate ------
const snapshot = await app.getState(checkpointConfig);
console.log(
snapshot.values.messages
.map((msg: BaseMessage) => msg.content)
.join("\n\n\n------------------\n\n\n"),
);
Generate an essay on the topicality of The Little Prince and its message in modern life ------------------ The Little Prince, a novella written by Antoine de Saint-Exupéry in 1943, has remained a timeless classic, captivating readers of all ages with its poignant and thought-provoking themes. Despite being written over seven decades ago, the story's message continues to resonate with modern society, making it a topical and relevant work of literature. One of the primary reasons for The Little Prince's enduring popularity is its exploration of the human condition. The novella delves into the complexities of adult relationships, highlighting the superficiality and materialism that often characterize them. The Little Prince's encounters with various strange characters on different planets serve as a commentary on the flaws of modern society, where people often prioritize wealth, power, and status over genuine human connections. This critique of modern society remains pertinent today, as people continue to struggle with the pressures of social media, consumerism, and the pursuit of material success. Furthermore, The Little Prince's emphasis on the importance of human relationships and emotional connections is a message that resonates deeply with modern audiences. In an era where technology has made it easier to connect with others, yet simultaneously created a sense of isolation and disconnection, the novella's themes of love, friendship, and empathy are more relevant than ever. The story encourages readers to reevaluate their priorities and focus on building meaningful relationships, rather than getting caught up in the superficialities of modern life. The Little Prince's exploration of the environment and our responsibility towards it is another aspect of the novella that remains topical today. The character's concern for the well-being of his own planet and his desire to protect it from harm serves as a powerful metaphor for the environmental crises facing our world. As we grapple with the consequences of climate change, deforestation, and pollution, the novella's message about the importance of preserving our planet's natural beauty and resources is more urgent than ever. In conclusion, The Little Prince's themes of human relationships, emotional connections, and environmental responsibility continue to resonate with modern audiences, making it a topical and relevant work of literature. As we navigate the complexities of modern life, the novella's message serves as a powerful reminder of the importance of prioritizing what truly matters: love, friendship, and the well-being of our planet. ------------------ The essay provides a good overview of the topicality of The Little Prince and its message in modern life. However, there are some areas that need improvement. Firstly, the introduction could be stronger. Instead of simply stating that the novella is a timeless classic, the writer could provide more context about its enduring popularity and why it remains relevant today. Secondly, the body paragraphs could be more detailed and nuanced. For example, the writer could provide more examples from the novella to support their arguments about the human condition, relationships, and the environment. Additionally, the writer could explore the implications of these themes in more depth, such as how they relate to contemporary issues like social media, consumerism, and climate change. Thirdly, the conclusion could be more concise and impactful. Instead of simply restating the main points, the writer could summarize the key takeaways and provide a final thought or call to action. In terms of style, the writing is clear and concise, but could benefit from more varied sentence structures and vocabulary. Additionally, the writer could use more transitions to connect the different paragraphs and ideas. Overall, the essay provides a good foundation for exploring the topicality of The Little Prince, but could benefit from more depth, nuance, and attention to style. Grade: B+ Recommendations: * Provide more context and background information in the introduction to set up the essay. * Use more specific examples and details from the novella to support arguments in the body paragraphs. * Explore the implications of the themes in more depth and relate them to contemporary issues. * Use more varied sentence structures and vocabulary to improve style. * Use transitions to connect the different paragraphs and ideas. * Summarize key takeaways and provide a final thought or call to action in the conclusion. Length: 500-750 words Depth: 7/10 Style: 6/10 Overall, the essay provides a good foundation for exploring the topicality of The Little Prince, but could benefit from more depth, nuance, and attention to style. ------------------ Here is a revised version of the essay that addresses the critique provided: The Little Prince, a novella written by Antoine de Saint-Exupéry in 1943, has captivated readers of all ages with its poignant and thought-provoking themes. Despite being written over seven decades ago, the story's message continues to resonate with modern society, making it a timeless classic that remains relevant today. One of the primary reasons for its enduring popularity is its ability to tap into the universal human experiences of love, friendship, and the search for meaning. As a result, the novella has become a cultural touchstone, with its themes and characters continuing to inspire and influence contemporary art, literature, and popular culture. One of the most striking aspects of The Little Prince is its exploration of the human condition. The novella delves into the complexities of adult relationships, highlighting the superficiality and materialism that often characterize them. For example, the character of the businessman, who is so consumed by his own importance that he fails to see the beauty of the stars, serves as a powerful commentary on the flaws of modern society. Similarly, the character of the king, who is so obsessed with his own power and authority that he neglects the needs of his subjects, highlights the dangers of unchecked ambition and the importance of empathy and compassion. These critiques of modern society remain pertinent today, as people continue to struggle with the pressures of social media, consumerism, and the pursuit of material success. Furthermore, The Little Prince's emphasis on the importance of human relationships and emotional connections is a message that resonates deeply with modern audiences. In an era where technology has made it easier to connect with others, yet simultaneously created a sense of isolation and disconnection, the novella's themes of love, friendship, and empathy are more relevant than ever. For example, the Little Prince's relationship with the rose, which is characterized by a deep sense of love and responsibility, serves as a powerful metaphor for the importance of nurturing and caring for others. Similarly, the character of the fox, who teaches the Little Prince about the importance of human connection and the value of relationships, highlights the need for empathy and understanding in our interactions with others. The Little Prince's exploration of the environment and our responsibility towards it is another aspect of the novella that remains topical today. The character's concern for the well-being of his own planet and his desire to protect it from harm serves as a powerful metaphor for the environmental crises facing our world. As we grapple with the consequences of climate change, deforestation, and pollution, the novella's message about the importance of preserving our planet's natural beauty and resources is more urgent than ever. For example, the character of the lamplighter, who is so consumed by his own routine that he fails to see the beauty of the stars, serves as a powerful commentary on the dangers of complacency and the importance of taking action to protect our planet. In conclusion, The Little Prince's themes of human relationships, emotional connections, and environmental responsibility continue to resonate with modern audiences, making it a timeless classic that remains relevant today. As we navigate the complexities of modern life, the novella's message serves as a powerful reminder of the importance of prioritizing what truly matters: love, friendship, and the well-being of our planet. Ultimately, The Little Prince encourages us to reevaluate our priorities and focus on building meaningful relationships, rather than getting caught up in the superficialities of modern life. By doing so, we can create a more compassionate, empathetic, and sustainable world for ourselves and for future generations. ------------------ The revised essay provides a more detailed and nuanced exploration of the topicality of The Little Prince and its message in modern life. The writer has addressed the critique provided by adding more specific examples and details from the novella to support their arguments, and by exploring the implications of the themes in more depth. One of the strengths of the revised essay is its ability to provide a more detailed and nuanced exploration of the human condition. The writer has done a good job of highlighting the complexities of adult relationships and the dangers of superficiality and materialism. The use of specific examples from the novella, such as the character of the businessman and the king, adds depth and nuance to the argument. The revised essay also does a good job of exploring the importance of human relationships and emotional connections in modern life. The writer has provided more specific examples from the novella, such as the Little Prince's relationship with the rose and the character of the fox, to support their argument. The use of these examples adds depth and nuance to the argument and helps to make it more relatable to modern audiences. The revised essay also does a good job of exploring the environmental themes in the novella and their relevance to modern life. The writer has provided more specific examples from the novella, such as the character of the lamplighter, to support their argument. The use of these examples adds depth and nuance to the argument and helps to make it more relatable to modern audiences. One area for improvement is the conclusion. While the writer has done a good job of summarizing the main points, the conclusion could be more concise and impactful. The writer could consider ending with a more thought-provoking question or a call to action to leave the reader with a lasting impression. Overall, the revised essay provides a more detailed and nuanced exploration of the topicality of The Little Prince and its message in modern life. The writer has done a good job of addressing the critique provided and has added more depth and nuance to the argument. Grade: A- Recommendations: * Consider ending the conclusion with a more thought-provoking question or a call to action to leave the reader with a lasting impression. * Use more varied sentence structures and vocabulary to improve style. * Consider adding more transitions to connect the different paragraphs and ideas. Length: 750-1000 words Depth: 9/10 Style: 8/10 ------------------ Here is a revised version of the essay that addresses the critique provided: The Little Prince, a novella written by Antoine de Saint-Exupéry in 1943, has captivated readers of all ages with its poignant and thought-provoking themes. Despite being written over seven decades ago, the story's message continues to resonate with modern society, making it a timeless classic that remains relevant today. One of the primary reasons for its enduring popularity is its ability to tap into the universal human experiences of love, friendship, and the search for meaning. As a result, the novella has become a cultural touchstone, with its themes and characters continuing to inspire and influence contemporary art, literature, and popular culture. One of the most striking aspects of The Little Prince is its exploration of the human condition. The novella delves into the complexities of adult relationships, highlighting the superficiality and materialism that often characterize them. For example, the character of the businessman, who is so consumed by his own importance that he fails to see the beauty of the stars, serves as a powerful commentary on the flaws of modern society. Similarly, the character of the king, who is so obsessed with his own power and authority that he neglects the needs of his subjects, highlights the dangers of unchecked ambition and the importance of empathy and compassion. These critiques of modern society remain pertinent today, as people continue to struggle with the pressures of social media, consumerism, and the pursuit of material success. Furthermore, The Little Prince's emphasis on the importance of human relationships and emotional connections is a message that resonates deeply with modern audiences. In an era where technology has made it easier to connect with others, yet simultaneously created a sense of isolation and disconnection, the novella's themes of love, friendship, and empathy are more relevant than ever. For example, the Little Prince's relationship with the rose, which is characterized by a deep sense of love and responsibility, serves as a powerful metaphor for the importance of nurturing and caring for others. Similarly, the character of the fox, who teaches the Little Prince about the importance of human connection and the value of relationships, highlights the need for empathy and understanding in our interactions with others. The Little Prince's exploration of the environment and our responsibility towards it is another aspect of the novella that remains topical today. The character's concern for the well-being of his own planet and his desire to protect it from harm serves as a powerful metaphor for the environmental crises facing our world. As we grapple with the consequences of climate change, deforestation, and pollution, the novella's message about the importance of preserving our planet's natural beauty and resources is more urgent than ever. For example, the character of the lamplighter, who is so consumed by his own routine that he fails to see the beauty of the stars, serves as a powerful commentary on the dangers of complacency and the importance of taking action to protect our planet. In conclusion, The Little Prince's themes of human relationships, emotional connections, and environmental responsibility continue to resonate with modern audiences, making it a timeless classic that remains relevant today. As we navigate the complexities of modern life, the novella's message serves as a powerful reminder of the importance of prioritizing what truly matters: love, friendship, and the well-being of our planet. Ultimately, The Little Prince encourages us to reevaluate our priorities and focus on building meaningful relationships, rather than getting caught up in the superficialities of modern life. By doing so, we can create a more compassionate, empathetic, and sustainable world for ourselves and for future generations. As we look to the future, we would do well to remember the Little Prince's wise words: "You become responsible, forever, for what you have tamed." ------------------ The revised essay provides a more detailed and nuanced exploration of the topicality of The Little Prince and its message in modern life. The writer has addressed the critique provided by adding more specific examples and details from the novella to support their arguments, and by exploring the implications of the themes in more depth. One of the strengths of the revised essay is its ability to provide a more detailed and nuanced exploration of the human condition. The writer has done a good job of highlighting the complexities of adult relationships and the dangers of superficiality and materialism. The use of specific examples from the novella, such as the character of the businessman and the king, adds depth and nuance to the argument. The revised essay also does a good job of exploring the importance of human relationships and emotional connections in modern life. The writer has provided more specific examples from the novella, such as the Little Prince's relationship with the rose and the character of the fox, to support their argument. The use of these examples adds depth and nuance to the argument and helps to make it more relatable to modern audiences. The revised essay also does a good job of exploring the environmental themes in the novella and their relevance to modern life. The writer has provided more specific examples from the novella, such as the character of the lamplighter, to support their argument. The use of these examples adds depth and nuance to the argument and helps to make it more relatable to modern audiences. The conclusion is also well-written and effectively summarizes the main points of the essay. The use of the Little Prince's quote at the end adds a nice touch and helps to drive home the importance of prioritizing what truly matters in life. Overall, the revised essay provides a more detailed and nuanced exploration of the topicality of The Little Prince and its message in modern life. The writer has done a good job of addressing the critique provided and has added more depth and nuance to the argument. Grade: A Recommendations: * None. The essay is well-written and effectively explores the topicality of The Little Prince and its message in modern life. Length: 750-1000 words Depth: 9/10 Style: 9/10 ------------------ I'm glad to hear that the revised essay meets your expectations. I believe that the essay provides a thorough and nuanced exploration of the topicality of The Little Prince and its message in modern life. The use of specific examples from the novella adds depth and nuance to the argument, and the conclusion effectively summarizes the main points of the essay. I'm also pleased to hear that the essay is well-written and effectively explores the themes of the human condition, human relationships, and environmental responsibility. The use of the Little Prince's quote at the end adds a nice touch and helps to drive home the importance of prioritizing what truly matters in life. Overall, I'm proud of the work that I've done on this essay, and I'm glad to hear that it meets your expectations. If you have any other requests or need further assistance, please don't hesitate to ask. Thank you for your feedback and guidance throughout this process. I appreciate your input and look forward to continuing to work with you in the future.