From 8c192414a24021a43de37e30415fb60b28a209c4 Mon Sep 17 00:00:00 2001 From: Zapiron <125368863+DangerousPotential@users.noreply.github.com> Date: Mon, 3 Feb 2025 22:29:19 +0800 Subject: [PATCH] docs: Update Hierarchial Multi Agent Example (#3282) Was trying to learn the Multi Agent Workflow examples and encountered some errors, which I fixed by editing these: * Added missing state for Team1, and importing `Command` * `ValueError: Node `LangGraph` already present.`: Seems to happen we add the `team_1_graph` node without giving it a name, it will default to the name `LangGraph`. Solved by giving the sub-graph a name when building the top-level supervisor. * Added the edges for the graph to feedback to the top level supervisor to decide whether it still needs to relegate the task to other nodes or end from there --------- Co-authored-by: Vadym Barda --- docs/docs/concepts/multi_agent.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/docs/concepts/multi_agent.md b/docs/docs/concepts/multi_agent.md index a6f4ad5c1..d6ccb5529 100644 --- a/docs/docs/concepts/multi_agent.md +++ b/docs/docs/concepts/multi_agent.md @@ -241,7 +241,7 @@ To address this, you can design your system _hierarchically_. For example, you c from typing import Literal from langchain_openai import ChatOpenAI from langgraph.graph import StateGraph, MessagesState, START, END - +from langgraph.types import Command model = ChatOpenAI() # define team 1 (same as the single supervisor example above) @@ -286,7 +286,7 @@ team_2_graph = team_2_builder.compile() # define top-level supervisor builder = StateGraph(MessagesState) -def top_level_supervisor(state: MessagesState): +def top_level_supervisor(state: MessagesState) -> Command[Literal["team_1_graph", "team_2_graph", END]]: # you can pass relevant parts of the state to the LLM (e.g., state["messages"]) # to determine which team to call next. a common pattern is to call the model # with a structured output (e.g. force it to return an output with a "next_team" field) @@ -297,10 +297,11 @@ def top_level_supervisor(state: MessagesState): builder = StateGraph(MessagesState) builder.add_node(top_level_supervisor) -builder.add_node(team_1_graph) -builder.add_node(team_2_graph) - +builder.add_node("team_1_graph", team_1_graph) +builder.add_node("team_2_graph", team_2_graph) builder.add_edge(START, "top_level_supervisor") +builder.add_edge("team_1_graph", "top_level_supervisor") +builder.add_edge("team_2_graph", "top_level_supervisor") graph = builder.compile() ```