General Introduction
LangGraph Supervisor is a Python library based on the LangGraph framework designed for creating and managing multi-intelligent body systems. The library coordinates the work of multiple specialized agents through a central supervisory agent, ensuring efficient management of communication flows and task assignments.LangGraph Supervisor supports both router and coordinator modes, provides instrumented agent handoff mechanisms, flexible message history management, and is suitable for application scenarios in streaming processing, short- and long-term memory, and human-in-the-loop.
Function List
- Create supervisory agents to coordinate multiple specialized agents
- Supports router and coordinator modes
- Instrumentalized proxy handover mechanisms
- Flexible message history management
- Supports streaming processing, short-term and long-term memory
- Human-in-the-loop application scenario support
Using Help
Installation process
- Ensure that the Python environment is installed.
- Install the LangGraph Supervisor library using pip:
pip install langgraph-supervisor
- Install the LangChain and OpenAI libraries:
pip install langchain-openai
- Set the OpenAI API key:
export OPENAI_API_KEY=
usage example
The following is a simple example showing how to manage two specialized agents using the LangGraph Supervisor:
- Import the necessary libraries and modules:
from langchain_openai import ChatOpenAI
from langgraph_supervisor import create_supervisor
from langgraph.prebuilt import create_react_agent
- Create specialized agents:
def add(a: float, b: float) -> float.
"""Add two numbers.""""
return a + b
def multiply(a: float, b: float) -> float.
"""Multiply two numbers.""""
return a * b
def web_search(query: str) -> str.
"""Search the web for information.""""
return "Here are the headcounts for each of the FAANG companies in 2024:..."
model = ChatOpenAI(model="gpt-4o")
math_agent = create_react_agent(model=model, tools=[add, multiply], name="math_expert", prompt="You are a math expert. Always use one tool at a Always use one tool at a time.")
research_agent = create_react_agent(model=model, tools=[web_search], name="research_expert", prompt="You are a world class researcher with Do not do any math.")
- Create the supervisor agent and compile and run it:
workflow = create_supervisor([research_agent, math_agent], model=model, prompt="You are a team supervisor managing a research expert and a math expert.")
app = workflow.compile()
result = app.invoke({
"messages": [
{ "role": "user", "content": "what's the combined headcount of the FAANG companies in 2024?"}
]
})
Detailed function operation flow
- Creating Supervisory Agents: By
create_supervisor
function creates a supervisory agent that is responsible for coordinating the work of multiple specialized agents. - Defining Specialized Agents: Use
create_react_agent
Functions define specialized agents for different functions, such as mathematical computation agents and web search agents. - Compile and run workflows: By
workflow.compile()
Compile the workflow and use theapp.invoke()
Runs a workflow that processes user input and returns results.
LangGraph Supervisor provides a flexible multi-intelligence system management solution for automating and coordinating a variety of complex tasks.