Logo
首页
前沿
AI热点新闻
36氦头条
博客
开源
极简简历
极简智库
瞬间
相册
友链
知识库
创作者
搜索文章 Ctrl K

Spring AI Alibaba

返回 0
  • 快速开始
  • 版本说明
  • 概览
  • 框架
    • Agent 框架
      • 进阶指南
        • 多智能体(Multi-agent)
        • 分布式智能体(A2A Agent)
        • 工作流(Workflow)
        • 记忆管理(Memory)
        • 检索增强生成(RAG)
        • 人工介入(Human-in-the-Loop)
        • 沙箱运行时(Sandbox)
        • 上下文工程(Context Engineering)
        • 智能体作为工具(Agent Tool)
      • 教程
        • Agents
        • Hooks 和 Interceptors
        • Memory 短期记忆
        • Messages 消息
        • Models 模型
        • Skills 技能
        • Structured Output 结构化输出
        • Tools 工具
    • Studio
      • 快速开始
    • Graph Core
      • Graph 工作流编排指南
      • 核心概念
        • 持久化
        • 核心概念
        • 流式输出
        • 内存管理
      • 示例
        • 并行节点定义
        • 并行流式输出
        • 持久化执行
        • 多智能体监督者模式
        • 分配MCP工具给指定节点
        • 人类反馈
        • 时光旅行 - 状态历史回溯
        • 为图添加持久化(记忆)
        • 子图 Subgraphs
        • 子图作为 CompiledGraph
        • 子图作为 StateGraph
        • 子图作为节点操作
        • Graph 执行取消
        • PlantUML 图表可视化
        • Redis 检查点持久化
        • Spring AI Alibaba LLM 流式集成

子图作为节点操作

在 Spring AI Alibaba Graph 中,可以将一个完整的 Graph 作为另一个 Graph 的节点,实现工作流的模块化设计。

概念

将子图作为 NodeAction 使用,可以:

  • 模块化: 将复杂工作流拆分为可重用的子模块

  • 封装: 隐藏子图的内部实现细节

  • 组合: 构建层次化的工作流结构

基本示例

定义子图

import com.alibaba.cloud.ai.graph.CompiledGraph;

import com.alibaba.cloud.ai.graph.KeyStrategy;

import com.alibaba.cloud.ai.graph.KeyStrategyFactory;

import com.alibaba.cloud.ai.graph.StateGraph;

import com.alibaba.cloud.ai.graph.exception.GraphStateException;

import com.alibaba.cloud.ai.graph.state.strategy.ReplaceStrategy;



import java.util.HashMap;

import java.util.Map;



import static com.alibaba.cloud.ai.graph.StateGraph.END;

import static com.alibaba.cloud.ai.graph.StateGraph.START;

import static com.alibaba.cloud.ai.graph.action.AsyncNodeAction.node_async;



/**

 * 定义子图

 */

public static CompiledGraph createSubGraph(KeyStrategyFactory keyStrategyFactory) throws GraphStateException {

    StateGraph subGraph = new StateGraph(keyStrategyFactory)

            .addNode("substep1", node_async(state -> {

            String input = (String) state.value("input").orElse("");

            return Map.of("result", "SubStep1:" + input);

        }))

            .addNode("substep2", node_async(state -> {

            String prev = (String) state.value("result").orElse("");

            return Map.of("result", prev + "->SubStep2");

        }))

            .addEdge(START, "substep1")

        .addEdge("substep1", "substep2")

            .addEdge("substep2", END);



    return subGraph.compile();

}

将子图包装为 NodeAction

import com.alibaba.cloud.ai.graph.CompiledGraph;

import com.alibaba.cloud.ai.graph.OverAllState;

import com.alibaba.cloud.ai.graph.RunnableConfig;

import com.alibaba.cloud.ai.graph.action.NodeAction;



import java.util.Map;

import java.util.Optional;



/**

 * 将子图包装为 NodeAction

 */

public static class SubGraphNode implements NodeAction {



    private final CompiledGraph subGraph;



    public SubGraphNode(CompiledGraph subGraph) {

        this.subGraph = subGraph;

    }



    @Override

    public Map<String, Object> apply(OverAllState state) {

        // 从父状态提取子图需要的数据

        String input = (String) state.value("data").orElse("");



        // 执行子图

        Map<String, Object> subInput = Map.of("input", input);

        Optional<OverAllState> subResult = subGraph.invoke(subInput, RunnableConfig.builder().build());



        // 返回结果给父图

        String result = (String) subResult.get().value("result").orElse("");

        return Map.of("processed", result);

    }

}

在父图中使用

import com.alibaba.cloud.ai.graph.CompiledGraph;

import com.alibaba.cloud.ai.graph.StateGraph;



import static com.alibaba.cloud.ai.graph.StateGraph.END;

import static com.alibaba.cloud.ai.graph.StateGraph.START;

import static com.alibaba.cloud.ai.graph.action.AsyncNodeAction.node_async;



/**

 * 在父图中使用

 */

public static CompiledGraph useInParentGraph(KeyStrategyFactory keyStrategyFactory, CompiledGraph subGraph) throws GraphStateException {

SubGraphNode subGraphNode = new SubGraphNode(subGraph);



StateGraph parentGraph = new StateGraph(keyStrategyFactory)

            .addNode("prepare", node_async(state -> {

        return Map.of("data", "Input Data");

    }))

            .addNode("process", node_async(subGraphNode))  // 使用子图作为节点

            .addNode("finalize", node_async(state -> {

        String processed = (String) state.value("processed").orElse("");

        return Map.of("final", "Final:" + processed);

    }))

            .addEdge(START, "prepare")

    .addEdge("prepare", "process")

    .addEdge("process", "finalize")

            .addEdge("finalize", END);



    return parentGraph.compile();

}

执行父图

// 执行

OverAllState result = compiledParent.invoke(Map.of());

System.out.println("Final result: " + result.value("final").orElse(""));



// 输出: Final result: Final:SubStep1:Input Data->SubStep2

高级示例 - 带参数的子图

// 可配置的子图节点

public class ConfigurableSubGraphNode implements NodeAction {



    private final CompiledGraph subGraph;

    private final String inputKey;

    private final String outputKey;



    public ConfigurableSubGraphNode(

        CompiledGraph subGraph,

        String inputKey,

        String outputKey

    ) {

        this.subGraph = subGraph;

        this.inputKey = inputKey;

        this.outputKey = outputKey;

    }



    @Override

    public Map<String, Object> apply(OverAllState state) {

        // 从父状态读取指定键的数据

        Object input = state.value(inputKey).orElse(null);



        // 执行子图

        OverAllState subResult = subGraph.invoke(Map.of("input", input));



        // 将结果写入指定键

        Object output = subResult.value("result").orElse(null);

        return Map.of(outputKey, output);

    }

}



// 使用

CompiledGraph subGraph = createSubGraph(keyStrategyFactory);

NodeAction configurableNode = new ConfigurableSubGraphNode(

    subGraph,

    "userInput",    // 从父状态读取 userInput

    "processedOutput"  // 写入 processedOutput

);

状态映射

处理父子图之间的状态映射:

public class SubGraphNodeWithMapping implements NodeAction {



    private final CompiledGraph subGraph;

    private final Function<OverAllState, Map<String, Object>> inputMapper;

    private final BiFunction<OverAllState, OverAllState, Map<String, Object>> outputMapper;



    public SubGraphNodeWithMapping(

        CompiledGraph subGraph,

        Function<OverAllState, Map<String, Object>> inputMapper,

        BiFunction<OverAllState, OverAllState, Map<String, Object>> outputMapper

    ) {

        this.subGraph = subGraph;

        this.inputMapper = inputMapper;

        this.outputMapper = outputMapper;

    }



    @Override

    public Map<String, Object> apply(OverAllState parentState) {

        // 使用 mapper 转换父状态到子状态

        Map<String, Object> subInput = inputMapper.apply(parentState);



        // 执行子图

        OverAllState subResult = subGraph.invoke(subInput);



        // 使用 mapper 转换子状态到父状态

        return outputMapper.apply(parentState, subResult);

    }

}

应用场景

  1. 数据处理管道: 将数据清洗、转换、验证等步骤封装为独立子图

  2. 微服务编排: 每个微服务调用封装为子图

  3. 条件处理: 根据不同条件选择不同的子图执行

  4. 并行处理: 多个子图并行执行不同任务

  5. 版本控制: 不同版本的处理逻辑封装为不同子图

附图

png

相关文档

  • 子图作为 StateGraph - 另一种子图集成方式

  • 子图作为 CompiledGraph - 编译后的子图使用

  • 快速入门 - Graph 基础使用

目录
    共 50 篇 Spring AI Alibaba / 子图作为节点操作 0 字 0 行 cosolar
    创建 2026-09-13T09:33:00.084834478Z 更新 2026-11-27T02:05:05.979937890Z
    Logo

    以极简为笔,以求索为墨。 简单书写,深度思考,每一次记录,都是与内心的对话。

    30,291 访问量
    219 点赞数
    0 评论数
    162 文章数 13 分类数 243 标签数
    2026-06-06 建站时间
    中国·北京 博主地址

    订阅更新

    订阅我们的技术文章,每周精选内容送达你的邮箱

    隐私政策 | 使用条款 | 友情链接 | 关于我们
    本主题由 腾讯云 提供云服务
    © 极简笔记 版权所有 津ICP备2026156702号-1
    Powered by Halo Theme halo-theme-cosolar