【Spring Boot】在项目中使用Spring AI

Spring AI是Spring框架中用于集成和使用人工智能和机器学习功能的组件。它提供了一种简化的方式来与AI模型进行交互。下面是一个简单的示例,展示了如何在Spring Boot项目中使用Spring AI。

步骤 1: 添加依赖

首先,在pom.xml文件中添加Spring AI的依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ai</artifactId>
    <version>0.1.0</version>
</dependency>

确保配置了Spring Cloud的版本管理,例如:

xml 复制代码
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

步骤 2: 创建一个AI模型服务

创建一个服务来使用AI模型。这可以是一个简单的Spring服务类。以下是一个示例,展示了如何使用Spring AI来预测数据:

创建一个AI模型配置类
java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.cloud.ai.annotation.EnableAi;

@Configuration
@EnableAi
public class AiModelConfig {

    @Bean
    public AiModel myAiModel() {
        return new AiModel("my-model");
    }
}
创建一个AI服务类
java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.ai.annotation.AiModel;
import org.springframework.stereotype.Service;

@Service
public class AiService {

    @Autowired
    private AiModel aiModel;

    public String predict(String input) {
        return aiModel.predict(input);
    }
}

步骤 3: 创建一个控制器来使用AI服务

创建一个Spring MVC控制器,来调用AI服务:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AiController {

    @Autowired
    private AiService aiService;

    @GetMapping("/predict")
    public String predict(@RequestParam String input) {
        return aiService.predict(input);
    }
}

步骤 4: 启动应用程序

确保启动类已经配置:

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AiApplication {
    public static void main(String[] args) {
        SpringApplication.run(AiApplication.class, args);
    }
}

全部代码示例

整合以上所有部分,完整的代码示例如下:

pom.xml
xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ai</artifactId>
        <version>0.1.0</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR9</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
AiApplication.java
java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AiApplication {
    public static void main(String[] args) {
        SpringApplication.run(AiApplication.class, args);
    }
}
AiModelConfig.java
java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.cloud.ai.annotation.EnableAi;

@Configuration
@EnableAi
public class AiModelConfig {

    @Bean
    public AiModel myAiModel() {
        return new AiModel("my-model");
    }
}
AiService.java
java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.ai.annotation.AiModel;
import org.springframework.stereotype.Service;

@Service
public class AiService {

    @Autowired
    private AiModel aiModel;

    public String predict(String input) {
        return aiModel.predict(input);
    }
}
AiController.java
java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AiController {

    @Autowired
    private AiService aiService;

    @GetMapping("/predict")
    public String predict(@RequestParam String input) {
        return aiService.predict(input);
    }
}

以上就完成了一个简单的Spring AI集成示例。这个示例展示了如何配置和使用一个AI模型,并通过REST API来调用该模型进行预测。

相关推荐
程序边界3 分钟前
全球人工智能技术大会(GAITC 2025):技术前沿与产业融合的深度交响
人工智能
OpenCSG10 分钟前
电子行业AI赋能软件开发经典案例——某金融软件公司
人工智能·算法·金融·开源
新加坡内哥谈技术16 分钟前
极客时间:在 Google Colab 上尝试 Prefix Tuning
人工智能
今天又学了啥22 分钟前
李飞飞World Labs开源革命性Web端3D渲染器Forge!3D高斯溅射技术首次实现全平台流畅运行
人工智能
极智视界32 分钟前
分类场景数据集大全「包含数据标注+训练脚本」 (持续原地更新)
人工智能·yolo·数据集·分类算法·数据标注·classification·分类数据集
翻滚的小@强1 小时前
自动驾驶科普(百度Apollo)学习笔记
人工智能·自动驾驶·百度apollo
从零开始学习人工智能1 小时前
从游戏到自动驾驶:互联网时代强化学习如何让机器学会自主决策?
人工智能·游戏·自动驾驶
幼稚园的山代王1 小时前
Prompt Enginering(提示工程)先进技术
java·人工智能·ai·chatgpt·langchain·prompt
dfsj660111 小时前
LLMs 系列科普文(14)
人工智能·深度学习·算法
摘取一颗天上星️1 小时前
深入解析机器学习的心脏:损失函数及其背后的奥秘
人工智能·深度学习·机器学习·损失函数·梯度下降