news 2026/6/16 11:21:00

langgraph4j笔记

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
langgraph4j笔记

文章目录

        • 引入依赖
        • 定义状态类
        • 定义controller
        • 检查点 todo

版本:
jdk17
springboot # 3.3.5
langchain4j 0.34.0
langgraph4j # 1.8.17

引入依赖

langchain4j的依赖也是需要的,在此不赘述。

属性标签:<langgraph4j.version>1.8.17</langgraph4j.version><dependency><groupId>org.bsc.langgraph4j</groupId><artifactId>langgraph4j-core</artifactId><version>${langgraph4j.version}</version></dependency>
定义状态类
importorg.bsc.langgraph4j.state.AgentState;importjava.util.Map;publicclassMyCustomStateextendsAgentState{publicMyCustomState(Map<String,Object>initData){super(initData);}// 封装获取用户输入的方法,避免类型转换publicStringgetUserInput(){returnthis.<String>value("userInput").orElse("");}// 封装获取AI回复的方法publicStringgetAiResponse(){returnthis.<String>value("aiResponse").orElse("");}}
定义controller

run方法里面定义了一个langGraph,共两个节点,运行时会按照这个步骤走。

importcom.example.demo.entity.MyCustomState;importdev.langchain4j.model.chat.ChatLanguageModel;importorg.bsc.langgraph4j.CompiledGraph;importorg.bsc.langgraph4j.StateGraph;// 1. 导入异步节点动作接口importorg.bsc.langgraph4j.action.AsyncNodeAction;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importjava.util.Map;importjava.util.Optional;importstaticorg.bsc.langgraph4j.GraphDefinition.START;importstaticorg.bsc.langgraph4j.GraphDefinition.END;// 2. 导入 node_async 静态方法,用于包装同步逻辑importstaticorg.bsc.langgraph4j.action.AsyncNodeAction.node_async;@RestController@RequestMapping("/api")publicclassLangGraph4jController{privatefinalChatLanguageModelchatLanguageModel;publicLangGraph4jController(ChatLanguageModelchatLanguageModel){this.chatLanguageModel=chatLanguageModel;}@GetMapping("/langgraph4j/run")publicStringrunGraph(@RequestParamStringmessage)throwsException{// 3. 使用 AsyncNodeAction 并配合 node_async 包装器AsyncNodeAction<MyCustomState>aiNode=node_async(state->{StringuserInput=state.value("userInput").map(Object::toString).orElse("");Stringresponse=chatLanguageModel.generate("请简短回答:"+userInput);returnMap.of("aiResponse",response);});AsyncNodeAction<MyCustomState>formatNode=node_async(state->{StringaiResponse=state.value("aiResponse").map(Object::toString).orElse("");StringfinalResult=String.format("[格式化结果]: AI的最终回复是 -> %s",aiResponse);returnMap.of("aiResponse",finalResult);});// 4. 构建图StateGraph<MyCustomState>workflow=newStateGraph<>(MyCustomState::new).addNode("ai_processor",aiNode).addNode("formatter",formatNode).addEdge(START,"ai_processor").addEdge("ai_processor","formatter").addEdge("formatter",END);// 5. 编译并执行CompiledGraph<MyCustomState>graph=workflow.compile();Map<String,Object>initialState=Map.of("userInput",message);Optional<MyCustomState>finalStateOpt=graph.invoke(initialState);MyCustomStatefinalState=finalStateOpt.orElseThrow();returnfinalState.getAiResponse();}}
检查点 todo
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/16 11:19:50

高层次综合C语言库设计

一、常见库和库的作用二、hls_stream.h&#xff1a;硬件数据流的灵魂 #include <hls_stream.h>hls::stream<data_t> ch("fifo_name"); ch.write(val); // 阻塞写 data_t v ch.read(); // 阻塞读 bool empty ch.empty(); // 检查空满 bool…

作者头像 李华
网站建设 2026/6/16 11:14:04

AI时代的一人公司实操手册:从需求到App上线的72小时闭环

1. 项目概述&#xff1a;当“一人公司”不再是个概念&#xff0c;而是一份可拆解的实操手册“5个月用AI做了120多个App&#xff0c;职高毕业的小伙在杭州上城开‘一人公司’火到了海外”——这个标题不是短视频平台的夸张封面&#xff0c;而是我上个月在杭州城东一家共享办公空…

作者头像 李华
网站建设 2026/6/16 11:03:55

Ubuntu binary镜像:开源系统交付的确定性与信任基石

1. 项目概述&#xff1a;这不是一个普通镜像&#xff0c;而是一份“开箱即用”的系统交付契约 “Ubuntu (binary)”这个标题乍看平淡无奇&#xff0c;甚至有点让人困惑——它不像“Ubuntu 24.04 LTS 桌面版”那样具象&#xff0c;也不像“Ubuntu Server ARM64 镜像”那样指向明…

作者头像 李华
网站建设 2026/6/16 11:00:06

Ubuntu启用root账户的四重安全加固指南

1. 为什么这个操作既常见又危险——从Ubuntu设计哲学说起刚接触Ubuntu的朋友常会问&#xff1a;“我装完系统&#xff0c;root密码是多少&#xff1f;”答案是&#xff1a;没有。Ubuntu默认禁用root账户&#xff0c;所有管理操作都通过sudo完成。这不是技术缺陷&#xff0c;而是…

作者头像 李华
网站建设 2026/6/16 10:55:54

Ubuntu 18.04深度学习驱动安装避坑指南:NVIDIA 418.56稳定实践

1. 为什么在Ubuntu 18.04上装NVIDIA驱动是深度学习入门的第一道硬门槛刚接触深度学习的朋友&#xff0c;十有八九卡在第一步&#xff1a;显卡驱动装不上。不是黑屏进不去系统&#xff0c;就是nvidia-smi报“NVIDIA-SMI has failed because it couldn’t communicate with the N…

作者头像 李华