当前位置: 首页 > 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 {@Beanpublic 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 {@Autowiredprivate 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 {@Autowiredprivate 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 {@Beanpublic 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 {@Autowiredprivate 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 {@Autowiredprivate AiService aiService;@GetMapping("/predict")public String predict(@RequestParam String input) {return aiService.predict(input);}
}

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

相关文章:

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

Spring AI是Spring框架中用于集成和使用人工智能和机器学习功能的组件。它提供了一种简化的方式来与AI模型进行交互。下面是一个简单的示例&#xff0c;展示了如何在Spring Boot项目中使用Spring AI。 步骤 1: 添加依赖 首先&#xff0c;在pom.xml文件中添加Spring AI的依赖&…...

【java程序设计期末复习】chapter3 运算符、表达式和语句

运算符、表达式和语句 Java提供了丰富的运算符&#xff0c;如算术运算符、关系运算符、逻辑运算符、位运算符等。 Java语言中的绝大多数运算符和C语言相同&#xff0c;基本语句&#xff0c;如条件分支语句、循环语句等也和C语言类似&#xff0c;因此&#xff0c;本章就主要知识…...

【建议收藏】30个较难Python脚本,纯干货分享

本篇较难&#xff0c;建议优先学习上篇 &#xff1b;20个硬核Python脚本-CSDN博客 接上篇文章&#xff0c;对于Pyhon的学习&#xff0c;上篇学习的结束相信大家对于Pyhon有了一定的理解和经验&#xff0c;学习完上篇文章之后再研究研究剩下的30个脚本你将会有所成就&…...

01-05.Vue自定义过滤器

目录 前言过滤器的概念过滤器的基本使用给过滤器添加多个参数 前言 我们接着上一篇文章01-04.Vue的使用示例&#xff1a;列表功能 来讲。 下一篇文章 02-Vue实例的生命周期函数 过滤器的概念 概念&#xff1a;Vue.js 允许我们自定义过滤器&#xff0c;可被用作一些常见的文本…...

C++系列-static成员

&#x1f308;个人主页&#xff1a;羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” 概念 声明为static的类成员称为类的静态成员&#xff0c;用static修饰的成员变量&#xff0c;称之为静态成员变量&#xff0c;用static修饰的成员函数&#xff0c;称之为静态成…...

Git | 创建和管理Pull Request总结

如是我闻&#xff1a; 在使用 GitHub 进行项目协作时&#xff0c;掌握如何创建、更新和合并&#xff08;squash&#xff09;pull request 是非常有帮助的。本文将详细介绍这些操作&#xff0c;帮助我们更好地管理项目代码&#xff0c;并解释每个操作的原因和解决的问题。 1. 什…...

电机控制系列模块解析(23)—— 同步机初始位置辨识

一、两个常见问题 为什么感应电机&#xff08;异步机&#xff09;不需要初始位置辨识&#xff1f;&#xff08;因此感应电机转子磁场在定子侧进行励磁&#xff0c;其初始位置可以始终人为定义为0&#xff09; 为什么同步磁阻电机需要初始位置辨识&#xff1f;&#xff08;因为…...

【数据库基础-mysql详解之索引的魅力(N叉树)】

索引的魅力目录 &#x1f308;索引的概念&#x1f308;使用场景&#x1f308;索引的使用&#x1f31e;&#x1f31e;&#x1f31e;查看MySQL中的默认索引&#x1f31e;&#x1f31e;&#x1f31e;创建索引&#x1f31e;&#x1f31e;&#x1f31e;删除索引 站在索引背后的那个男…...

力扣739. 每日温度

Problem: 739. 每日温度 文章目录 题目描述思路复杂度Code 题目描述 思路 若本题目使用暴力法则会超时&#xff0c;故而使用单调栈解决&#xff1a; 1.创建结果数组res&#xff0c;和单调栈stack&#xff1b; 2.循环遍历数组temperatures&#xff1a; 2.1.若当stack不为空同时…...

KDE6桌面于2024年2月发布

原文&#xff1a;KDE MegaRelease 6 - KDE 社区 1. **Plasma 6 桌面环境**&#xff1a;KDE Plasma 是一个现代化、功能丰富的 Linux 操作系统桌面环境&#xff0c;以其时尚设计、可定制界面和广泛的应用程序而闻名。Plasma 6 带来了两项重大技术升级&#xff1a;过渡到最新的应…...

「TypeScript系列」TypeScript 对象及对象的使用场景

文章目录 一、TypeScript 对象1. 对象字面量2. 类实例化3. 使用接口定义对象形状4. 使用类型别名定义对象类型5. 使用工厂函数创建对象 二、TypeScript 对象属性及方法1. 对象属性2. 对象方法3. 访问器和修改器&#xff08;Getters 和 Setters&#xff09; 三、TypeScript 对象…...

shell从入门到精通(22)shell正则匹配~=

文章目录 1. 基本用法2. 正则表达式捕获组(catch group)3. 匹配结果提取1. 基本用法 在 Shell 脚本中,可以使用正则表达式进行文本匹配和提取。Bash shell 支持使用 [[ … =~ … ]] 结构进行正则表达式匹配,同时还能提取匹配结果。 以下是一个简单的例子,展示了如何在 Bas…...

【Spring】使用Spring常用导入依赖介绍

当使用Spring框架时&#xff0c;以下是常用导入的依赖的详细介绍&#xff0c;按照不同的功能和类别进行分点表示和归纳&#xff1a; 1、核心依赖 Spring Core (spring-core) 功能&#xff1a;提供了Spring框架的基础功能&#xff0c;包括IoC&#xff08;控制反转&#xff09;…...

PC端应用订阅SDK接入攻略

本文档介绍了联想应用联运sdk接入操作指南&#xff0c;您可在了解文档内容后&#xff0c;自行接入应用联运sdk。 1. 接入前准备 1. 请先与联想商务达成合作意向。 2. 联系联想运营&#xff0c;提供应用和公司信息&#xff0c;并获取商户id、app id、key&#xff08;公私钥、…...

WebService的wsdl详解

webservice服务的wsdl内容详解&#xff0c;以及如何根据其内容编写调用代码 wsdl示例 展示一个webservice的wsdl&#xff0c;及调用这个接口的Axis客户端 wsdl This XML file does not appear to have any style information associated with it. The document tree is shown…...

数据分析实战:从0到1完成数据获取分析到可视化

文章目录 1.数据分析基本流程1.1 数据采集1.2 数据提炼1.3 数据探索分析 2.数据获取的方法和工具2.1 数据解锁器2.2 爬虫浏览器2.3 数据洞察市场 3.完整案例分析&#xff1a;从数据采集到数据可视化3.1 直接按需定制数据集获取数据3.2 获取IP代理&#xff0c;利用python爬取数据…...

【Spring】深入理解 Spring 中的 ImportSelector、Aware 和 Processor 接口

前言 Spring 框架提供了一系列接口和机制&#xff0c;为开发者提供了灵活、可扩展的编程模型。其中&#xff0c;ImportSelector、Aware 接口以及 Processor 系列接口是非常重要的扩展点&#xff0c;本文将深入探讨它们的设计目的、使用方法以及示例应用。 一、ImportSelector…...

【C语言】strstr函数的使用和模拟

前言 今天给大家带来一个字符串函数&#xff0c;strstr()的使用介绍和模拟实现。 模拟实现这个函数&#xff0c;可以帮助我们更深刻地理解这个函数的功能和提高解决字符串相关问题的能力&#xff0c;有兴趣的话就请往下看吧。 strstr函数介绍 函数功能&#xff1a; strstr函…...

五分钟”手撕“异常

目录 一、什么是异常 二、异常的体系和分类 三、异常的处理 1.抛出异常 2.异常的捕获 异常声明throws&#xff1a; try-catch处理 四、finally finally一定会被执行吗&#xff1f; 五、throw和throws区别 六、异常处理的流程 七、自定义异常 一、什么是异常 顾名…...

【vue3+elementuiplus】el-select下拉框会自动触发校验规则

场景&#xff1a;编辑弹框省份字段下拉框必填&#xff0c;触发方式change&#xff0c;有值第一次打开不会触发校验提示&#xff0c;关闭弹框再次打开触发必填校验提示&#xff0c;但是该字段有值 问题的原因是&#xff1a;在关闭弹层事件中&#xff0c;我做了resetfileds&…...

装饰模式(Decorator Pattern)重构java邮件发奖系统实战

前言 现在我们有个如下的需求&#xff0c;设计一个邮件发奖的小系统&#xff0c; 需求 1.数据验证 → 2. 敏感信息加密 → 3. 日志记录 → 4. 实际发送邮件 装饰器模式&#xff08;Decorator Pattern&#xff09;允许向一个现有的对象添加新的功能&#xff0c;同时又不改变其…...

docker详细操作--未完待续

docker介绍 docker官网: Docker&#xff1a;加速容器应用程序开发 harbor官网&#xff1a;Harbor - Harbor 中文 使用docker加速器: Docker镜像极速下载服务 - 毫秒镜像 是什么 Docker 是一种开源的容器化平台&#xff0c;用于将应用程序及其依赖项&#xff08;如库、运行时环…...

进程地址空间(比特课总结)

一、进程地址空间 1. 环境变量 1 &#xff09;⽤户级环境变量与系统级环境变量 全局属性&#xff1a;环境变量具有全局属性&#xff0c;会被⼦进程继承。例如当bash启动⼦进程时&#xff0c;环 境变量会⾃动传递给⼦进程。 本地变量限制&#xff1a;本地变量只在当前进程(ba…...

江苏艾立泰跨国资源接力:废料变黄金的绿色供应链革命

在华东塑料包装行业面临限塑令深度调整的背景下&#xff0c;江苏艾立泰以一场跨国资源接力的创新实践&#xff0c;重新定义了绿色供应链的边界。 跨国回收网络&#xff1a;废料变黄金的全球棋局 艾立泰在欧洲、东南亚建立再生塑料回收点&#xff0c;将海外废弃包装箱通过标准…...

AI书签管理工具开发全记录(十九):嵌入资源处理

1.前言 &#x1f4dd; 在上一篇文章中&#xff0c;我们完成了书签的导入导出功能。本篇文章我们研究如何处理嵌入资源&#xff0c;方便后续将资源打包到一个可执行文件中。 2.embed介绍 &#x1f3af; Go 1.16 引入了革命性的 embed 包&#xff0c;彻底改变了静态资源管理的…...

有限自动机到正规文法转换器v1.0

1 项目简介 这是一个功能强大的有限自动机&#xff08;Finite Automaton, FA&#xff09;到正规文法&#xff08;Regular Grammar&#xff09;转换器&#xff0c;它配备了一个直观且完整的图形用户界面&#xff0c;使用户能够轻松地进行操作和观察。该程序基于编译原理中的经典…...

GruntJS-前端自动化任务运行器从入门到实战

Grunt 完全指南&#xff1a;从入门到实战 一、Grunt 是什么&#xff1f; Grunt是一个基于 Node.js 的前端自动化任务运行器&#xff0c;主要用于自动化执行项目开发中重复性高的任务&#xff0c;例如文件压缩、代码编译、语法检查、单元测试、文件合并等。通过配置简洁的任务…...

【网络安全】开源系统getshell漏洞挖掘

审计过程&#xff1a; 在入口文件admin/index.php中&#xff1a; 用户可以通过m,c,a等参数控制加载的文件和方法&#xff0c;在app/system/entrance.php中存在重点代码&#xff1a; 当M_TYPE system并且M_MODULE include时&#xff0c;会设置常量PATH_OWN_FILE为PATH_APP.M_T…...

git: early EOF

macOS报错&#xff1a; Initialized empty Git repository in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/ remote: Enumerating objects: 2691797, done. remote: Counting objects: 100% (1760/1760), done. remote: Compressing objects: 100% (636/636…...

Spring Boot 与 Kafka 的深度集成实践(二)

3. 生产者实现 3.1 生产者配置 在 Spring Boot 项目中&#xff0c;配置 Kafka 生产者主要是配置生产者工厂&#xff08;ProducerFactory&#xff09;和 KafkaTemplate 。生产者工厂负责创建 Kafka 生产者实例&#xff0c;而 KafkaTemplate 则是用于发送消息的核心组件&#x…...