跳到内容

LangGraph 主管

函数

名称 描述
create_supervisor

创建一个多智能体主管。

create_supervisor

create_supervisor(
    agents: list[Pregel],
    *,
    model: LanguageModelLike,
    tools: list[BaseTool | Callable] | 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] | None

用于主管的工具

None
prompt Prompt | None

可选参数,用于主管的提示。可以是以下之一:

  • str: 这会被转换为一个 SystemMessage 并添加到 state["messages"] 消息列表的开头。
  • SystemMessage: 这会被添加到 state["messages"] 消息列表的开头。
  • Callable: 这个函数应该接收完整的图状态,然后将输出传递给语言模型。
  • Runnable: 这个 Runnable 应该接收完整的图状态,然后将输出传递给语言模型。
None
response_format Optional[Union[StructuredResponseSchema, tuple[str, StructuredResponseSchema]]]

最终主管输出的可选模式(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 要求你的状态模式中包含 structured_response 键。你可以使用预置的 langgraph.prebuilt.chat_agent_executor.AgentStateWithStructuredResponse

None
parallel_tool_calls bool

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

重要

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

False
state_schema StateSchemaType

用于主管图谱的状态模式。

AgentState
config_schema Type[Any] | None

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

None
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

None
add_handoff_back_messages Optional[bool]

在将控制权返回给主管时,是否将一对 (AIMessage, ToolMessage) 添加到消息历史记录中,以表明已发生移交。

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

函数

名称 描述
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>

None
description str | None

移交工具的可选描述。如果未提供,描述将是 Ask agent <agent_name> for help

None
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' 工具。

评论