Site icon Rajeev Singh | Coder, Blogger, YouTuber

Unlock the Power of AutoGen Framework for Multi-Agent Systems

In my previous post, “Getting Started with AutoGen – A Framework for Building Multi-Agent,” I delved into the basics of the AutoGen Framework, providing step-by-step instructions and discussing how AutoGen can streamline the development of multi-agent systems. We also showcased a demo using AutoGen Studio.

In this new post, I will take a deeper dive into the AutoGen Framework, exploring its fundamentals, getting started with a “Hello World” example, and covering additional details. Finally, we will revisit the example used in AutoGen Studio for ideation and demonstrate how this concept can be further enhanced using the AutoGen Framework.

What is AutoGen Framework?

AutoGen is an open-source framework that leverages multiple agents to enable complex workflows.

 It provides a structured environment where various agents can interact, collaborate, and execute tasks in a coordinated manner.

This framework is particularly useful for building multi-agent systems that require sophisticated interactions and task management, making it an ideal choice for developers looking to streamline and enhance their workflow automation processes.

Why AutoGen Framework?

In my previous post, I explored AutoGen and discussed two approaches to develop solutions, 1. AutoGen Studio and 2. AutoGen Framework

We have seen how AutoGen Studio works and what is the purpose of this.

Lets recap AutoGen Studio and then understand why do we need AutoGen Framework.

AutoGen Studio

Purpose:

Features:

AutoGen Framework

Purpose:

Features:

Summary

Now, we can say that both tools complement each other, allowing users to start with AutoGen Studio for quick prototyping and then transition to the AutoGen Framework for more advanced development and deployment

Both AutoGen Studio and the Multi-agent Conversation Framework leverage the same core components (AssistantAgent and UserProxyAgent) to facilitate multi-agent interactions and task automation. The primary difference lies in the interface and additional features provided by AutoGen Studio, such as the low-code environment and deployment options.

Setting up AutoGen Framework:

Guide on AutoGen 0.4

AutoGen v0.4 introduces several significant improvements and changes based on feedback from the community and users.

Key Improvements in AutoGen v0.4

  1. Asynchronous, Event-Driven Architecture: AutoGen v0.4 adopts an asynchronous, event-driven architecture. This change addresses issues related to observability, flexibility, interactive control, and scalability. It allows for more efficient and responsive agent workflows.
  2. Layered API Structure:
    • Core API: The foundation layer of AutoGen v0.4, offering a scalable, event-driven actor framework for creating agentic workflows. This layer provides the essential building blocks for developing complex agent interactions.
    • AgentChat API: Built on top of the Core API, the AgentChat API offers a task-driven, high-level framework for building interactive agentic applications. It simplifies the process of creating and managing agent interactions.
  3. Enhanced Observability and Control: The new architecture improves observability, making it easier to monitor and debug agent workflows. It also provides more interactive control over agent behavior, allowing developers to fine-tune and optimize their applications.
  4. Scalability: The event-driven design enhances the scalability of AutoGen v0.4, making it suitable for both small startups and large enterprises. It can handle a higher volume of interactions and more complex workflows.

Differences from AutoGen v0.2

  1. Architecture: AutoGen v0.2 used a more synchronous and less flexible architecture, which could lead to limitations in scalability and control. AutoGen v0.4’s asynchronous, event-driven design addresses these limitations.
  2. API Structure: AutoGen v0.2 had a more monolithic API, whereas AutoGen v0.4 introduces a layered API structure with the Core API and AgentChat API. This separation allows for more modular and flexible development.
  3. Task-Driven Framework: The AgentChat API in AutoGen v0.4 provides a task-driven framework, making it easier to build interactive agentic applications. This high-level framework simplifies the development process compared to the more manual approach in AutoGen v0.2.
  4. Community Feedback: AutoGen v0.4 incorporates extensive feedback from the community and users, leading to improvements in usability, flexibility, and performance. These enhancements make it a more robust and user-friendly platform for building agentic applications.

Overall, AutoGen v0.4 represents a significant evolution of the platform, offering a more powerful and flexible framework for creating and managing agent interactions.

Installation

When installing AgentChat locally, it is recommend to use a virtual environment for the installation.

python3 -m venv .venv
source .venv/bin/activate

Install Using pip

Install the autogen-agentchat package using pip:

pip install -U "autogen-agentchat"

Install OpenAI for Model Client

To use the OpenAI and Azure OpenAI models, you need to install the following extensions:

pip install "autogen-ext[openai]"

Demo#1: HelloWorld using AssistantAgent

Overview

Via AgentChat, you can build applications quickly using preset agents. To illustrate this, I will begin with creating a single tool-use agent.

This code creates an assistant agent using the OpenAI GPT-4 model and instructs it to say, “Hello World!”. The result is printed on the console.

Below code demonstrates key components used by AutoGen.

Prerequisites:

To get started, you’ll need to set up an OpenAI account and generate an OpenAI Key.

Sign in here Overview – OpenAI API


import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient


api_key="<Enter your OPENAI key here>"

async def main() -> None:
    agent = AssistantAgent("assistant", OpenAIChatCompletionClient(model="gpt-4o",api_key=api_key))
    
    #print(await agent.run(task="Say 'Hello World!'"))

    result=await agent.run(task="Say 'Hello World!'")
    # Extract and print the assistant's response
    for message in result.messages:
        if message.source == 'assistant':
            print(message.content)
asyncio.run(main())

Output

Hello World!

Code Explanation

  1. Importing Modules
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

2. Setting the API Key:

 api_key="<Your_OPENAI_Key>"

           api_key: A variable that stores your OpenAI API key.

3. Defining the Main Asynchronous Function:

async def main() -> None:

       agent = AssistantAgent("assistant", OpenAIChatCompletionClient(model="gpt-4o", api_key=api_key))

       print(await agent.run(task="Say 'Hello World!'"))

main(): An asynchronous function that creates an instance of AssistantAgent using the OpenAIChatCompletionClient with the specified model and API key.

4. Running the Asynchronous Function:

   asyncio.run(main())

What’s Happening in the Background

  1. Initialization:
    • The OpenAIChatCompletionClient is initialized with the specified model (gpt-4o) and API key. This client will handle communication with the OpenAI API.
  2. Agent Creation:
    • An instance of AssistantAgent is created, which uses the OpenAIChatCompletionClient to process tasks.
  3. Running the Task:
    • The agent.run(task=”Say ‘Hello World!'”) method is called. This method sends the task to the OpenAI API via the OpenAIChatCompletionClient.
  4. API Interaction:
    • The OpenAIChatCompletionClient sends a request to the OpenAI API with the task “Say ‘Hello World!'”.
    • The OpenAI API processes the request and generates a response based on the GPT-4 model.
  5. Response Handling:
    • The response from the OpenAI API is received by the OpenAIChatCompletionClient.
    • The AssistantAgent processes the response and returns it.
  6. Output:
    • The result (“Hello World!”) is printed to the console.

This code demonstrates how to use the AutoGen framework to create an assistant agent that interacts with the OpenAI API to perform a simple task.

let’s move on to another example that utilizes a multi-agent system, specifically involving an AssistantAgent and a CodeExecutorAgent

Demo#2: Multi-agent system (AssistantAgent, CodeExecutorAgent)

Overview

This code sets up an asynchronous multi-agent system using the AutoGen framework.

It involves two main agents:

an AssistantAgent that generates Python code and

CodeExecutorAgent that executes the generated code.

The interaction between these agents is managed by a RoundRobinGroupChat, and the process continues until a termination condition is met.

Code:

import asyncio
from autogen_agentchat.agents import AssistantAgent, CodeExecutorAgent
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination, MaxMessageTermination
from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor
from autogen_ext.models.openai import OpenAIChatCompletionClient
import os
from dotenv import load_dotenv
from autogen_agentchat.ui import Console

async def main() -> None:
    load_dotenv()
    api_key = os.getenv("OPENAI_KEY")    
    #Smodel_client = OpenAIChatCompletionClient(model="gpt-4o", api_key=api_key, seed=42, temperature=0)
    model_client = OpenAIChatCompletionClient(model="gpt-4o", api_key=api_key)
    assistant = AssistantAgent(
        name="assistant",
        system_message="You are a helpful assistant. Write all code in python. Reply only 'TERMINATE' if the task is done.",
        model_client=model_client, )
    code_executor = CodeExecutorAgent(
        name="code_executor",
        code_executor=LocalCommandLineCodeExecutor(work_dir="coding"),  )

    # The termination condition is a combination of text termination and max message termination, either of which will cause the chat to terminate.
    termination = TextMentionTermination("TERMINATE") | MaxMessageTermination(10)

    # The group chat will alternate between the assistant and the code executor.
    group_chat = RoundRobinGroupChat([assistant, code_executor], termination_condition=termination)  

    while True:
        # Prompt the user for input
        user_task = input("Please enter your task (or type 'TERMINATE' to end): ")
        if user_task.strip().upper() == "TERMINATE":
            print("Terminating the session. Goodbye!")
            break
        # `run_stream` returns an async generator to stream the intermediate messages.
        stream = group_chat.run_stream(task=user_task)
        # `Console` is a simple UI to display the stream.
        await Console(stream)

asyncio.run(main())

Code Description (Components Used)

  1. Imports necessary modules and classes: This includes agents, termination conditions, and other utilities from the AutoGen framework.
  2. Loads environment variables: It uses a .env file to get the OpenAI API key.
  3. Initializes an OpenAIChatCompletionClient: This client uses the GPT-4 model for generating responses.
  4. Creates an AssistantAgent: This agent generates Python code based on user input.
  5. Creates a CodeExecutorAgent: This agent executes the generated Python code.
  6. Sets up termination conditions: The chat will terminate if either the text “TERMINATE” is mentioned or a maximum of 10 messages are exchanged.
  7. Alternates between the AssistantAgent and CodeExecutorAgent: This is managed by the RoundRobinGroupChat.
  8. Prompts the user for input tasks: The user can enter tasks, and the system will stream intermediate messages using a simple UI (Console) until the user types “TERMINATE” to end the session.
  9. Runs the main function asynchronously: This orchestrates the interaction between the user, AssistantAgent, and CodeExecutorAgent.

The main function ensures that the user can interact with the agents, and the agents can collaborate to generate and execute Python code based on the user’s tasks.

Output:

Want to explore the live demo, watch below.

Demonstrates how to create a chat between an assistant and a code executor agent using the autogen_agentchat library

What’s Happening in the Background

  1. User Input:
    • The user enters a question or task, such as “Write a Python script to call OpenAI from Python.”
  2. Task Initialization:
    • The main function initializes the model client, creates the agents, sets up the termination conditions, and creates the RoundRobinGroupChat.
  3. Task Execution:
    • The group_chat.run_stream method is called with the user’s task. This method returns an asynchronous generator that streams the intermediate messages.
    • The Console component displays the stream of messages and results.
  4. Agent Interaction:
    • The AssistantAgent generates Python code based on the user’s input.
    • The CodeExecutorAgent executes the generated code and returns the results.
    • The interaction alternates between the two agents until one of the termination conditions is met.
  5. Termination:
    • The process continues until either the word “TERMINATE” is mentioned or 10 messages have been exchanged.
    • Once a termination condition is met, the final results are displayed to the user.

This setup allows for an interactive and iterative process where the AssistantAgent generates code, the CodeExecutorAgent executes it, and the results are displayed to the user until the task is completed. 

Conclusion

In conclusion, the AutoGen Framework and AutoGen Studio are complementary tools for building multi-agent systems. AutoGen Studio offers a low-code, user-friendly interface for rapid prototyping and visual development, making multi-agent AI accessible to a broader audience.

Meanwhile, the AutoGen Framework provides developers with detailed control, customization, and integration capabilities.

Both tools leverage the same core components to facilitate multi-agent interactions and task automation.

By starting with AutoGen Studio for quick prototyping and transitioning to the AutoGen Framework for advanced development, you can streamline and enhance their workflow automation processes.

What’s Next

Code Generation, Execution, and Debugging

Task Solving with Code Generation, Execution and Debugging | AutoGen 0.2

This notebook demonstrates the use of two agents, AssistantAgent and UserProxyAgent, to write, execute, and debug Python code.

The process involves continuous communication between the two agents. AssistantAgent writes and debugs the code, while UserProxyAgent executes it and provides feedback until the task is successfully completed.

We’re excited to see how you’ll use AutoGen to uncover the best collaboration patterns and inspire new ideas for building multi-agent systems.

In the final Part 3 (upcoming) , we will wrap up our series by revisiting Part 1. We’ll include sample code from AutoGen Studio, translate the JSON code to Python, and utilize the AutoGen Framework.

Stay tuned for the next post!

Call to Action: 

Connect with me on LinkedIn Rajeev Singh | LinkedIn and be sure to like, comment, and share this post to help spread the knowledge!

Exit mobile version