当前位置: 首页 > news >正文

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

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

步骤 1: 添加依赖

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

<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的版本管理,例如:

<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模型配置类
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服务类
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服务:

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: 启动应用程序

确保启动类已经配置:

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
<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
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
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
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
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来调用该模型进行预测。

相关文章:

  • 《QT实用小工具·六十五》基于QPropertyAnimation实现的移动动画和控件覆盖
  • 13、Go Gin集成Viper配置
  • Typescript高级: 深入理解Extract类型
  • 2010-2022年各省新质生产力数据(含原始数据+测算代码+计算结果)
  • 【MySQL】库的操作和表的操作
  • Liunx系统中修改文件的创建时间以及访问时间
  • 汇编原理(二)
  • 为 AWS 子账户添加安全组修改权限
  • AI数据面临枯竭
  • 网络安全基础技术扫盲篇 — 名词解释
  • 粤嵌—2024/5/20—三角形最小路径和(✔)
  • 2024年5月19日优雅草蜻蜓K知识付费系统旗舰版v1.0.9进度更新
  • 开源大模型与闭源大模型
  • 服务器硬件基础知识
  • 5. C++网络编程-UDP协议的实现
  • 实验报告2-多线程并发
  • KuberSphere 安装kubernates
  • SVM兵王问题
  • Intel HDSLB 高性能四层负载均衡器 — 基本原理和部署配置
  • 设计模式八股文
  • “即买即退”扩容提质,上海静安推出离境退税2.0版新政
  • 解读|特朗普“助攻”下加拿大自由党“惨胜”,卡尼仍需克服“特鲁多阴影”
  • 烟花、美食和购物优惠都安排上了,上海多区开启热闹模式
  • 首映|“凤凰传奇”曾毅:拍电影,我是认真的
  • 新华每日电讯:从上海街区经济看账面、市面、人面、基本面
  • 古籍新书·2025年春季|中国土司制度史料集成