A basic collaborative agent team will be constructed using the AutoGen framework. AutoGen provides flexible abstractions for building applications involving multiple agents that can converse and work together. The 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.Setting Up the Environment and LLM ConfigurationFirst, 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 }Defining the Agent RolesWe need three core components for our collaborative team:User Proxy Agent: Represents the human user, initiating the task and potentially providing feedback. It can also execute code if needed, though we won't focus on that capability here.Researcher Agent: An LLM-based assistant responsible for gathering information on the requested topic.Writer Agent: Another LLM-based assistant responsible for structuring the gathered information into a blog post outline.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 important 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, 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 important 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.Establishing the Communication Protocol: Group ChatAutoGen 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.digraph G { rankdir=LR; node [shape=box, style=rounded, fontname="sans-serif", color="#4263eb", fontcolor="#495057"]; edge [color="#adb5bd"]; UserProxy [label="User Proxy\n(Initiator)"]; Manager [label="Group Chat Manager\n(Orchestrator)", shape=ellipse, color="#1c7ed6"]; Researcher [label="Researcher Agent\n(LLM)", color="#1098ad"]; Writer [label="Writer Agent\n(LLM)", color="#0ca678"]; UserProxy -> Manager [label="Initiates Task"]; Manager -> Researcher [label="Asks to Research"]; Researcher -> Manager [label="Provides Findings"]; Manager -> Writer [label="Asks to Outline"]; Writer -> Manager [label="Provides Outline\n(ends with TERMINATE)"]; Manager -> UserProxy [label="Delivers Final Outline"]; }Basic interaction flow within the AutoGen GroupChat for the blog post outlining task.Initiating the CollaborationWith 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, )Running and ObservingExecuting this script will trigger the multi-agent interaction.The UserProxyAgent sends the task_prompt to the GroupChatManager.The Manager, based on the prompt and agent roles, likely selects the Researcher to speak first.The Researcher processes the request, performs its "research" (simulated by the LLM based on its internal knowledge), and posts its findings to the chat.The Manager receives the findings and, seeing the need for an outline, selects the Writer.The Writer takes the researcher's input, structures it according to its instructions, creates the outline, appends TERMINATE, and posts it to the chat.The Manager relays the Writer's final message to the UserProxyAgent.The 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.Extensions and MoreThis example provides a foundational pattern for collaborative MAS using AutoGen. For more complex scenarios relevant to expert-level development, consider:More Specialized Roles: Introduce agents for editing, fact-checking, or code generation if the task requires it.Sophisticated Speaker Selection: Implement custom speaker transition logic within the GroupChat instead of relying solely on the LLM-based manager.Tool Use within MAS: Equip agents (e.g., the Researcher) with tools (like a web search API) using AutoGen's function calling capabilities.Error Handling and Recovery: Implement strategies for agents to handle situations where another agent fails or provides incorrect information.State Management: For longer or more complex collaborations, integrate memory mechanisms (as discussed in Chapter 3) for each agent or a shared memory space.Alternative Frameworks: Explore frameworks like CrewAI or LangGraph, which offer different abstractions (e.g., role-based definitions, state graph execution) for multi-agent workflows.This hands-on exercise demonstrates the core mechanics of setting up and running a simple multi-agent system. Building on these concepts allows for the development of significantly more complex and capable applications where specialized LLM agents collaborate to achieve goals that a single agent cannot.