Group Chat

AG2's group chat functionality provides the flexibility to orchestrate agents in a number of patterns, designed by you or available as presets.

The easiest pattern to get started with is one where a group manager agent automatically selects the agents to speak by evaluating the messages in the chat and the descriptions of the agents, this is known as the AutoPattern.

In the following example we will use the AutoPattern group chat to create lesson plans for our classroom.

Tip

Group chats afford you a great deal of flexibility and control, see the Group Chat documentation for a step-by-step walkthrough of the components of group chats.

from autogen import ConversableAgent, LLMConfig
from autogen.agentchat import initiate_group_chat
from autogen.agentchat.group.patterns import AutoPattern

llm_config = LLMConfig(api_type="openai", model="gpt-4o-mini")

# 1. Create our agents
planner_message = """You are a classroom lesson planner.
Given a topic, write a lesson plan for a fourth grade class.
If you are given revision feedback, update your lesson plan and record it.
Use the following format:
<title>Lesson plan title</title>
<learning_objectives>Key learning objectives</learning_objectives>
<script>How to introduce the topic to the kids</script>
"""

reviewer_message = """You are a classroom lesson reviewer.
You compare the lesson plan to the fourth grade curriculum
and provide a maximum of 3 recommended changes for each review.
Make sure you provide recommendations each time the plan is updated.
"""

teacher_message = """You are a classroom teacher.
You decide topics for lessons and work with a lesson planner.
and reviewer to create and finalise lesson plans.
"""

with llm_config:
    lesson_planner = ConversableAgent(
        name="planner_agent", system_message=planner_message
    )

    lesson_reviewer = ConversableAgent(
        name="reviewer_agent", system_message=reviewer_message
    )

    teacher = ConversableAgent(
        name="teacher_agent",
        system_message=teacher_message,
    )

# 2. Create our pattern
# In the AutoPattern we can nominate the LLM configuration for the group manager
agent_pattern = AutoPattern(
    initial_agent=teacher,
    agents=[lesson_planner, lesson_reviewer, teacher],
    group_manager_args={"llm_config": llm_config},
)

# 3. Initiate the group chat with our nominated pattern
result, _, _ = initiate_group_chat(
    pattern=agent_pattern,
    messages="Today, let's introduce our kids to the solar system.",
    max_rounds=10,
)