跳转至

预构建组件

ToolNode

zerograph.prebuilt.tool_node.ToolNode

Node that executes tools based on LLM tool_calls in the last message.

Usage::

tools = [search_tool, calculator_tool]
graph.add_node("tools", ToolNode(tools))

__init__

__init__(
    tools: list[Callable],
    *,
    handle_errors: bool = True,
    store: Any = None
) -> None

__call__

__call__(state: dict) -> dict

Execute tools synchronously.

ainvoke async

ainvoke(state: dict) -> dict

Execute tools asynchronously — supports async tool functions.

inject_tools staticmethod

inject_tools(tools: list[Callable]) -> list[dict]

Generate OpenAI function calling format tools parameter.

Usage::

tools_param = ToolNode.inject_tools([search, calc])
response = client.chat.completions.create(
    model="gpt-4", messages=msgs, tools=tools_param
)

InjectedState

zerograph.prebuilt.tool_node.InjectedState

Bases: _InjectedMarker

Marker annotation: inject the full graph state into this tool parameter.

Usage::

def my_tool(query: str, state: InjectedState) -> str:
    # ``state`` receives the full graph state dict
    return f"{query} in context of {state}"

InjectedStore

zerograph.prebuilt.tool_node.InjectedStore

Bases: _InjectedMarker

Marker annotation: inject the Store into this tool parameter.

Usage::

def my_tool(query: str, store: InjectedStore) -> str:
    # ``store`` receives the InMemoryStore instance
    return store.get("namespace", key)

create_react_agent

zerograph.prebuilt.react_agent.create_react_agent

create_react_agent(
    llm_callable: Callable,
    tools: list[Callable],
    *,
    state_schema: type | None = None,
    checkpointer: Any | None = None,
    max_iterations: int = 25
) -> Any

Create a ReAct agent graph with minimal code.

Usage::

agent = create_react_agent(llm_call, [search_tool, calc_tool])
result = agent.invoke({"messages": [{"role": "user", "content": "..."}]})

参数:

名称 类型 描述 默认
llm_callable Callable

Function(messages, tools=...) -> message_dict. Must accept a list of message dicts and an optional tools keyword argument with OpenAI-format tool schemas. Must return a dict with at least role and content.

必需
tools list[Callable]

List of tool functions. Each function's name, docstring, and signature are used to generate the tools schema.

必需
state_schema type | None

Optional TypedDict for state. Defaults to {messages: Annotated[list, add_messages]}.

None
checkpointer Any | None

Optional checkpoint saver instance.

None

返回:

类型 描述
Any

CompiledStateGraph ready for .invoke() / .stream().

create_supervisor

zerograph.prebuilt.supervisor.create_supervisor

create_supervisor(
    llm_callable: Callable,
    agents: list[Any],
    *,
    state_schema: type | None = None,
    checkpointer: Any | None = None
) -> Any

Create a supervisor multi-agent graph.

The supervisor node calls llm_callable(messages, tools=...) to decide which agent to route to next. Each agent runs and returns to the supervisor. The loop continues until the LLM response contains no tool_calls with agent names (indicating it wants to finish).

参数:

名称 类型 描述 默认
llm_callable Callable

fn(messages, tools=...) -> message_dict. Must return a dict with role, content, and optionally tool_calls. When the supervisor wants to invoke an agent it returns a tool_call whose function.name matches the agent name.

必需
agents list[Any]

List of agent callables or CompiledStateGraph instances.

必需
state_schema type | None

Optional state TypedDict. Defaults to {messages: Annotated[list, add_messages]}.

None
checkpointer Any | None

Optional checkpoint saver.

None

返回:

类型 描述
Any

CompiledStateGraph ready for .invoke() / .stream().

create_swarm

zerograph.prebuilt.swarm.create_swarm

create_swarm(
    agents: list[Callable],
    tools: list[Callable] | None = None,
    *,
    state_schema: type | None = None,
    checkpointer: Any | None = None
) -> Any

Create a swarm multi-agent graph.

Each agent is a callable fn(state) -> dict. An agent signals a handoff by returning a message dict with a "handoff" key whose value is the name of the next agent. If no handoff is signalled the swarm terminates.

Optional shared tools are available to every agent via a shared "tools" node (ToolNode).

参数:

名称 类型 描述 默认
agents list[Callable]

List of agent callables.

必需
tools list[Callable] | None

Optional list of tool functions shared by all agents.

None
state_schema type | None

Optional state TypedDict.

None
checkpointer Any | None

Optional checkpoint saver.

None

返回:

类型 描述
Any

CompiledStateGraph.