Spring AI 作为 Spring 生态的一部分,能够帮助开发者在 Spring Boot 项目中快速集成 AI 功能。以下是如何通过 Spring Initializr 创建一个 Spring Boot 项目,并添加 Spring AI 相关依赖的步骤。

步骤 1:通过 Spring Initializr 创建项目

  1. 打开 Spring Initializr
    访问 Spring Initializr 的网站:
    https://start.spring.io/

  2. 选择项目设置

    • 项目构建工具:选择 MavenGradle(推荐选择 Maven)
    • Spring Boot 版本:选择一个稳定的 Spring Boot 版本(建议选择 2.x 或 3.x 版本)
    • 项目元数据
      • Group:输入组织名称(例如:com.example
      • Artifact:输入项目名称(例如:spring-ai-demo
      • Name:项目名称(与 Artifact 一致)
      • Description:简短描述(例如:Spring AI Demo Project
      • Package name:默认为 com.example.springaidemo
      • Packaging:选择 Jar
      • Java Version:选择你使用的 Java 版本(例如:17
  3. 添加依赖
    在 Dependencies 栏中,点击 Add Dependencies 并选择以下依赖:

    • Spring Web:用于创建 REST API 服务
    • Spring Boot DevTools:方便开发过程中的热部署
    • Spring AI(如果在可选依赖中没有显示,选择其他相关 AI 依赖)
  4. 生成项目
    点击 Generate 按钮,下载生成的项目压缩包。

  5. 解压并导入到 IDE
    解压下载的项目文件,并将项目导入到 IDE(如 IntelliJ IDEA 或 Eclipse)中。

步骤 2:添加 Spring AI 依赖

  1. 打开 pom.xml 文件
    打开 Maven 项目中的 pom.xml 文件,添加 Spring AI 相关依赖。

  2. 添加 Spring AI 依赖
    dependencies 部分添加如下依赖:

    <dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <!-- Spring Boot DevTools for hot reloading -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
    
        <!-- Spring AI -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter</artifactId>
            <version>最新版本</version>
        </dependency>
    </dependencies>
    

    请注意,spring-ai-starter 是 Spring AI 的核心启动器依赖。确保你使用的是最新版本。你可以在 Maven 仓库中查找最新版本。

  3. 刷新 Maven 依赖
    在 IDE 中,右击项目根目录并选择 Maven > Reimport(或类似操作)来下载并应用依赖。

步骤 3:配置 Spring AI

  1. 创建配置类
    在项目的 src/main/java/com/example/springaidemo 目录下,创建一个新的配置类 AiConfig.java,并配置 Spring AI 的相关设置。

    package com.example.springaidemo;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.ai.model.Model;
    import org.springframework.ai.model.ModelService;
    import org.springframework.ai.model.ai.ModelFactory;
    
    @Configuration
    public class AiConfig {
    
        @Bean
        public Model model() {
            // 配置一个简单的 AI 模型
            return ModelFactory.loadModel("path/to/your/model");
        }
    
        @Bean
        public ModelService modelService(Model model) {
            // 提供对 AI 模型的访问服务
            return new ModelService(model);
        }
    }
    

    在上述代码中:

    • ModelFactory.loadModel():用于加载 AI 模型,您可以根据需要替换成合适的模型路径。
    • ModelService:提供对 AI 模型的访问,用于在应用中调用模型。
  2. 配置 AI 模型路径
    如果您有自己的 AI 模型,可以通过设置 path/to/your/model 来加载它。或者,可以使用预训练的模型。

步骤 4:编写 AI 服务代码

  1. 创建 AI 控制器
    创建一个新的 REST 控制器类 AiController.java,用于暴露 AI 功能的 API 接口。

    package com.example.springaidemo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.ai.model.ModelService;
    
    @RestController
    public class AiController {
    
        @Autowired
        private ModelService modelService;
    
        @GetMapping("/predict")
        public String predict() {
            // 调用模型进行推理(此处仅为示例,具体使用模型方式取决于模型类型)
            String prediction = modelService.predict("输入数据");
            return "Prediction: " + prediction;
        }
    }
    

    在此示例中,/predict 接口将会调用模型并返回预测结果。modelService.predict() 是一个示例方法,具体的预测方法取决于您使用的 AI 模型。

  2. 启动应用

    • 启动 Spring Boot 应用(使用 mvn spring-boot:run 或在 IDE 中直接运行应用)。
    • 访问 http://localhost:8080/predict 查看模型的推理结果。

步骤 5:测试与优化

  1. 测试 REST API

    • 使用 Postman 或浏览器访问 http://localhost:8080/predict,检查模型预测是否正确返回。
  2. 调整模型参数
    根据实际需要,您可以调整模型加载的方式,或者优化推理的性能(如增加并发处理、使用 GPU 加速等)。

  3. 查看日志与调试

    • Spring Boot 的日志系统会帮助您调试应用。在 IDE 中查看控制台日志,或者使用 Spring Boot 提供的 Actuator 模块来监控应用。

总结

通过 Spring Initializr 创建 Spring Boot 项目,并集成 Spring AI 后,您就能够快速搭建一个 AI 应用。通过配置和编写控制器代码,您可以实现 AI 模型的调用与部署。Spring AI 将 Spring 框架的高效与灵活性扩展到了 AI 领域,让开发者可以在熟悉的环境中轻松实现机器学习和人工智能的集成。

Logo

脑启社区是一个专注类脑智能领域的开发者社区。欢迎加入社区,共建类脑智能生态。社区为开发者提供了丰富的开源类脑工具软件、类脑算法模型及数据集、类脑知识库、类脑技术培训课程以及类脑应用案例等资源。

更多推荐