跳转到内容

LangGraph Supervisor

langgraph_supervisor.supervisor

函数 描述
create_supervisor

创建一个多代理主管。

create_supervisor

create_supervisor(
    agents: list[Pregel],
    *,
    model: LanguageModelLike,
    tools: list[BaseTool | Callable] | ToolNode | None = None,
    prompt: Prompt | None = None,
    response_format: StructuredResponseSchema
    | tuple[str, StructuredResponseSchema]
    | None = None,
    pre_model_hook: RunnableLike | None = None,
    post_model_hook: RunnableLike | None = None,
    parallel_tool_calls: bool = False,
    state_schema: StateSchemaType | None = None,
    context_schema: Type[Any] | None = None,
    output_mode: OutputMode = "last_message",
    add_handoff_messages: bool = True,
    handoff_tool_prefix: str | None = None,
    add_handoff_back_messages: bool | None = None,
    supervisor_name: str = "supervisor",
    include_agent_name: AgentNameMode | None = None,
    **deprecated_kwargs: Unpack[DeprecatedKwargs],
) -> StateGraph

创建一个多代理主管。

参数 描述
agents

要管理的代理列表。一个代理可以是 LangGraph 的 CompiledStateGraph、一个函数式 API 工作流,或任何其他 Pregel 对象。

类型: list[Pregel]

model

用于主管的语言模型

类型: LanguageModelLike

工具

用于主管的工具

类型: list[BaseTool | Callable] | ToolNode | None 默认值: None

prompt

用于主管的可选提示。可以是以下之一

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

类型: Prompt | None 默认值: None

response_format

一个可选的模式,用于最终的主管输出。

如果提供,输出将被格式化以匹配给定的模式,并返回在 'structured_response' 状态键中。

如果不提供,structured_response 将不会出现在输出状态中。

可以作为以下形式传入

- An OpenAI function/tool schema,
- A JSON Schema,
- A TypedDict class,
- 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 要求在您的状态模式中有 structured_response 键。您可以使用预构建的 langgraph.prebuilt.chat_agent_executor.AgentStateWithStructuredResponse

类型: StructuredResponseSchema | tuple[str, StructuredResponseSchema] | None 默认值: None

pre_model_hook

一个可选的节点,用于在主管代理中的 LLM 节点(即调用 LLM 的节点)之前添加。对于管理长消息历史(例如消息修剪、摘要等)很有用。模型前钩子必须是一个可调用对象或一个可运行对象,它接收当前图状态并以如下形式返回状态更新

# At least one of `messages` or `llm_input_messages` MUST be provided
{
    # If provided, will UPDATE the `messages` in the state
    "messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES), ...],
    # If provided, will be used as the input to the LLM,
    # and will NOT UPDATE `messages` in the state
    "llm_input_messages": [...],
    # Any other state keys that need to be propagated
    ...
}

重要

必须提供 messagesllm_input_messages 中的至少一个,并且它将用作 agent 节点的输入。其余的键将被添加到图状态中。

警告

如果您在模型前钩子中返回 messages,您应该通过执行以下操作来覆盖 messages

{
    "messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES), *new_messages]
    ...
}

类型: RunnableLike | None 默认值: None

post_model_hook

一个可选的节点,用于在主管代理中的 LLM 节点(即调用 LLM 的节点)之后添加。对于实现人在环路、护栏、验证或其他后处理很有用。模型后钩子必须是一个可调用对象或一个可运行对象,它接收当前图状态并返回状态更新。

类型: RunnableLike | None 默认值: None

parallel_tool_calls

是否允许主管 LLM 并行调用工具(仅限 OpenAI 和 Anthropic)。用此来控制主管是否可以同时将任务移交给多个代理。

如果为 True,将启用并行工具调用。

如果为 False,将禁用并行工具调用。

重要

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

类型: bool 默认值: False

state_schema

用于主管图的状态模式。

类型: StateSchemaType | None 默认值: None

context_schema

指定将传递给工作流的上下文对象的模式。

类型: Type[Any] | None 默认值: None

output_mode

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

  • full_history:添加整个代理消息历史
  • last_message:仅添加最后一条消息

类型: OutputMode 默认值: 'last_message'

add_handoff_messages

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

类型: bool 默认值: True

handoff_tool_prefix

移交工具的可选前缀(例如,'delegate_to_''transfer_to_'

如果提供,移交工具将被命名为 handoff_tool_prefix_agent_name

如果不提供,移交工具将被命名为 transfer_to_agent_name

类型: str | None 默认值: None

add_handoff_back_messages

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

类型: bool | None 默认值: None

supervisor_name

主管节点的名称。

类型: str 默认值: 'supervisor'

include_agent_name

用于指定如何向底层主管 LLM 公开代理名称。

  • None:依赖于 LLM 提供商使用 AI 消息上的 name 属性。目前,只有 OpenAI 支持此功能。
  • 'inline':使用 XML 风格的标签将代理名称直接添加到 AI 消息的内容字段中。示例:"我能帮你什么?" -> "<name>agent_name</name><content>我能帮你什么?</content>"

类型: AgentNameMode | None 默认值: None

示例
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?"
        }
    ]
})

langgraph_supervisor.handoff

函数 描述
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

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

类型: str

name

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

类型: str | None 默认值: None

描述

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

类型: str | None 默认值: None

add_handoff_messages

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

类型: bool 默认值: True

create_forward_message_tool

create_forward_message_tool(supervisor_name: str = 'supervisor') -> BaseTool

创建一个主管可以用来按名称转发工作代理消息的工具。

这有助于在主管重写工作代理对用户的查询时避免信息丢失,并且还可以节省一些令牌。

参数 描述
supervisor_name

主管节点的名称(用于工具的命名空间)。

类型: str 默认值: 'supervisor'

返回 描述
BaseTool

“forward_message”工具。

类型: BaseTool

© . This site is unofficial and not affiliated with LangChain, Inc.