找回密码
 立即注册
首页 业界区 安全 Spring AI Alibaba 项目源码学习(一)-整体介绍 ...

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

敕码 5 小时前
Spring AI Alibaba 项目目录结构说明

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

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

第一层模块

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

1. spring-ai-alibaba-bom

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


  • 提供依赖版本管理
  • 简化子模块的依赖配置
  • 确保版本一致性
使用方式
  1. <dependencyManagement>
  2.     <dependencies>
  3.         <dependency>
  4.             <groupId>com.alibaba.cloud.ai</groupId>
  5.             spring-ai-alibaba-bom</artifactId>
  6.             <version>1.1.0.0-M4</version>
  7.             <type>pom</type>
  8.             <scope>import</scope>
  9.         </dependency>
  10.     </dependencies>
  11. </dependencyManagement>
复制代码
2. spring-ai-alibaba-graph-core

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


  • StateGraph:状态图定义类,用于构建工作流图
    1. public class StateGraph {
    2.         /**
    3.          * Constant representing the END of the graph.
    4.          */
    5.         public static final String END = "__END__";
    6.         /**
    7.          * Constant representing the START of the graph.
    8.          */
    9.         public static final String START = "__START__";
    10.         /**
    11.          * Constant representing the ERROR of the graph.
    12.          */
    13.         public static final String ERROR = "__ERROR__";
    14.         /**
    15.          * Constant representing the NODE_BEFORE of the graph.
    16.          */
    17.         public static final String NODE_BEFORE = "__NODE_BEFORE__";
    复制代码

  • GraphRunner:图执行引擎,负责执行编译后的图
    1. public class GraphRunner {
    2.         private final CompiledGraph compiledGraph;
    3.         private final RunnableConfig config;
    4.         private final AtomicReference<Object> resultValue = new AtomicReference<>();
    5.         // Handler for main execution flow - demonstrates encapsulation
    6.         private final MainGraphExecutor mainGraphExecutor;
    7.         public GraphRunner(CompiledGraph compiledGraph, RunnableConfig config) {
    8.                 this.compiledGraph = compiledGraph;
    9.                 this.config = config;
    10.                 // Initialize the main execution handler - demonstrates encapsulation
    11.                 this.mainGraphExecutor = new MainGraphExecutor();
    12.         }
    13.         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)范式
[code]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

相关推荐

您需要登录后才可以回帖 登录 | 立即注册