A lightweight graph execution engine with zero external dependencies — state management, checkpointing, streaming, and multi-agent orchestration.
Project description
ZeroGraph
一个轻量级图执行引擎,用于构建有状态的多步骤工作流 —— 零外部依赖。
受 LangGraph 启发,ZeroGraph 实现了类似 Pregel 的超步执行模型,支持检查点、流式输出、子图支持和预构建 LLM 智能体模式,全部纯 Python 实现。
特性
- StateGraph —— 将工作流定义为带类型的状态机,支持节点、边和条件路由
- 通道系统 —— 通过归约器灵活管理状态(
LastValue、BinaryOperatorAggregate、AnyValue、Topic等) - 检查点 —— 使用
InMemorySaver和SqliteSaver(同步 + 异步)持久化和恢复执行 - 流式输出 —— 多种流模式:
values、updates、custom、messages、checkpoints、tasks - 子图 —— 在图中嵌套图,状态按命名空间隔离
- 中断与恢复 —— 在任意节点暂停执行,稍后使用用户输入恢复
- 函数式 API ——
@entrypoint和@task装饰器,工作流风格定义 - 缓存与存储 —— 节点级结果缓存(支持 TTL)和长期键值记忆
- 预构建智能体 ——
ToolNode、create_react_agent、create_supervisor、create_swarm - LLM 流式 ——
LLMStreamAdapter适配 OpenAI/Anthropic 风格的分块流式 - 可视化 —— 从任意
StateGraph生成 Mermaid 图
安装
pip install zerograph
快速开始
简单线性图
from typing import Annotated, TypedDict
import operator
from zerograph import StateGraph, START, END
class State(TypedDict):
messages: Annotated[list, operator.add]
def greet(state: State) -> dict:
return {"messages": ["你好!"]}
def bye(state: State) -> dict:
return {"messages": ["再见!"]}
graph = StateGraph(State)
graph.add_node("greet", greet)
graph.add_node("bye", bye)
graph.add_edge(START, "greet")
graph.add_edge("greet", "bye")
graph.add_edge("bye", END)
app = graph.compile()
result = app.invoke({"messages": []})
print(result["messages"]) # ['你好!', '再见!']
条件路由
from zerograph import StateGraph, START, END
def router(state: dict) -> str:
if state["x"] > 0:
return "positive"
return "negative"
graph = StateGraph(dict)
graph.add_node("positive", lambda s: {"label": "正"})
graph.add_node("negative", lambda s: {"label": "负"})
graph.add_conditional_edges(START, router, {"positive": "positive", "negative": "negative"})
graph.add_edge("positive", END)
graph.add_edge("negative", END)
app = graph.compile()
print(app.invoke({"x": 5})) # {'x': 5, 'label': '正'}
print(app.invoke({"x": -3})) # {'x': -3, 'label': '负'}
流式输出
app = graph.compile()
for event in app.stream({"messages": []}, stream_mode="updates"):
print(event)
# {'greet': {'messages': ['你好!']}}
# {'bye': {'messages': ['再见!']}}
检查点与中断
from zerograph import StateGraph, START, END, InMemorySaver, interrupt
from zerograph.types import Command
def human_review(state: dict) -> dict:
answer = interrupt("请审核并确认:")
return {"approved": answer}
graph = StateGraph(dict)
graph.add_node("review", human_review)
graph.add_edge(START, "review")
graph.add_edge("review", END)
checkpointer = InMemorySaver()
app = graph.compile(checkpointer=checkpointer, interrupt_after=["review"])
# 第一次调用 —— 在 "review" 处暂停
config = {"configurable": {"thread_id": "1"}}
result = app.invoke({"approved": False}, config)
# 使用用户输入恢复
result = app.invoke(Command(resume=True), config)
print(result["approved"]) # True
ReAct 智能体
from zerograph import create_react_agent
def search(query: str) -> str:
"""搜索网页。"""
return f"{query} 的结果"
def calculator(expr: str) -> float:
"""计算数学表达式。"""
return eval(expr)
def llm_call(messages, tools=None):
# 你的 LLM 集成代码
...
agent = create_react_agent(llm_call, [search, calculator])
result = agent.invoke({"messages": [{"role": "user", "content": "2+2 等于多少?"}]})
API 概览
核心
| 符号 | 说明 |
|---|---|
StateGraph |
带类型状态的图构建器 |
CompiledStateGraph |
编译后的可执行图 |
START, END |
特殊节点常量 |
Send |
动态扇出到指定节点 |
Command |
更新状态并控制流程 |
执行
| 符号 | 说明 |
|---|---|
interrupt() |
从节点内部暂停执行 |
entrypoint() |
函数式 API 入口点装饰器 |
task() |
离散工作单元装饰器 |
检查点
| 符号 | 说明 |
|---|---|
BaseCheckpointSaver |
抽象检查点后端 |
InMemorySaver |
内存检查点存储 |
SqliteSaver |
SQLite 检查点存储 |
AsyncSqliteSaver |
异步 SQLite 检查点存储 |
状态与类型
| 符号 | 说明 |
|---|---|
add_messages |
消息列表归约器(按 ID 进行 upsert) |
RemoveMessage |
按 ID 删除消息的标记 |
RetryPolicy |
可配置的指数退避重试策略 |
TimeoutPolicy |
每节点超时配置 |
预构建智能体
| 符号 | 说明 |
|---|---|
ToolNode |
执行工具调用的节点 |
create_react_agent |
一行构建 ReAct 循环 |
create_supervisor |
构建主管多智能体图 |
create_swarm |
构建基于 handoff 路由的群组 |
LLMStreamAdapter |
OpenAI/Anthropic 分块流适配器 |
基础设施
| 符号 | 说明 |
|---|---|
BaseCache / InMemoryCache |
节点级结果缓存,支持 TTL |
BaseStore / InMemoryStore |
长期键值记忆 |
要求
- Python >= 3.11
- 零外部依赖
许可证
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
zerograph-0.4.0.tar.gz
(138.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
zerograph-0.4.0-py3-none-any.whl
(68.7 kB
view details)
File details
Details for the file zerograph-0.4.0.tar.gz.
File metadata
- Download URL: zerograph-0.4.0.tar.gz
- Upload date:
- Size: 138.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43f0ea96d4c8e9778ce2016f1240b582e4c82450eb49fdf15bcc35b7487347d3
|
|
| MD5 |
80dce467d512a8e6d1eb7945a0341e73
|
|
| BLAKE2b-256 |
ebed2cd6daf6f11e4cdbdd1f8de94ebe48074c7c55a447ee07bb4891f5d8cdad
|
File details
Details for the file zerograph-0.4.0-py3-none-any.whl.
File metadata
- Download URL: zerograph-0.4.0-py3-none-any.whl
- Upload date:
- Size: 68.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
039cdd5895d140e6a36926c859f65f16d5a6e8c5704090e184b618d637d0d868
|
|
| MD5 |
4799a622c4c379c19d5e61afe283d16d
|
|
| BLAKE2b-256 |
00df793c13653775e6fed8eabe75732bfcf4ea0f9ba67606e1624797a36a2b05
|