跳到内容

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

监督器可选的提示词。可以是以下之一:

  • str:这会被转换为 SystemMessage 并添加到 state["messages"] 消息列表的开头。
  • SystemMessage:这会被添加到 state["messages"] 消息列表的开头。
  • Callable:此函数应接收完整的图状态,其输出随后会传递给语言模型。
  • Runnable:此可运行对象应接收完整的图状态,其输出随后会传递给语言模型。
response_format Optional[Union[StructuredResponseSchema, tuple[str, StructuredResponseSchema]]]

最终监督器输出的可选 schema。

如果提供,输出将格式化为与给定 schema 匹配,并在 'structured_response' 状态键中返回。如果未提供,structured_response 将不存在于输出状态中。可以作为以下形式传入:

- an OpenAI function/tool schema,
- a JSON Schema,
- a TypedDict class,
- or a Pydantic class.
- a tuple (prompt, schema), where schema is one of the above.
    The prompt will be used together with the model that is being used to generate the structured response.

重要

response_format 要求模型支持 .with_structured_output

注意

response_format 要求您的状态 schema 中包含 structured_response 键。您可以使用预构建的 langgraph.prebuilt.chat_agent_executor.AgentStateWithStructuredResponse

parallel_tool_calls bool

是否允许监督器 LLM 并行调用工具(仅限 OpenAI 和 Anthropic)。使用此参数可控制监督器是否可以同时移交给多个代理。如果为 True,将启用并行工具调用。如果为 False,将禁用并行工具调用(默认)。

重要

目前仅 OpenAI 和 Anthropic 模型支持此功能。要控制其他提供商的并行工具调用,请在系统提示中添加明确的工具使用说明。

False
state_schema StateSchemaType

监督器图要使用的状态 schema。

AgentState
config_schema Type[Any] | None

配置的可选 schema。使用此参数可通过 supervisor.config_specs 暴露可配置参数。

output_mode OutputMode

将受管代理的输出添加到多代理工作流消息历史的模式。可以是以下之一:

  • full_history:添加完整的代理消息历史
  • last_message:仅添加最后一条消息(默认)
'last_message'
add_handoff_messages bool

当发生移交时,是否向消息历史添加一对 (AIMessage, ToolMessage)。

True
handoff_tool_prefix Optional[str]

移交工具的可选前缀(例如,“delegate_to_”或“transfer_to_”)。如果提供,移交工具将被命名为 handoff_tool_prefix_agent_name。如果未提供,移交工具将被命名为 transfer_to_agent_name

add_handoff_back_messages Optional[bool]

当将控制权返回给监督器时,是否向消息历史添加一对 (AIMessage, ToolMessage) 以指示已发生移交。

supervisor_name str

监督器节点的名称。

'supervisor'
include_agent_name AgentNameMode | None

用于指定如何将代理名称暴露给底层监督器 LLM。

  • None:依赖 LLM 提供商使用 AI 消息中的 name 属性。目前,只有 OpenAI 支持此功能。
  • "inline":使用 XML 样式标签将代理名称直接添加到 AI 消息的内容字段中。示例:"How can I help you" -> "<name>agent_name</name><content>How can I help you?</content>"
示例
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 提供商接受的工具名称(工具名称将如下所示:transfer_to_<agent_name>)。

必需
name str | None

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

description str | None

移交工具的可选描述。如果未提供,描述将为 向代理 <agent_name> 请求帮助

add_handoff_messages bool

是否将移交消息添加到消息历史中。如果为 False,移交消息将从消息历史中省略。

True

create_forward_message_tool

create_forward_message_tool(
    supervisor_name: str = "supervisor",
) -> BaseTool

创建一个监督器可以按名称转发工作器消息的工具。

这有助于避免监督器将工作器查询重写给用户时发生信息丢失,并且还可以节省一些 token。

参数

名称 类型 描述 默认值
supervisor_name str

监督器节点的名称(用于工具的命名空间)。

'supervisor'

返回

名称 类型 描述
BaseTool BaseTool

“forward_message”工具。