跳到内容

LangGraph Swarm

名称 描述
SwarmState

用于多代理群体的状态模式。

函数

名称 描述
create_swarm

创建一个多代理群体。

add_active_agent_router

向当前活动代理添加路由器到 StateGraph。

SwarmState

基类: MessagesState

用于多代理群体的状态模式。

create_swarm

create_swarm(
    agents: list[Pregel],
    *,
    default_active_agent: str,
    state_schema: StateSchemaType = SwarmState,
    config_schema: type[Any] | None = None
) -> StateGraph

创建一个多代理群体。

参数

名称 类型 描述 默认值
agents list[Pregel]

要添加到群体的代理列表。代理可以是 LangGraph CompiledStateGraph、功能 API 工作流,或任何其他 Pregel 对象。

必填
default_active_agent str

默认路由到的代理名称(如果没有代理当前处于活动状态)。

必填
state_schema StateSchemaType

用于多代理图的状态模式。

SwarmState
config_schema type[Any] | None

一个可选的配置模式。使用此项通过 swarm.config_specs 公开可配置参数。

None

返回

类型 描述
StateGraph

一个多代理群体 StateGraph。

示例
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.prebuilt import create_react_agent
from langgraph_swarm import create_handoff_tool, create_swarm

def add(a: int, b: int) -> int:
    '''Add two numbers'''
    return a + b

alice = create_react_agent(
    "openai:gpt-4o",
    [add, create_handoff_tool(agent_name="Bob")],
    prompt="You are Alice, an addition expert.",
    name="Alice",
)

bob = create_react_agent(
    "openai:gpt-4o",
    [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
    prompt="You are Bob, you speak like a pirate.",
    name="Bob",
)

checkpointer = InMemorySaver()
workflow = create_swarm(
    [alice, bob],
    default_active_agent="Alice"
)
app = workflow.compile(checkpointer=checkpointer)

config = {"configurable": {"thread_id": "1"}}
turn_1 = app.invoke(
    {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
    config,
)
turn_2 = app.invoke(
    {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
    config,
)

add_active_agent_router

add_active_agent_router(
    builder: StateGraph,
    *,
    route_to: list[str],
    default_active_agent: str
) -> StateGraph

向当前活动代理添加路由器到 StateGraph。

参数

名称 类型 描述 默认值
builder StateGraph

要添加路由器的图构建器 (StateGraph)。

必填
route_to list[str]

要路由到的代理(节点)名称列表。

必填
default_active_agent str

默认路由到的代理名称(如果没有代理当前处于活动状态)。

必填

返回

类型 描述
StateGraph

添加了路由器的 StateGraph。

示例
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.prebuilt import create_react_agent
from langgraph.graph import StateGraph
from langgraph_swarm import SwarmState, create_handoff_tool, add_active_agent_router

def add(a: int, b: int) -> int:
    '''Add two numbers'''
    return a + b

alice = create_react_agent(
    "openai:gpt-4o",
    [add, create_handoff_tool(agent_name="Bob")],
    prompt="You are Alice, an addition expert.",
    name="Alice",
)

bob = create_react_agent(
    "openai:gpt-4o",
    [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
    prompt="You are Bob, you speak like a pirate.",
    name="Bob",
)

checkpointer = InMemorySaver()
workflow = (
    StateGraph(SwarmState)
    .add_node(alice, destinations=("Bob",))
    .add_node(bob, destinations=("Alice",))
)
# this is the router that enables us to keep track of the last active agent
workflow = add_active_agent_router(
    builder=workflow,
    route_to=["Alice", "Bob"],
    default_active_agent="Alice",
)

# compile the workflow
app = workflow.compile(checkpointer=checkpointer)

config = {"configurable": {"thread_id": "1"}}
turn_1 = app.invoke(
    {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
    config,
)
turn_2 = app.invoke(
    {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
    config,
)

函数

名称 描述
create_handoff_tool

创建一个可以将控制权移交给指定代理的工具。

create_handoff_tool

create_handoff_tool(
    *,
    agent_name: str,
    name: str | None = None,
    description: str | None = None
) -> BaseTool

创建一个可以将控制权移交给指定代理的工具。

参数

名称 类型 描述 默认值
agent_name str

要将控制权移交给的代理名称,即多代理图中代理节点的名称。代理名称应简单、清晰且唯一,最好采用蛇形命名法(snake_case),尽管您仅受限于 LangGraph 节点接受的名称以及 LLM 提供商接受的工具名称(工具名称将类似于:transfer_to_<agent_name>)。

必填
name str | None

用于移交工具的可选名称。如果未提供,工具名称将为 transfer_to_<agent_name>

None
description str | None

移交工具的可选描述。如果未提供,工具描述将为 Ask agent <agent_name> for help

None