In this lesson, you'll learn about the agent reflection framework, and leverage its powered to create a high-quality blog post. You will learn how to use the nested chat conversation pattern to realize a sophisticated reflection process. You'll get to build a system where a set of reviewer agents is nested within a querying agent as in the monologue to reflect on a blog post written by a writer agent. Let's make the magic happen. In this lesson let's consider a blog post the writing task. Let's say we want to write a concise but engaging blog post about DeepLearning.AI I want to make sure the blog post is within 100 words. So with what we have learned in the first lesson, I guess the first thing that comes into your mind is to construct a writer agent to do the task, and that is true. So let's do that real quick. So here we could just import AutoGen and construct the model based assistant agent as the writer. Here, of course, you could just let the writer agent to do the writing by calling this generate reply function and provide the task as the input. And then you could check this result from this generate reply function. All right. Now we have a blog post about DeepLearning.AI. This is good, but we want to make it even better. One idea here is to use reflection, which is one of the notable and effective agentic design patterns. One way to realize reflection, is to involve another agent, to reflect on the work and help improve it. Following this idea, let's now create a critic agent to reflect on the work of the writer agent. So similarly, let's use the assistant agent class from AutoGen and create this large language model based agent. And here we can prompt this critic to investigate the writing of the writer and provide feedback. And with these two agents, we will be able to initiate a chat between these two agents to have a back-and-forth conversation between the critic and the writer. So here you can see that the writer gives a first version of the blog post, and then the critic provides some feedback, and then the writer gives another version of the blog post. From this result, we could see that this agent is reflecting on the work and making suggestions. And this is good, but the suggestions are still kind of general. In many cases, when they want to realize an even more complex reflection workflow as in the monologue of the critic agent. For example, when they want to ensure the critic agent to provide criticism regarding certain aspects of the work. For example, whether the content will rank well in search engines, whether it will attract organic traffic, whether it has legal problems and ethical problems, etc... Let's now see how we could use nested chat to handle all this. Nested chat is effectively a chat registered as the inner monologue of an agent. So let's first define the chat in this specific blog post writing task. Let's say we want to create a bunch of reviewers within the critic agent to carefully examine different aspects of the writing. So, the first agent we want to include is our SEO agent. This reviewer is responsible for optimizing the content for search engines to ensure that it ranks well and attracts organic traffic. And we realize that by setting the system message of this reviewer agent. And the second reviewer is a legal reviewer. We prompted it to be responsible for ensuring the content is legally compliant. The other reviewer agent we want to include is the ethics reviewer. This reviewer is responsible for ensuring the content is as ethically sound and free from any potential ethical issues. And know that in all these three reviewer agents, we are instructed to, concept suggestions, and we also want the reviewers to begin the review of the role, so that later we can further aggregate the reviews from these reviewers, to a final review. Finally mimicking how a real reviewing process works. We also want a meta reviewer to aggregate all the reviews and give final suggestion. To do that, we here, we create a meta reviewer, and the meta reviewer will aggregate all the reviews from the reviewers. The next step is to define the task to be registered. Here with further use the sequential chat conversation pattern learned in the last lesson to construct a series of chats between the critic and the reviewers. Here we are effectively having a list of four chats. Each one of them involve a particular reviewer as a recipient. Later we will register this chat list to the critic agent. So the critic agent will be used as a center by default, and we don't need to further specify the syntax here. And also for the first three chat sessions, we also use a large language model to do summary following the desired format. So that's why the summary method is said to be reflection with LLM and the summary, we provide a summary prompt so that, each of these reviewers could return the review in a json format, which includes a field of reviewer and a field of review. And in all of these chat sessions, we said to the max turns to be one. One other thing worth mentioning is that we need to set up the initial message properly, so that the nested reviewers can get to the content to be reviewed. One commonly used way to do that is to gather the content from the summary of the auto chat session. That's why we define, the initial message to be a function reflection message. So, this function's responsibility is to gather the summary from the agents in the auto chat session. And here we are calling the chat messages for summary as the initial message. Finally, we could register the review chat to the critic agent as a nested chat. And in this register nested chat function, we also set the trigger to be the writer. By doing so, whenever the critic agent receives a message from this writer agent, it will automatically roll to the message to this nested chat session for careful reflection. And after this registration step, we can now initiate the chat between the critic and the writer. And note that this step is basically the same as what we did when we do a reflection between the critic and the writer agent. And that's the difference, is that, this time we are registering this nested chat session on top of this critic, to do more careful reflection. And here we kick off the chat session. As we can see, the first step is for the writer to generate a first version of the blog post. So basically this is the same as we have seen before. And here, instead of directly giving feedback, the critic agent will route the message to the nested chat. Here, for example, you can see that we are starting a new chat between the critic agent and the SEO reviewer. And the SEO reviewer will investigate this content and the give suggestions on how to optimize the content for search engines, by incorporating relevant keywords such as AI courses, DeepLearning.AI Andrew Ng. And then we move on to the legal reviewer. And the legal reviewer will investigate, potential legal risks. For example, this legal reviewer is suggesting to review to investigate this title. And now we move on to the ethics reviewer. After investigating the blog post, the ethics reviewer investigate potential ethics issues and concludes that no modification or concern is raised regarding ethical implications. And finally, we move on to the meta reviewer. So basically the meta reviewer will investigate all the reviewers we received from the previous reviewers. And because we have, instructed the tabbed sessions to summarize, the chat, each of these chat in this nice json format. So we have these nice reviews and the reviewers. And finally, meta reviewer will give the final aggregated suggestion to the critic agent, to the writer agent, and after this writer agent receives, the suggestion from this critic agent, it will refund the blog post accordingly. So this is basically how we leverage nested chat to realize reflection, especially careful reflection, with a dedicated workflow. And now let's further check the result from this chat session here we are calling the summary, to check the result. And here you can see that it is returning the final, refined version of the blog post. Feel free to pause here and try your own task. For example, you can ask agent to write a blog post on topics that you are interested in. All right. In this lesson, we have learned how to realize the reflection agentic design pattern with nested chat. In the next lesson, we will learn how to further leverage through using in nested chat to further level up the agentic design patterns. Stay tuned.