LangGraph 监督器¶
函数
名称 | 描述 |
---|---|
create_supervisor |
创建一个多代理监督器。 |
create_supervisor ¶
create_supervisor(
agents: list[Pregel],
*,
model: LanguageModelLike,
tools: (
list[BaseTool | Callable] | ToolNode | None
) = None,
prompt: Prompt | None = None,
response_format: Optional[
Union[
StructuredResponseSchema,
tuple[str, StructuredResponseSchema],
]
] = None,
parallel_tool_calls: bool = False,
state_schema: StateSchemaType = AgentState,
config_schema: Type[Any] | None = None,
output_mode: OutputMode = "last_message",
add_handoff_messages: bool = True,
handoff_tool_prefix: Optional[str] = None,
add_handoff_back_messages: Optional[bool] = None,
supervisor_name: str = "supervisor",
include_agent_name: AgentNameMode | None = None
) -> StateGraph
创建一个多代理监督器。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
agents
|
list[Pregel]
|
要管理的代理列表。代理可以是 LangGraph 的 CompiledStateGraph、函数式 API 工作流,或任何其他 Pregel 对象。 |
必需 |
model
|
LanguageModelLike
|
监督器使用的语言模型 |
必需 |
tools
|
list[BaseTool | Callable] | ToolNode | None
|
监督器使用的工具 |
无
|
prompt
|
Prompt | None
|
监督器可选的提示词。可以是以下之一:
|
无
|
response_format
|
Optional[Union[StructuredResponseSchema, tuple[str, StructuredResponseSchema]]]
|
最终监督器输出的可选 schema。 如果提供,输出将格式化为与给定 schema 匹配,并在 'structured_response' 状态键中返回。如果未提供,
重要
注意
|
无
|
parallel_tool_calls
|
bool
|
是否允许监督器 LLM 并行调用工具(仅限 OpenAI 和 Anthropic)。使用此参数可控制监督器是否可以同时移交给多个代理。如果为 True,将启用并行工具调用。如果为 False,将禁用并行工具调用(默认)。 重要 目前仅 OpenAI 和 Anthropic 模型支持此功能。要控制其他提供商的并行工具调用,请在系统提示中添加明确的工具使用说明。 |
False
|
state_schema
|
StateSchemaType
|
监督器图要使用的状态 schema。 |
AgentState
|
config_schema
|
Type[Any] | None
|
配置的可选 schema。使用此参数可通过 |
无
|
output_mode
|
OutputMode
|
将受管代理的输出添加到多代理工作流消息历史的模式。可以是以下之一:
|
'last_message'
|
add_handoff_messages
|
bool
|
当发生移交时,是否向消息历史添加一对 (AIMessage, ToolMessage)。 |
True
|
handoff_tool_prefix
|
Optional[str]
|
移交工具的可选前缀(例如,“delegate_to_”或“transfer_to_”)。如果提供,移交工具将被命名为 |
无
|
add_handoff_back_messages
|
Optional[bool]
|
当将控制权返回给监督器时,是否向消息历史添加一对 (AIMessage, ToolMessage) 以指示已发生移交。 |
无
|
supervisor_name
|
str
|
监督器节点的名称。 |
'supervisor'
|
include_agent_name
|
AgentNameMode | None
|
用于指定如何将代理名称暴露给底层监督器 LLM。
|
无
|
示例
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 web_search(query: str) -> str:
'''Search the web for information.'''
return 'Here are the headcounts for each of the FAANG companies in 2024...'
math_agent = create_react_agent(
model="openai:gpt-4o",
tools=[add],
name="math_expert",
)
research_agent = create_react_agent(
model="openai:gpt-4o",
tools=[web_search],
name="research_expert",
)
# Create supervisor workflow
workflow = create_supervisor(
[research_agent, math_agent],
model=ChatOpenAI(model="gpt-4o"),
)
# Compile and run
app = workflow.compile()
result = app.invoke({
"messages": [
{
"role": "user",
"content": "what's the combined headcount of the FAANG companies in 2024?"
}
]
})
函数
名称 | 描述 |
---|---|
create_handoff_tool |
创建一个可以将控制权移交给指定代理的工具。 |
create_forward_message_tool |
创建一个监督器可以按名称转发工作器消息的工具。 |
create_handoff_tool ¶
create_handoff_tool(
*,
agent_name: str,
name: str | None = None,
description: str | None = None,
add_handoff_messages: bool = True
) -> BaseTool
创建一个可以将控制权移交给指定代理的工具。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
agent_name
|
str
|
要移交控制权的代理名称,即多代理图中的代理节点名称。代理名称应简洁、清晰且唯一,最好采用 snake_case 命名,但您仅限于 LangGraph 节点接受的名称以及 LLM 提供商接受的工具名称(工具名称将如下所示: |
必需 |
name
|
str | None
|
用于移交工具的可选名称。如果未提供,工具名称将为 |
无
|
description
|
str | None
|
移交工具的可选描述。如果未提供,描述将为 |
无
|
add_handoff_messages
|
bool
|
是否将移交消息添加到消息历史中。如果为 False,移交消息将从消息历史中省略。 |
True
|