跳到内容

LangGraph 集群

名称 描述
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 公开可配置参数。

返回

类型 描述
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>

description str | None

移交工具的可选描述。如果未提供,工具描述将为“向代理 寻求帮助”。