Multi-Agent Supervisor

Open in ColabOpen in GitHub

Overview

In the previous tutorial, we showed how to automatically route messages based on the output of the initial Researcher agent. However, when there are multiple agents that need to be coordinated, simple branching logic has limitations. Here, we introduce how to manage agents through LLM-based Supervisor and coordinate the entire team based on the results of each agent node.

In this tutorial, we'll explore how to build a multi-agent system using LangGraph , efficiently coordinate tasks between agents, and manage them through a Supervisor . We'll cover handling multiple agents simultaneously, managing each agent to perform their role, and properly handling task completion.

Key Points :

  • The Supervisor brings together various expert agents and operates them as a single team.

  • The Supervisor agent monitors the team's progress and executes logic such as calling appropriate agents for each step or terminating tasks.

What We'll Cover in This Tutorial

  • Setup : How to install required packages and set up API keys

  • Tool Creation : Defining tools for agents to use, such as web search and plot generation

  • Helper Utilities : Defining utility functions needed for creating agent nodes

  • Creating the Supervisor : Creating a Supervisor that contains logic for selecting Worker nodes and handling task completion

  • Constructing the Graph : Constructing the complete graph by defining State and Worker nodes

  • Calling the Team : Calling the graph to see how the multi-agent system actually works

In this process, we'll use LangGraph's pre-built create_react_agent function to simplify each agent node.

This use of "advanced agents" is meant to demonstrate specific design patterns in LangGraph, and can be combined with other basic patterns as needed to achieve optimal results.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.

[Note] This is not necessary if you've already set the required API keys in previous steps.

Setting State

Define state to be used in the multi-agent system.

Creating Agents

Creating Tools

In this example, we'll create agents that use a search engine to perform web research and generate plots.

Define the tools to be used below.

  • Research : Use TavilySearch tool to perform web research. To use this tool, you need to set the TAVILY_API_KEY . Please refer to previous tutorial for more details.

  • Coder : Use PythonREPLTool tool to run code.

Creating Utility for Creating Agents

When building a multi-agent system using LangGraph, helper functions play a crucial role in creating and managing agent nodes. These functions enhance code reusability and simplify interactions between agents.

  • Creating Agent Nodes : Define functions to create nodes for each agent's role

  • Managing Workflow : Provide utilities to manage the workflow between agents

  • Error Handling : Include mechanisms to handle errors that may occur during agent execution

The following is an example of defining a function called agent_node .

This function creates an agent node using the given state and agent. We will call this function later using functools.partial .

Below is an example of creating a research_node using functools.partial .

Note

Role of functools.partial

functools.partial is used to create a new function by fixing some arguments or keyword arguments of an existing function. In other words, it helps simplify commonly used function call patterns.

Roles

  1. Create new function with predefined values : Returns a new function with some arguments of the existing function pre-specified.

  2. Code simplification : Reduces code duplication by simplifying commonly used function call patterns.

  3. Improved readability : Customizes function behavior for specific tasks to make it more intuitive to use.

Example code

  1. Assume there is an existing function called agent_node .

    • This function can accept multiple arguments and keyword arguments.

  2. functools.partial fixes the values agent=research_agent and names="Researcher" for this function.

    • This means that research_node no longer needs to specify the agent and names values when calling agent_node .

    • For example:

    Instead, you can use:

Let's run the code and check the results.

Creating Agent Supervisor

Create an agent that manages and supervises agents.

Constructing the Graph

Now, we're ready to build the graph. Below, we'll use the functions we just defined to define state and worker nodes.

Visualize the graph.

png

Calling the Team

Now, we can check the performance by calling the graph.

png

Last updated