Spring AI 来啦,快速上手

news2024/9/20 5:53:24

Spring AI

Spring框架在软件开发领域,特别是在Java企业级应用中,一直扮演着举足轻重的角色。它以其强大的功能和灵活的架构,帮助开发者高效构建复杂的应用程序。而Spring Boot的推出,更是简化了新Spring应用的初始搭建和开发过程,让开发者能够更快速地构建和部署应用。
随着人工智能技术的快速发展,越来越多的领域开始融合AI技术,以提高效率、优化流程和增强用户体验。在这个背景下,Spring AI项目的推出无疑为Java开发者打开了一扇新的大门,使他们能够更容易地将AI技术集成到Spring应用程序中。
Spring AI项目不仅为开发者提供了一套丰富的工具和库,还集成了自然语言处理(NLP)、机器学习以及图像识别和计算机视觉等功能。这意味着开发者无需深入了解AI技术的细节,就可以利用Spring AI轻松实现复杂的AI功能,如文本处理、情感分析、语音识别、图像识别等。
此外,Spring AI的推出也标志着Spring生态的进一步扩展。随着AI技术的不断发展和普及,未来Spring框架有望与更多AI技术进行深度集成,为开发者提供更强大的功能和更广阔的应用场景。

Spring AI 特性

模型支持:Spring AI框架集成了多家国外主流的AI模型提供商,如OpenAI等,使得开发者能够轻松地使用这些高质量的AI模型。此外,框架的扩展性设计也使得后续集成国内的大模型变得十分方便,从而满足更广泛的业务需求。
模型类型:Spring AI支持多种模型类型,包括聊天机器人、文本到图像转换等,为开发者提供了丰富的选择。这些模型类型能够覆盖各种应用场景,满足不同的业务需求。
API可移植性:Spring AI框架提供了跨不同AI提供商的可移植API,这使得开发者能够轻松地在不同的服务提供商之间切换,而无需担心功能丢失或重新开发。这种可移植性有助于降低技术锁定风险,提高系统的灵活性和可扩展性。
数据映射:Spring AI将AI模型的输出映射到Java对象(POJO),从而简化了数据处理流程。这种映射机制使得开发者能够更方便地处理和使用AI模型的输出数据,提高了开发效率和代码的可读性。
矢量数据库支持:Spring AI框架覆盖了所有主流的矢量数据库供应商,从Azure Vector Search到Weaviate等,为开发者提供了丰富的数据库选择。这种支持使得开发者能够根据自己的需求选择合适的数据库,从而优化系统的性能和稳定性。
SQL类过滤器API:Spring AI提供了类似SQL的元数据过滤器API,使得开发者能够跨供应商实现一致性的数据过滤操作。这种API设计降低了学习成本,提高了开发效率,使得开发者能够更快速地构建出功能强大的AI应用。
Spring Boot集成:Spring AI专为Spring Boot设计了自动配置和启动器,使得AI集成变得轻而易举。开发者只需在Spring Boot项目中添加相应的依赖和配置,即可快速集成AI功能,无需进行繁琐的手动配置和编码工作。这种集成方式降低了技术门槛,使得更多的开发者能够轻松地利用Spring AI框架构建出高质量的AI应用。

快速入门

MAVEN配置

导入Spring AI BOM: 使用Spring AI BOM定义

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

添加AI功能:添加相关的AI模块依赖项到pom.xml,比如OpenAI 聊天。
(您需要使用 OpenAI 创建 API 来访问 ChatGPT 模型。在OpenAI 注册页面创建帐户并在API 密钥页面生成令牌。 Spring AI 项目定义了一个配置属性,您应该将其设置为从 openai.com 获取的spring.ai.openai.api-key值。API Key导出环境变量是设置该配置属性的一种方法:export SPRING_AI_OPENAI_API_KEY=)

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

Chat Properties配置

重试属性

该前缀spring.ai.retry用作属性前缀,可让您配置 OpenAI Chat 客户端的重试机制。

PropertyDescriptionDefault
spring.ai.retry.max-attemptsMaximum number of retry attempts.10
spring.ai.retry.backoff.initial-intervalInitial sleep duration for the exponential backoff policy.2 sec.
spring.ai.retry.backoff.multiplierBackoff interval multiplier.5
spring.ai.retry.backoff.max-intervalMaximum backoff duration.3 min.
spring.ai.retry.on-client-errorsIf false, throw a NonTransientAiException, and do not attempt retry for 4xx client error codesfalse
spring.ai.retry.exclude-on-http-codesList of HTTP status codes that should not trigger a retry (e.g. to throw NonTransientAiException).empty
spring.ai.retry.on-http-codesList of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException).empty
连接属性

该前缀spring.ai.openai用作允许您连接到 OpenAI 的属性前缀。

PropertyDescriptionDefault
spring.ai.openai.base-urlThe URL to connect toapi.openai.com
spring.ai.openai.api-keyThe API Key-
配置属性

前缀spring.ai.openai.chat是属性前缀,可让您配置 OpenAI 的聊天客户端实现。

PropertyDescriptionDefault
spring.ai.openai.chat.enabledEnable OpenAI chat client.true
spring.ai.openai.chat.base-urlOptional overrides the spring.ai.openai.base-url to provide chat specific url-
spring.ai.openai.chat.api-keyOptional overrides the spring.ai.openai.api-key to provide chat specific api-key-
spring.ai.openai.chat.options.modelThis is the OpenAI Chat model to usegpt-3.5-turbo (the gpt-3.5-turbo, gpt-4, and gpt-4-32k point to the latest model versions)
spring.ai.openai.chat.options.temperatureThe sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction of these two settings is difficult to predict.0.8
spring.ai.openai.chat.options.frequencyPenaltyNumber between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.0.0f
spring.ai.openai.chat.options.logitBiasModify the likelihood of specified tokens appearing in the completion.-
spring.ai.openai.chat.options.maxTokensThe maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model’s context length.-
spring.ai.openai.chat.options.nHow many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.1
spring.ai.openai.chat.options.presencePenaltyNumber between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.-
spring.ai.openai.chat.options.responseFormatAn object specifying the format that the model must output. Setting to { “type”: “json_object” } enables JSON mode, which guarantees the message the model generates is valid JSON.-
spring.ai.openai.chat.options.seedThis feature is in Beta. If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.-
spring.ai.openai.chat.options.stopUp to 4 sequences where the API will stop generating further tokens.-
spring.ai.openai.chat.options.topPAn alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both.-
spring.ai.openai.chat.options.toolsA list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.-
spring.ai.openai.chat.options.toolChoiceControls which (if any) function is called by the model. none means the model will not call a function and instead generates a message. auto means the model can pick between generating a message or calling a function. Specifying a particular function via {"type: “function”, “function”: {“name”: “my_function”}} forces the model to call that function. none is the default when no functions are present. auto is the default if functions are present.-
spring.ai.openai.chat.options.userA unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.-
spring.ai.openai.chat.options.functionsList of functions, identified by their names, to enable for function calling in a single prompt requests. Functions with those names must exist in the functionCallbacks registry.-

您可以覆盖通用的spring.ai.openai.base-urland spring.ai.openai.api-keyforChatClient和EmbeddingClient实现。如果设置了和属性spring.ai.openai.chat.base-url,spring.ai.openai.chat.api-key则优先于公共属性。如果您想对不同的模型和不同的模型端点使用不同的 OpenAI 帐户,这非常有用。

代码demo

配置

spring.ai.openai.api-key=YOUR_API_KEY
spring.ai.openai.embedding.options.model=text-embedding-ada-002

Controller代码

@RestController
public class EmbeddingController {

    private final EmbeddingClient embeddingClient;

    @Autowired
    public EmbeddingController(EmbeddingClient embeddingClient) {
        this.embeddingClient = embeddingClient;
    }

    @GetMapping("/ai/embedding")
    public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
        return Map.of("embedding", embeddingResponse);
    }
}

其它

ttps://spring.io/projects/spring-ai

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1633546.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

(超全)python图像处理详细解析(1)

图像处理 skimage包的子模块1.读取图像2.图像灰度处理3.加载程序自带图像4.查看存储路径5.保存图片6.图片信息7.输出小猫图片的G通道中的第20行30行列的像素值8.显示红色单通道图片9.对小猫图片添加椒盐噪声10.高斯去噪11.中值滤波去噪12.随机生成噪声点13.对小猫图像进行裁剪1…

vue报错:Do not mutate vuex store state outside mutation handlers.

vue报错&#xff1a;Do not mutate vuex store state outside mutation handlers. 原因&#xff1a;在vuex store的state外部直接修改了state的值&#xff0c;但是Vuex要求所有的state的修改必须在vuex中&#xff0c;不允许直接咋组件中修改store中的状态&#xff0c;除非通过m…

ios CI/CD 持续集成 组件化专题五-(自动发布私有库-组件化搭建)

一&#xff1a;手动发布私有库总结 手动发布pod私有库&#xff0c;需要进行如下几步操作&#xff1a; 1、修改完代码之后&#xff0c;需要提交代码push到git仓库。 2、给代码打tag。 3、修改podspec文件的version值&#xff0c;使其和设置的tag一直。 4、命令行执行pod repo…

【Java EE】日志框架(SLF4J)与门面模式

文章目录 &#x1f340;SLF4j&#x1f333;门面模式(外观模式)&#x1f338;门面模式的定义&#x1f338;门面模式的模拟实现&#x1f338;门面模式的优点 &#x1f332;关于SLF4J框架&#x1f338;引入日志门面 ⭕总结 &#x1f340;SLF4j SLF4J不同于其他⽇志框架,它不是⼀个…

分子动力学模拟学习-Gromacs工具链

1、总体流程 在gromacs的使用说明中有一个flow chart&#xff0c;比较简略。以下针对一般体系&#xff08;非蛋白等领域&#xff09;进行了一些调整&#xff0c;通用性更强。 在做分子动力学模拟时&#xff0c;其复杂性除了以上各种输入输出文件的操作&#xff0c;另一点就是力…

DataX数据采集流程(项目)

目录 1.CDH介绍 2.ClouderaManager架构 3.服务器 4.dataX架构 5.Datax数据处理流程 6.DataX的使用说明 7.Mysql数据切割 8.Mysql数据导入HDFS 9.查询站点 站点页面如下&#xff0c;可进一步查询导入的数据内容 10.dataX-Web访问页面 创建数据库连接 1.CDH介绍 --(…

pycharm中执行./activate命令激活服务器提示“about_Execution_Policies”

1.虚拟环境创建 环境&#xff1a; 操作系统&#xff1a;Windows11 pycharm&#xff1a; 2022.1.4 python版本&#xff1a;3.9 执行命令安装&#xff1a; pip install virtualenv 执行命令创建虚拟环境&#xff1a; virtualenv venv 2.激活报错 执行命令激活虚拟环境&…

朋友们,帮忙填写一个问卷呀!关于高速服务区一体化车流管理系统的线上调研,急需各位大神的帮助!!!

✌ 作者简介&#xff1a;瑞骏 RUIJUN &#x1f4eb; 如果文章知识点有错误的地方&#xff0c;请指正&#xff01;和大家一起学习&#xff0c;一起进步&#x1f440; &#x1f4ac; 人生格言&#xff1a;没有我不会的语言&#xff0c;没有你过不去的坎儿。&#x1f4ac; &#x…

Kafka Exactly Once 语义实现原理:幂等性与事务消息

01 前言 在现代分布式系统中&#xff0c;确保数据处理的准确性和一致性是至关重要的。Apache Kafka&#xff0c;作为一个广泛使用的流处理平台&#xff0c;提供了强大的消息队列和流处理功能。随着业务需求的增长&#xff0c;Kafka 的事务消息功能应运而生&#xff0c;它允许应…

cocos-lua资源管理

本文介绍cocos-lua项目的资源管理和工作流&#xff0c;适用人群包括初学者和有经验开发者&#xff0c;故读者可根据自己的需要有选择性的查阅自己需要的内容&#xff0c;下文以ccs代指Cocos Studio 一.简单案例解析 下文通过介绍一个简单demo&#xff0c;介绍合图和资源目录结…

React Router 路由配置数组配组持久化

在一些特定场景下,你可能需要将路由配置数组进行持久化,例如从后端动态加载路由配置或根据用户权限动态生成路由配置。这时,持久化路由配置数组就很有用,可以避免每次应用启动时重新获取或计算路由配置。 持久化路由配置数组的步骤如下: 定义路由配置数组 首先,你需要定义一…

每日一题(力扣55):跳跃游戏--贪心

刚开始像这道题&#xff0c;想的是这么从当前可以走的那几步中选择一步&#xff0c;所以一坨屎一样的代码 class Solution { public:bool canJump(vector<int>& nums) {int nnums.size();int step0;int u0;int u_max0;int step_size0;int max_size0;int loci0;while…

前端vue如何生成二维码

有时候有需要链接直接生成二维码在手机上看的需求&#xff0c;比如下载&#xff0c;比如信息&#xff0c;比如excel 下面先引入包 import QRCode from qrcode; 然后上代码 // 将res转换成二维码const qrCodeData JSON.stringify(res); // 将res转换为字符串作为二维码数据// …

基于Springboot的水产养殖系统(有报告)。Javaee项目,springboot项目。

演示视频&#xff1a; 基于Springboot的水产养殖系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;springboot项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&…

php7.4在foreach中对使用数据使用无法??[]判读,无法使用引用传递

代码如下图&#xff1a;这样子在foreach中是无法修改class_history的。正确的应该是去掉??[]判断。 public function actionY(){$array [name>aaa,class_history>[[class_name>一班,class_num>1],[class_name>二班,class_num>2]]];foreach ($array[class_…

系统思考—企业辅导咨询

从2004年、2014年到2024年&#xff0c;国九条政策的发布与变迁不仅影响了行业趋势&#xff0c;更深刻地改变了企业的风险预估和策略辅导。彼得杜鲁克曾经说过&#xff1a;“必须系统地抛弃旧知识。”这不仅是企业领导者的挑战&#xff0c;也是我们每个人的难题。难点不在于我们…

cesium教程

环境搭建 vscode安装Visual Studio Code - Code Editing. Redefined nodejs安装Node.js — Run JavaScript Everywhere cesium源码下载编译 cesium官网下载源码https://cesium.com/downloads/ 解压下载的源码 VsCode打开远吗&#xff0c;找到index.html,右键打开 Open wit…

开源博客项目Blog .NET Core源码学习(20:App.Hosting项目结构分析-8)

本文学习并分析App.Hosting项目中后台管理页面的个人资料页面、修改密码页面。 个人资料页面 个人资料页面用于显示和编辑个人信息&#xff0c;支持从本地上传个人头像。整个页面使用了layui中的表单、日期与时间选择、上传等样式或模块&#xff0c;通过layui.css文件设置样式…

案例-部门管理-删除

黑马程序员JavaWeb开发教程 文章目录 一、查看页面原型二、查看接口文档三、开发1、Controller2、Service&#xff08;1&#xff09;service接口层&#xff08;3&#xff09;service实现层 3、Mapper4、Postman 一、查看页面原型 二、查看接口文档 三、开发 1、Controller 因…

2022-2003年上市公司企业商业信用融资数据

01、数据简介 企业商业信用融资是指企业之间在买卖商品时&#xff0c;以商品形式提供的借贷活动。这种融资方式是经济活动中一种最普遍的债权债务关系。商业信用的存在对于扩大生产和促进流通起到了十分积极的作用&#xff0c;但不可避免的也存在着一些消极的影响。 测算方式…