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
|
列表[Pregel]
|
要添加到群体的智能体列表。智能体可以是 LangGraph CompiledStateGraph、函数式 API 工作流 或任何其他 Pregel 对象。 |
必填 |
default_active_agent
|
str
|
默认路由到的智能体名称(如果没有当前活跃的智能体)。 |
必填 |
state_schema
|
StateSchemaType
|
用于多智能体图的状态模式。 |
SwarmState
|
config_schema
|
类型[Any] | None
|
可选的配置模式。使用此参数通过 |
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
|
列表[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 提供商接受的工具名称(工具名称将如下所示: |
必填 |
name
|
str | None
|
用于移交的可选工具名称。如果未提供,工具名称将为 |
None
|
description
|
str | None
|
移交工具的可选描述。如果未提供,工具描述将为 |
None
|