预构建组件¶
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__ ¶
ainvoke
async
¶
Execute tools asynchronously — supports async tool functions.
inject_tools
staticmethod
¶
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
|
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
|
None
|
checkpointer
|
Any | None
|
Optional checkpoint saver instance. |
None
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
CompiledStateGraph ready for |
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
|
|
必需 |
agents
|
list[Any]
|
List of agent callables or CompiledStateGraph instances. |
必需 |
state_schema
|
type | None
|
Optional state TypedDict. Defaults to
|
None
|
checkpointer
|
Any | None
|
Optional checkpoint saver. |
None
|
返回:
| 类型 | 描述 |
|---|---|
Any
|
CompiledStateGraph ready for |
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. |