Let's translate the principles of multi-agent systems into practice. In this section, we will construct a basic collaborative agent team using the AutoGen framework. AutoGen provides flexible abstractions for building applications involving multiple agents that can converse and work together. Our goal is to create a system where one agent acts as a researcher and another as a writer, collaborating to produce an outline for a technical blog post based on a user's request.
This practical exercise assumes you have AutoGen installed (pip install pyautogen
) and have access to an LLM endpoint configured (e.g., through environment variables like OPENAI_API_KEY
or an OAI_CONFIG_LIST
JSON file). We will focus on setting up the agents, defining their roles, establishing communication via a group chat, and initiating the collaborative task.
First, ensure your environment is ready. Typically, AutoGen utilizes a configuration list to specify the LLM(s) to be used. You can define this using environment variables or load it from a JSON file. For example, an OAI_CONFIG_LIST
file might look like this:
[
{
"model": "gpt-4-turbo-preview",
"api_key": "YOUR_API_KEY"
}
]
In our Python script, we'll load this configuration:
import autogen
import os
# Load LLM configuration from environment variable or file
# Example assumes OAI_CONFIG_LIST environment variable is set,
# pointing to a JSON string or file path.
# Replace with your specific configuration method.
config_list = autogen.config_list_from_json(
env_or_file="OAI_CONFIG_LIST",
filter_dict={
"model": ["gpt-4-turbo-preview"] # Or your preferred model
}
)
# Configuration arguments for the LLM
llm_config = {
"timeout": 600,
"cache_seed": 42, # Use None for non-deterministic results
"config_list": config_list,
"temperature": 0, # Lower temperature for more deterministic output
}
We need three core components for our collaborative team:
Let's define these agents using AutoGen's UserProxyAgent
and AssistantAgent
:
# Define the User Proxy Agent
# Acts as the initiator and can execute code if needed (human_input_mode="TERMINATE" means it stops after one interaction)
user_proxy = autogen.UserProxyAgent(
name="User_Proxy",
system_message="A human user seeking a blog post outline.",
code_execution_config=False, # We don't need code execution for this example
human_input_mode="TERMINATE", # Stop after getting the final response
is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
)
# Define the Researcher Agent
researcher = autogen.AssistantAgent(
name="Researcher",
llm_config=llm_config,
system_message="You are an expert researcher. Your task is to find relevant information and key points about a given technical topic. Focus on accuracy and depth. Do not write the final outline, just provide the research findings."
)
# Define the Writer Agent
writer = autogen.AssistantAgent(
name="Writer",
llm_config=llm_config,
system_message="You are an expert technical writer. Your task is to take the research findings provided by the Researcher and structure them into a clear, logical blog post outline. Include sections like Introduction, Key Concepts, Practical Examples (if applicable), Conclusion. Ensure the structure flows well. Conclude your response with the word TERMINATE."
)
Note the distinct system_message
for each assistant agent. This is crucial for guiding their behavior and specialization within the team. The TERMINATE
keyword in the writer's final message and the is_termination_msg
check in the UserProxyAgent
help control the conversation flow.
AutoGen facilitates multi-agent conversations using GroupChat
. We add our defined agents to a group and instantiate a GroupChatManager
to orchestrate the interaction.
# Create the Group Chat
groupchat = autogen.GroupChat(
agents=[user_proxy, researcher, writer],
messages=[],
max_round=10 # Set a limit on the number of conversation rounds
)
# Create the Group Chat Manager
manager = autogen.GroupChatManager(
groupchat=groupchat,
llm_config=llm_config
)
The max_round
parameter prevents infinite loops and controls resource usage. The GroupChatManager
uses its own LLM configuration to decide which agent should speak next based on the conversation history, unless a specific speaker selection method is provided.
Basic interaction flow within the AutoGen GroupChat for the blog post outlining task.
With the agents and communication structure defined, the UserProxyAgent
can now initiate the task by sending the initial message to the GroupChatManager
.
# Define the task prompt
task_prompt = """
Please research the core concepts behind Retrieval-Augmented Generation (RAG)
in Large Language Models and create a blog post outline covering its
importance, components, typical workflow, and common challenges.
"""
# Initiate the chat
user_proxy.initiate_chat(
manager,
message=task_prompt,
)
Executing this script will trigger the multi-agent interaction.
UserProxyAgent
sends the task_prompt
to the GroupChatManager
.Manager
, based on the prompt and agent roles, likely selects the Researcher
to speak first.Researcher
processes the request, performs its "research" (simulated by the LLM based on its internal knowledge), and posts its findings to the chat.Manager
receives the findings and, seeing the need for an outline, selects the Writer
.Writer
takes the researcher's input, structures it according to its instructions, creates the outline, appends TERMINATE
, and posts it to the chat.Manager
relays the Writer
's final message to the UserProxyAgent
.UserProxyAgent
, seeing the TERMINATE
condition met in the message, concludes the chat.You will observe the conversation unfolding in your console output, showing the messages exchanged between the agents. The final output from the Writer
should be the requested blog post outline.
This example provides a foundational pattern for collaborative MAS using AutoGen. For more complex scenarios relevant to expert-level development, consider:
GroupChat
instead of relying solely on the LLM-based manager.This hands-on exercise demonstrates the core mechanics of setting up and running a simple multi-agent system. Building upon these concepts allows for the development of significantly more complex and capable applications where specialized LLM agents collaborate to achieve goals beyond the reach of a single agent.
© 2025 ApX Machine Learning