首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >LangGraph4j 学习系列(3)-循环工作流

LangGraph4j 学习系列(3)-循环工作流

作者头像
菩提树下的杨过
发布2026-03-02 09:54:45
发布2026-03-02 09:54:45
590
举报

上节继续,本节将演示条件工作流如何用langgraph4j实现。

image
image

注:循环工作流可以看成 条件工作流的一个变种。node1 -> node2 -> node1 这样就形成了1个死循环(loop),为了能跳出死循环,用条件边来判定跳出时机。

一、定义节点

代码语言:javascript
复制
public class Node1Action implements NodeAction<AgentState> {
    @Override
    public Map<String, Object> apply(AgentState state) throws Exception {
        System.out.println("current Node: node-1");
        return Map.of("myData", "node1-my-value",
                "node1Key", "node1-value");
    }
}
代码语言:javascript
复制
public class Node2Action implements NodeAction<AgentState> {
    @Override
    public Map<String, Object> apply(AgentState state) throws Exception {
        System.out.println("current Node: node-2");
        return Map.of("myData", "node2-my-value",
                "node2Key", "node2-value");
    }
}

二、完整示例

代码语言:javascript
复制
public class LoopGraphApplication {

    public static void main(String[] args) throws GraphStateException {
        StateGraph<AgentState> sequenceGraph = getLoopGraph();
        System.out.println(sequenceGraph.getGraph(GraphRepresentation.Type.MERMAID, "loop Graph", true).content());
        sequenceGraph.compile().invoke(Map.of("loopCount", 0L)).ifPresent(c -> {
            System.out.println(c.data());
        });
    }

    private static final int MAX_LOOP_ITERATIONS = 3;

    public static StateGraph<AgentState> getLoopGraph() throws GraphStateException {
        return new StateGraph<>(AgentState::new)
                .addNode("node-1", node_async(new Node1Action()))
                .addNode("node-2", node_async(new Node2Action()))
                .addEdge(GraphDefinition.START, "node-1")
                .addEdge("node-2", "node-1")
                //循环的跳出条件比较简单,3次后退出,这里就不单独定义EdgeAction类了,用lambda表达式
                .addConditionalEdges("node-1", state -> {
                    long count = getLoopCount(state);
                    System.out.println("loop Count: " + count);
                    if (count >= MAX_LOOP_ITERATIONS) {
                        return CompletableFuture.completedFuture("exit");
                    }
                    return CompletableFuture.completedFuture("continue");
                }, Map.of(
                        "exit", GraphDefinition.END,
                        "continue", "node-2"));
    }

    private static long getLoopCount(AgentState state) {
        Optional<Object> loopCount = state.value("loopCount");
        if (loopCount.isEmpty()) {
            return 0L;
        }
        Object v = loopCount.get();
        if (v instanceof Number n) {
            return n.longValue();
        }
        return Long.parseLong(v.toString());
    }


}

三、运行结果

代码语言:javascript
复制
current Node: node-1
loop Count: 0
current Node: node-2
current Node: node-1
loop Count: 1
current Node: node-2
current Node: node-1
loop Count: 2
current Node: node-2
current Node: node-1
loop Count: 3
{node1Key=node1-value, loopCount=3, node2Key=node2-value, myData=node1-my-value}

文中源码:https://github.com/yjmyzz/langgraph4j-study/tree/main/src/main/java/org/bsc/langgraph4j/agent/_08_loop

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-03-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档