当前位置: 首页 > news >正文

LangGraph4j 学习系列(7)-checkpoint检查点

Checkpoint(检查点)的主要作用是保存图执行过程中的状态,让图可以在需要时暂停并在之后从断点恢复执行,通常需要与interrupt结合使用。
 
先定义1个图
public static StateGraph<MessagesState<String>> getGraph() throws GraphStateException {return new StateGraph<>(MessagesState.SCHEMA, MessagesState<String>::new).addNode("node-1", node_async(state -> Map.of(MESSAGES_STATE, "have"))).addNode("node-2", node_async(state -> Map.of(MESSAGES_STATE, "a"))).addNode("node-3", node_async(state -> Map.of(MESSAGES_STATE, "good"))).addNode("node-4", node_async(state -> Map.of(MESSAGES_STATE, "trip"))).addEdge(GraphDefinition.START, "node-1").addEdge("node-1", "node-2").addEdge("node-2", "node-3").addEdge("node-3", "node-4").addEdge("node-4", GraphDefinition.END);
}

image

常规执行后,预期应该是 输出 [have a good trip]

现在我们小改一下,在node-3节点进入前,设置1个打断,测试interrupt效果
static void startWithoutCheckpoint() throws Exception {StateGraph<MessagesState<String>> graph = getGraph();graph.addBeforeCallNodeHook((String node, MessagesState<String> data, RunnableConfig config) -> {out.println("Before calling node: " + node + ", data: " + data.data());return CompletableFuture.completedFuture(data.data());});//node-3进入前,被打断CompileConfig cc = CompileConfig.builder().interruptBefore("node-3").build();RunnableConfig rc = RunnableConfig.builder().threadId("test-interrupt").build();CompiledGraph<MessagesState<String>> workflow = graph.compile(cc);//运行完后,最终只会输出[have a] - node-3被打断,执行中止workflow.invoke(Map.of(), rc).ifPresent(state -> System.out.println(state.value(MESSAGES_STATE).orElse(null)));
}

  

运行效果

Before calling node: node-1, data: {messages=[]}
Before calling node: node-2, data: {messages=[have]}
[have, a]

可见,interrupt会让图提前中止。
 
仅仅设置interrupt断点通常没有太大的实际意义 ,可以结合CheckpointSaver在断点时,将图的状态保存下来,便于后续恢复。

image

 LangGraph4j提供了几种常用的CheckPointSaver实现,如上图。 可以先拿最简单的MemorySaver测试一下,基本用法如下:

static BaseCheckpointSaver getSaver() {return new MemorySaver();
//        return new FileSystemSaver(Path.of("output"), new ObjectStreamStateSerializer<>(MessagesState<String>::new));
//        return new JsonFileSystemSaver(Path.of("output"));}static void startWithCheckpoint(BaseCheckpointSaver saver) throws Exception {StateGraph<MessagesState<String>> graph = getGraph();graph.addBeforeCallNodeHook((String node, MessagesState<String> data, RunnableConfig config) -> {out.println("Before calling node: " + node + ", data: " + data.data());return CompletableFuture.completedFuture(data.data());});//node-3进入前,被打断CompileConfig cc = CompileConfig.builder().checkpointSaver(saver).interruptBefore("node-3").build();RunnableConfig rc = RunnableConfig.builder().threadId("test-interrupt").build();CompiledGraph<MessagesState<String>> workflow = graph.compile(cc);//运行完后,最终只会输出[have a] - node-3被打断,执行中止workflow.invoke(Map.of(), rc).ifPresent(state -> System.out.println(state.value(MESSAGES_STATE).orElse(null)));}static void recoverFromCheckpoint(BaseCheckpointSaver saver) throws Exception {StateGraph<MessagesState<String>> graph = getGraph();graph.addBeforeCallNodeHook((String node, MessagesState<String> data, RunnableConfig config) -> {out.println("Before calling node: " + node + ", data: " + data.data());return CompletableFuture.completedFuture(data.data());});CompileConfig cc = CompileConfig.builder().checkpointSaver(saver).interruptBefore("node-3").build();RunnableConfig rc = RunnableConfig.builder().threadId("test-interrupt").build();CompiledGraph<MessagesState<String>> workflow = graph.compile(cc);//取出interrupt前的状态快照StateSnapshot<MessagesState<String>> snapshot = workflow.getState(rc);System.out.println("snapshot=>" + snapshot.state().data());//将图的状态,更新到interrupt前的状态快照RunnableConfig runnableConfig = workflow.updateState(rc, snapshot.state().data());//从断点恢复运行workflow.invoke(GraphInput.resume(), runnableConfig).ifPresent(state -> System.out.println(state.value(MESSAGES_STATE).orElse(null)));}

17行这里,在打断时,同时设置了CheckPointSaver,这样就能在将打断时的状态保存起来。

接下来的recoverFromCheckpoint方法中,行47将先前保存的状态取出来,然后更新到图中(相当于恢复打断前的现场),最后54行继续从断点处运行。
串在一起测试下:
public static void main(String[] args) throws Exception {startWithoutCheckpoint();out.println("\n------------------------\n");BaseCheckpointSaver saver = getSaver();startWithCheckpoint(saver);out.println("\n------------------------\n");recoverFromCheckpoint(saver);
}

 

运行结果: 

Before calling node: node-1, data: {messages=[]}
Before calling node: node-2, data: {messages=[have]}
[have, a]------------------------Before calling node: node-1, data: {messages=[]}
Before calling node: node-2, data: {messages=[have]}
[have, a]------------------------snapshot=>{messages=[have, a]}
Before calling node: node-3, data: {messages=[have, a]}
Before calling node: node-4, data: {messages=[have, a, good]}
[have, a, good, trip]
13-16行的输出可以看出,node-3, node-4从断点处继续运行,直至结束。

  

文中源码:langgraph4j-study/src/main/java/org/bsc/langgraph4j/agent/_13_checkpoint at main · yjmyzz/langgraph4j-study · GitHub

http://www.jsqmd.com/news/425084/

相关文章:

  • 小学生KET备考大揭秘!这些网校让孩子轻松上岸 - 品牌测评鉴赏家
  • 留种率与选择强度的关系
  • 2026年密集型母线槽推荐,专注品质与项目落地能力 - 品牌鉴赏师
  • 基于深度学习图像分割的无人机洪水灾害图像分割检测与水量估算 洪水分割数据集 图像分割算法
  • LangGraph4j 学习系列(8)-checkpoint检查点
  • 2026小学生KET线上机构推荐|避开3大坑,新手家长直接抄作业 - 品牌测评鉴赏家
  • 2000—2024年应计盈余管理-修正的琼斯模型数据+Stata代码
  • 农历正月十四
  • 2026小学英语线上培训机构哪家好?宝妈抄作业不踩坑 - 品牌测评鉴赏家
  • 精选 5 款基于 .NET 开源的 Visual Studio 实用插件
  • Halcon助手实现相机标定
  • NMN哪个品牌好?从吸收方式到性价比的十大NMN品牌解析 - 资讯焦点
  • 《Linux系统编程》5.Linux基础开发工具(中)-makefile,进度条
  • 光甘草定为什么能够实现安全美白?附2026美白去黄水乳推荐 - 资讯焦点
  • P7687 [CEOI 2005] Critical Network Lines TJ
  • 贪心做题笔记
  • 32并发输出速度519.87t/s!四卡T10(Turing, sm75) Qwen3-27B-FP8 并发吞吐量测试
  • 小学剑桥原版线上英语课推荐|家长实测不踩坑,选课直接抄作业 - 品牌测评鉴赏家
  • 现在分词
  • 从技术到口碑,惠耳神逸助听器彰显国产硬核实力 - 资讯焦点
  • 初中英语提分难?6个宝藏学习平台实测推荐,覆盖同步、口语、冲刺全场景 - 品牌测评鉴赏家
  • 2026年特氟龙输送带TOP10厂商评测排名 - 资讯焦点
  • 口碑较好的小型冰箱排行榜——2026年非常值得入手的选项 - 资讯焦点
  • 2026实测|初中英语线上辅导机构哪家好?避坑不花冤枉钱,家长直接抄作业 - 品牌测评鉴赏家
  • 2026线上少儿英语培训班推荐|家长闭眼抄作业不踩坑 - 品牌测评鉴赏家
  • 揭秘 Qt 的底层黑魔法:元对象系统 (MOS) 与元对象编译器 (MOC) 深度解析
  • 2026年知名的振动温度传感器厂家行业热门推荐 - 品牌鉴赏师
  • 小学生剑桥英语选课指南,3大主流品牌实测,新手家长直接抄作业 - 品牌测评鉴赏家
  • 西门子PLC结构化编程_数值改变检测标准块
  • 初中生必看|3款剑桥原版线上英语课实测,告别无效刷题 - 品牌测评鉴赏家