Welcome to the fourth part of our Free AI Course on AutoGen series.
If you’ve been following along, you know we’ve covered a lot of ground with multi-agent systems and customer onboarding.
The previous article revolved around enhancing customer onboarding using multi-agent systems. Now, we’re diving into something super exciting: leveraging the agent reflection framework to create high-quality blog posts.
By the end of this article, you’ll be able to use nested chat patterns to build a sophisticated reflection process that is perfect for generating well-crafted content. Let’s make some magic happen!
What is the Agent Reflection Framework?
In this lesson, we’ll learn about the agent reflection framework and how to use it to create top-notch blog posts. We’ll build a system where a set of reviewer agents are nested within a critic agent as the inner monologue, reflecting on a blog post written by a writer agent.

Setting Up Your Environment
Importing the OpenAI API Key
First things first, let’s set up our environment. We’ll start by importing the necessary configurations, beginning with the OpenAI API key.
python
from autogen.utils import get_openai_api_key
api_key = get_openai_api_key()Defining LLM Configuration
Next, we’ll define the Large Language Model (LLM) configuration that our agents will use.
from autogen.configs import LLMConfig
llm_config = LLMConfig(
model_name='gpt-3.5-turbo',
api_key=api_key,
temperature=0.7,
max_tokens=150
)
Constructing the Writer Agent
Let’s say we want to write a concise but engaging blog post about “AI For Developers”, keeping it within 100 words. The first thing that comes to mind is to construct a writer agent to do the task.
from autogen.agents import ConversableAgent
writer_agent = ConversableAgent(
name='WriterAgent',
llm_config=llm_config,
human_input_mode='never'
)
writer_agent_message = """
Your name is WriterAgent. You are responsible for writing a concise and engaging blog post about aifordevelopers.io, and it should be within 100 words.
"""
writer_agent.set_system_message(writer_agent_message)Generating the Initial Blog Post
Now, let’s have the writer agent generate the first version of the blog post.
initial_blog_post = writer_agent.generate_reply(message="Write a blog post about aifordevelopers.io within 100 words.")
print(initial_blog_post)Title: "Unlocking AI Skills with aifordevelopers.io"
Unleash your potential in artificial intelligence with aifordevelopers.io! This innovative platform offers valuable resources, tutorials, and insights for both beginners and experienced developers diving into AI. Stay ahead in this rapidly evolving field with expert guidance on machine learning, deep learning, natural language processing, and more. From practical tips to hands-on projects, aifordevelopers.io equips you with the tools to thrive in the world of AI. Elevate your skills, explore new opportunities, and join a vibrant community passionate about AI. Let aifordevelopers.io be your gateway to mastering AI development!
Introducing the Critic Agent
To improve the blog post, we’ll use the reflection agentic design pattern. This involves creating a critic agent who will reflect on the writer’s work and provide feedback.
critic_agent = ConversableAgent(
name='CriticAgent',
llm_config=llm_config,
human_input_mode='never'
)
critic_agent_message = """
Your name is CriticAgent. You are responsible for reviewing the blog post written by WriterAgent and providing constructive feedback.
"""
critic_agent.set_system_message(critic_agent_message)Initiating the Reflection Process
With the writer and critic agents set up, let’s initiate a chat between them to improve the blog post.
# Writer generates the initial blog post
initial_blog_post = writer_agent.generate_reply(message="Write a blog post about aifordevelopers.io within 100 words.")
print(f"Initial Blog Post: {initial_blog_post}")
# Critic reviews the initial blog post
critic_feedback = critic_agent.generate_reply(message=f"Review this blog post: {initial_blog_post}")
print(f"Critic Feedback: {critic_feedback}")
# Writer revises the blog post based on the critic's feedback
revised_blog_post = writer_agent.generate_reply(message=f"Revise the blog post based on this feedback: {critic_feedback}")
print(f"Revised Blog Post: {revised_blog_post}") Critic (to Writer): Write a concise but engaging blogpost about aifordevelopers.io. Make sure the blogpost is within 100 words. -------------------------------------------------------------------------------- Writer (to Critic): Title: "Unlocking AI Skills with aifordevelopers.io" Unleash your potential in artificial intelligence with aifordevelopers.io! This innovative platform offers valuable resources, tutorials, and insights for both beginners and experienced developers diving into AI. Stay ahead in this rapidly evolving field with expert guidance on machine learning, deep learning, natural language processing, and more. From practical tips to hands-on projects, aifordevelopers.io equips you with the tools to thrive in the world of AI. Elevate your skills, explore new opportunities, and join a vibrant community passionate about AI. Let aifordevelopers.io be your gateway to mastering AI development! -------------------------------------------------------------------------------- Critic (to Writer): This blog post effectively conveys the key benefits of using aifordevelopers.io to enhance AI skills. The title is engaging and descriptive, capturing the essence of the platform. The content is informative and motivating, highlighting the resources and community support available on the website. Consider adding specific examples or testimonials to make the post more relatable and engaging. Additionally, including links to specific tutorials or success stories could further enhance the post and drive reader engagement. Great job overall, keep up the good work! -------------------------------------------------------------------------------- Writer (to Critic): Title: "Empower Your AI Journey with aifordevelopers.io" Dive into the world of artificial intelligence with aifordevelopers.io - your ultimate resource hub for all things AI! Whether you're a newbie or a seasoned pro, this platform offers tutorials, insights, and a supportive community to elevate your AI skills. Master machine learning, delve into deep learning, and unravel the mysteries of natural language processing. From practical advice to engaging projects, aifordevelopers.io is your launchpad to AI excellence. Join a passionate community, explore endless possibilities, and let aifordevelopers.io guide you to AI mastery. Your AI adventure starts here! --------------------------------------------------------------------------------Nested Chat for Detailed Reflection
To take the reflection process a step further, we’ll create a nested chat system with multiple reviewer agents focusing on SEO, legal compliance, and ethical considerations.
SEO Reviewer
seo_reviewer = ConversableAgent(
name='SEOReviewer',
llm_config=llm_config,
human_input_mode='never'
)
seo_reviewer_message = """
Your name is SEOReviewer. You are responsible for optimizing the blog post for search engines to ensure it ranks well and attracts organic traffic.
"""
seo_reviewer.set_system_message(seo_reviewer_message)Legal Reviewer
legal_reviewer = ConversableAgent(
name='LegalReviewer',
llm_config=llm_config,
human_input_mode='never'
)
legal_reviewer_message = """
Your name is LegalReviewer. You are responsible for ensuring the blog post is legally compliant.
"""
legal_reviewer.set_system_message(legal_reviewer_message)Ethics Reviewer
ethics_reviewer = ConversableAgent(
name='EthicsReviewer',
llm_config=llm_config,
human_input_mode='never'
)
ethics_reviewer_message = """
Your name is EthicsReviewer. You are responsible for ensuring the blog post is ethically sound and free from potential ethical issues.
"""
ethics_reviewer.set_system_message(ethics_reviewer_message)Aggregating Feedback
We also need a meta reviewer to aggregate all the feedback and provide final suggestions.
meta_reviewer = ConversableAgent(
name='MetaReviewer',
llm_config=llm_config,
human_input_mode='never'
)
meta_reviewer_message = """
Your name is MetaReviewer. You aggregate all the reviews from other reviewers and provide final suggestions.
"""
meta_reviewer.set_system_message(meta_reviewer_message)Registering the Nested Chat
We’ll now define and register the chats for the critic agent.
nested_chats = [
{"recipient": seo_reviewer, "summary_method": "reflection_with_llm", "summary_prompt": "Provide an SEO analysis of the blog post."},
{"recipient": legal_reviewer, "summary_method": "reflection_with_llm", "summary_prompt": "Review the blog post for legal compliance."},
{"recipient": ethics_reviewer, "summary_method": "reflection_with_llm", "summary_prompt": "Evaluate the blog post for ethical considerations."},
{"recipient": meta_reviewer, "summary_method": "reflection_with_llm", "summary_prompt": "Aggregate all reviews and provide final suggestions."}
]
critic_agent.register_nested_chat(nested_chats)Running the Reflection Process
Finally, let’s run the reflection process and see the results.
# Writer generates the initial blog post
initial_blog_post = writer_agent.generate_reply(message="Write a blog post about deeplearning.ai within 100 words.")
print(f"Initial Blog Post: {initial_blog_post}")
# Critic reviews the initial blog post with nested reviewers
critic_feedback = critic_agent.generate_reply(message=f"Review this blog post: {initial_blog_post}")
print(f"Critic Feedback: {critic_feedback}")
# Writer revises the blog post based on the aggregated feedback
final_blog_post = writer_agent.generate_reply(message=f"Revise the blog post based on this feedback: {critic_feedback}")
print(f"Final Blog Post: {final_blog_post}") Critic (to Writer):
Write a concise but engaging blogpost about
aifordevelopers.io. Make sure the blogpost is
within 100 words.
--------------------------------------------------------------------------------
Writer (to Critic):
Title: "Unlocking AI Skills with aifordevelopers.io"
Unleash your potential in artificial intelligence with aifordevelopers.io! This innovative platform offers valuable resources, tutorials, and insights for both beginners and experienced developers diving into AI. Stay ahead in this rapidly evolving field with expert guidance on machine learning, deep learning, natural language processing, and more. From practical tips to hands-on projects, aifordevelopers.io equips you with the tools to thrive in the world of AI. Elevate your skills, explore new opportunities, and join a vibrant community passionate about AI. Let aifordevelopers.io be your gateway to mastering AI development!
--------------------------------------------------------------------------------
********************************************************************************
Starting a new chat....
********************************************************************************
Critic (to SEO Reviewer):
Review the following content.
Title: "Unlocking AI Skills with aifordevelopers.io"
Unleash your potential in artificial intelligence with aifordevelopers.io! This innovative platform offers valuable resources, tutorials, and insights for both beginners and experienced developers diving into AI. Stay ahead in this rapidly evolving field with expert guidance on machine learning, deep learning, natural language processing, and more. From practical tips to hands-on projects, aifordevelopers.io equips you with the tools to thrive in the world of AI. Elevate your skills, explore new opportunities, and join a vibrant community passionate about AI. Let aifordevelopers.io be your gateway to mastering AI development!
--------------------------------------------------------------------------------
SEO Reviewer (to Critic):
As an SEO reviewer:
- Include relevant keywords in the title like "AI skills", "AI development", "AI resources" to improve searchability.
- Incorporate key phrases throughout the content such as "machine learning tutorials", "deep learning insights" to enhance visibility.
- Add meta tags with targeted keywords to optimize the page for search engines.
--------------------------------------------------------------------------------
********************************************************************************
Starting a new chat....
********************************************************************************
Critic (to Legal Reviewer):
Review the following content.
Title: "Unlocking AI Skills with aifordevelopers.io"
Unleash your potential in artificial intelligence with aifordevelopers.io! This innovative platform offers valuable resources, tutorials, and insights for both beginners and experienced developers diving into AI. Stay ahead in this rapidly evolving field with expert guidance on machine learning, deep learning, natural language processing, and more. From practical tips to hands-on projects, aifordevelopers.io equips you with the tools to thrive in the world of AI. Elevate your skills, explore new opportunities, and join a vibrant community passionate about AI. Let aifordevelopers.io be your gateway to mastering AI development!
Context:
{'Reviewer': 'SEO reviewer', 'Review': "- Include relevant keywords in the title like 'AI skills', 'AI development', 'AI resources' to improve searchability.\n- Incorporate key phrases throughout the content such as 'machine learning tutorials', 'deep learning insights' to enhance visibility.\n- Add meta tags with targeted keywords to optimize the page for search engines."}
--------------------------------------------------------------------------------
Legal Reviewer (to Critic):
As a Legal Reviewer:
1. Ensure there are no misleading claims or false promises regarding the benefits or outcomes of using aifordevelopers.io.
2. Confirm that all statements about the platform's capabilities and services are accurate and substantiated.
3. Verify that there are no copyright infringements or violations in the content's text or visuals.
--------------------------------------------------------------------------------
********************************************************************************
Starting a new chat....
********************************************************************************
Critic (to Ethics Reviewer):
Review the following content.
Title: "Unlocking AI Skills with aifordevelopers.io"
Unleash your potential in artificial intelligence with aifordevelopers.io! This innovative platform offers valuable resources, tutorials, and insights for both beginners and experienced developers diving into AI. Stay ahead in this rapidly evolving field with expert guidance on machine learning, deep learning, natural language processing, and more. From practical tips to hands-on projects, aifordevelopers.io equips you with the tools to thrive in the world of AI. Elevate your skills, explore new opportunities, and join a vibrant community passionate about AI. Let aifordevelopers.io be your gateway to mastering AI development!
Context:
{'Reviewer': 'SEO reviewer', 'Review': "- Include relevant keywords in the title like 'AI skills', 'AI development', 'AI resources' to improve searchability.\n- Incorporate key phrases throughout the content such as 'machine learning tutorials', 'deep learning insights' to enhance visibility.\n- Add meta tags with targeted keywords to optimize the page for search engines."}
{'Reviewer': 'SEO reviewer', 'Review': "- Include relevant keywords in the title like 'AI skills', 'AI development', 'AI resources' to improve searchability.\n- Incorporate key phrases throughout the content such as 'machine learning tutorials', 'deep learning insights' to enhance visibility.\n- Add meta tags with targeted keywords to optimize the page for search engines."}
--------------------------------------------------------------------------------
Ethics Reviewer (to Critic):
As an ethics reviewer:
- Ensure the promotional content accurately reflects the services provided by aifordevelopers.io, avoiding any exaggerated claims or misrepresentation of the platform's offerings.
- Confirm that the platform's resources are suitable for all skill levels, providing a fair and inclusive environment for individuals interested in artificial intelligence.
- Encourage transparency regarding any affiliations or sponsorships that may influence the content or recommendations provided on aifordevelopers.io.
--------------------------------------------------------------------------------
********************************************************************************
Starting a new chat....
********************************************************************************
Critic (to Meta Reviewer):
Aggregrate feedback from all reviewers and give final suggestions on the writing.
Context:
{'Reviewer': 'SEO reviewer', 'Review': "- Include relevant keywords in the title like 'AI skills', 'AI development', 'AI resources' to improve searchability.\n- Incorporate key phrases throughout the content such as 'machine learning tutorials', 'deep learning insights' to enhance visibility.\n- Add meta tags with targeted keywords to optimize the page for search engines."}
{'Reviewer': 'SEO reviewer', 'Review': "- Include relevant keywords in the title like 'AI skills', 'AI development', 'AI resources' to improve searchability.\n- Incorporate key phrases throughout the content such as 'machine learning tutorials', 'deep learning insights' to enhance visibility.\n- Add meta tags with targeted keywords to optimize the page for search engines."}
{'reviewer': 'Ethics reviewer', 'review': '- Ensure the promotional content accurately reflects the services provided by aifordevelopers.io, avoiding any exaggerated claims or misrepresentation of the platform\'s offerings.\n- Confirm that the platform\'s resources are suitable for all skill levels, providing a fair and inclusive environment for individuals interested in artificial intelligence.\n- Encourage transparency regarding any affiliations or sponsorships that may influence the content or recommendations provided on aifordevelopers.io.'}
--------------------------------------------------------------------------------
Meta Reviewer (to Critic):
Aggregating feedback from all reviewers, it is evident that there are some common recommendations for the writing:
1. **SEO Reviewers' Points:**
- Including relevant keywords like 'AI skills', 'AI development', 'AI resources' in the title and throughout the content.
- Incorporating key phrases such as 'machine learning tutorials', 'deep learning insights' for visibility.
- Adding meta tags with targeted keywords to optimize for search engines.
2. **Ethics Reviewer's Points:**
- Ensuring promotional content accurately reflects the services without exaggeration or misrepresentation.
- Confirming that resources cater to all skill levels to create an inclusive environment.
- Encouraging transparency regarding any affiliations or sponsorships influencing the content.
Final Suggestions:
- The writing should strike a balance between SEO optimization and ethical considerations.
- Use relevant keywords naturally rather than forcing them into the content.
- Ensure transparency in all promotional material and information provided.
- Aim for inclusivity and accuracy in catering to individuals with varying levels of skill in AI.
By incorporating these suggestions, the content can be both search engine friendly and ethically sound.
--------------------------------------------------------------------------------
Critic (to Writer):
Aggregating feedback from all reviewers, it is evident that there are some common recommendations for the writing:
1. **SEO Reviewers' Points:**
- Including relevant keywords like 'AI skills', 'AI development', 'AI resources' in the title and throughout the content.
- Incorporating key phrases such as 'machine learning tutorials', 'deep learning insights' for visibility.
- Adding meta tags with targeted keywords to optimize for search engines.
2. **Ethics Reviewer's Points:**
- Ensuring promotional content accurately reflects the services without exaggeration or misrepresentation.
- Confirming that resources cater to all skill levels to create an inclusive environment.
- Encouraging transparency regarding any affiliations or sponsorships influencing the content.
Final Suggestions:
- The writing should strike a balance between SEO optimization and ethical considerations.
- Use relevant keywords naturally rather than forcing them into the content.
- Ensure transparency in all promotional material and information provided.
- Aim for inclusivity and accuracy in catering to individuals with varying levels of skill in AI.
By incorporating these suggestions, the content can be both search engine friendly and ethically sound.
--------------------------------------------------------------------------------
Writer (to Critic):
Title: "Master AI Development with aifordevelopers.io"
Dive into AI development with aifordevelopers.io! Our platform offers machine learning tutorials, deep learning insights, and valuable resources for all skill levels. Stay ahead in AI development with expert guidance on natural language processing and more. Join a vibrant community passionate about AI and unlock your potential in this rapidly evolving field. Our platform equips you with the tools to thrive in AI. Transparency is key - aifordevelopers.io is committed to providing accurate and inclusive resources without exaggeration. Let aifordevelopers.io be your gateway to mastering AI development!Conclusion
In this lesson, we’ve learned how to leverage the reflection agentic design pattern with nested chat. This approach allows for a comprehensive review process, ensuring high-quality content. Feel free to experiment with your own tasks and see how you can improve your content with the power of reflection.
In our next adventure, we’ll take these reflection techniques to the next level by integrating external APIs and enhancing agents with machine-learning capabilities. This will enable us to create even more sophisticated AI-driven applications, such as a conversational chess game between two agent players who can both call a tool and make legal moves on the chessboard.
Discover more from AI For Developers
Subscribe to get the latest posts sent to your email.