敕码 发表于 2025-11-10 01:00:10

Spring AI Alibaba 项目源码学习(一)-整体介绍

Spring AI Alibaba 项目目录结构说明

请关注微信公众号:阿呆-bot
项目概述

Spring AI Alibaba 是一个多模块 Maven 项目,采用分层架构设计,从底层到上层依次为:Graph 核心运行时、Agent 框架、Studio 应用和 Spring Boot Starters。项目遵循模块化设计原则,每个模块都有明确的职责边界。
目录结构说明

第一层模块

项目根目录下包含以下主要模块:
spring-ai-alibaba/
├── spring-ai-alibaba-bom/            # BOM 模块
├── spring-ai-alibaba-graph-core/       # Graph 核心运行时
├── spring-ai-alibaba-agent-framework/ # Agent 框架
├── spring-ai-alibaba-studio/         # Studio 应用
├── spring-boot-starters/               # Spring Boot Starters
│   ├── spring-ai-alibaba-starter-a2a-nacos/
│   └── spring-ai-alibaba-starter-config-nacos/
├── tools/                              # 工具目录
└── docs/                               # 文档目录 模块详细说明

1. spring-ai-alibaba-bom

职责:Bill of Materials (BOM) 模块,统一管理所有 Spring AI Alibaba 模块的依赖版本。
关键特性:


[*]提供依赖版本管理
[*]简化子模块的依赖配置
[*]确保版本一致性
使用方式:
<dependencyManagement>
    <dependencies>
      <dependency>
            <groupId>com.alibaba.cloud.ai</groupId>
            spring-ai-alibaba-bom</artifactId>
            <version>1.1.0.0-M4</version>
            <type>pom</type>
            <scope>import</scope>
      </dependency>
    </dependencies>
</dependencyManagement> 2. spring-ai-alibaba-graph-core

职责:Graph 核心运行时,提供底层的工作流和多 Agent 编排框架。这是整个框架的基础层,提供状态图、节点、边等核心抽象。
关键入口点:


[*] StateGraph:状态图定义类,用于构建工作流图
public class StateGraph {

        /**
       * Constant representing the END of the graph.
       */
        public static final String END = "__END__";

        /**
       * Constant representing the START of the graph.
       */
        public static final String START = "__START__";

        /**
       * Constant representing the ERROR of the graph.
       */
        public static final String ERROR = "__ERROR__";

        /**
       * Constant representing the NODE_BEFORE of the graph.
       */
        public static final String NODE_BEFORE = "__NODE_BEFORE__";
[*] GraphRunner:图执行引擎,负责执行编译后的图
public class GraphRunner {

        private final CompiledGraph compiledGraph;

        private final RunnableConfig config;

        private final AtomicReference<Object> resultValue = new AtomicReference<>();

        // Handler for main execution flow - demonstrates encapsulation
        private final MainGraphExecutor mainGraphExecutor;

        public GraphRunner(CompiledGraph compiledGraph, RunnableConfig config) {
                this.compiledGraph = compiledGraph;
                this.config = config;
                // Initialize the main execution handler - demonstrates encapsulation
                this.mainGraphExecutor = new MainGraphExecutor();
        }

        public Flux<GraphResponse<NodeOutput>> run(OverAllState initialState) {
[*] CompiledGraph:编译后的可执行图

[*] OverAllState:全局状态对象,用于在节点间传递数据

核心概念:


[*]Node:工作流中的节点,封装特定操作或模型调用
[*]Edge:节点间的边,表示状态转换
[*]OverAllState:全局状态,携带整个流程的共享数据
3. spring-ai-alibaba-agent-framework

职责:Agent 框架,基于 Graph 核心运行时构建的高级 Agent 开发框架。提供 ReactAgent、SequentialAgent、ParallelAgent 等预构建的 Agent 类型,简化 Agent 开发。
关键入口点:
<ul>ReactAgent:核心 Agent 类型,实现 ReAct(Reasoning + Acting)范式
public class ReactAgent extends BaseAgent {         private static final int DEFAULT_MAX_ITERATIONS = 10;        private final AgentLlmNode llmNode;        private final AgentToolNode toolNode;        private CompiledGraph compiledGraph;        private List hooks;        private List modelInterceptors;        private List toolInterceptors;        private int maxIterations;        private int iterations = 0;        private String instruction;        private Function shouldContinueFunc;        public ReactAgent(AgentLlmNode llmNode, AgentToolNode toolNode, CompileConfig compileConfig, Builder builder) throws GraphStateException {                 super(builder.name, builder.description, builder.includeContents, builder.returnReasoningContents, builder.outputKey, builder.outputKeyStrategy);                 this.instruction = builder.instruction;                 this.llmNode = llmNode;                 this.toolNode = toolNode;                 this.compileConfig = compileConfig;                 this.shouldContinueFunc = builder.shouldContinueFunc;                 this.hooks = builder.hooks;                 this.modelInterceptors = builder.modelInterceptors;                 this.toolInterceptors = builder.toolInterceptors;                 this.includeContents = builder.includeContents;                 this.inputSchema = builder.inputSchema;                 this.inputType = builder.inputType;                 this.outputSchema = builder.outputSchema;                 this.outputType = builder.outputType;                 this.maxIterations = builder.maxIterations

替攀浮 发表于 前天 06:39

感谢分享
页: [1]
查看完整版本: Spring AI Alibaba 项目源码学习(一)-整体介绍