Spring AI 来啦,快速上手

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;@Autowiredpublic 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.mzph.cn/pingmian/4874.shtml

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

相关文章

【分治算法】【Python实现】棋盘覆盖

文章目录 [toc]问题描述分治算法时间复杂性Python实现 个人主页&#xff1a;丷从心 系列专栏&#xff1a;分治算法 学习指南&#xff1a;Python学习指南 问题描述 在一个 2 k 2 k 2^{k} \times 2^{k} 2k2k个方格组成的棋盘中&#xff0c;若恰有一个方格与其他方格不同&…

httpClient提交报文中文乱码

httpClient提交中文乱码&#xff0c;ContentType类型application/json 指定提交参数的编码即可 StringEntity se new StringEntity(paramBody.toJSONString(),"UTF-8");se.setContentType("application/json");context.httpPost.setHeader("Cookie&…

JUC并发-共享模型-无锁-乐观锁(非阻塞)

1、问题提出 有如下需求&#xff0c;保证 account.withdraw 取款方法的线程安全 public class TestAccount {public static void main(String[] args) {Account account new AccountCas(10000);Account.demo(account);} }class AccountUnsafe implements Account {private I…

2024LarkXR新增功能系列之五 | 单端口支持多并发

实时云渲染技术在为虚拟现实、游戏、和各种应用程序提供强大的渲染支持的同时&#xff0c;也带来了一些网络和运维上的挑战。在传统的设置中&#xff0c;实时云渲染推流技术需要为每个视频流单独占用服务器的一个端口。这种方法在多用户同时访问的情况下可能会导致端口资源的快…

MemFire解决方案-物联网数据平台解决方案

方案背景 随着各种通讯、传感技术发展&#xff0c;数据通讯成本的急剧下降&#xff0c;数以万亿计的智能设备&#xff08;智能手环、智能电表、智能手机、各种传感器设备等&#xff09;接入网络&#xff0c;并源源不断的产生海量的实时数据。这些海量数据的价值挖掘&#xff0…

Node私库Verdaccio使用记录,包的构建,推送和拉取

Node私库Verdaccio使用记录&#xff0c;包的构建&#xff0c;推送和拉取 Verdaccio是一个轻量级的私有npm代理注册中心&#xff0c;它可以帮助你在本地搭建一个npm仓库&#xff0c;非常适合企业内部使用。通过使用Verdaccio&#xff0c;你可以控制和缓存依赖包&#xff0c;提高…

边OTG边充电芯片LDR6500

随着科技的飞速发展&#xff0c;智能移动设备已成为我们生活中不可或缺的一部分。而在这些设备的连接与数据传输中&#xff0c;Type-C接口以其高效、便捷的特性逐渐占据了主导地位。OTG&#xff08;On-The-Go&#xff09;技术则进一步扩展了Type-C接口的功能&#xff0c;使得设…

构建安全高效的数字货币钱包:开发指南

在加密货币领域的蓬勃发展中&#xff0c;数字货币钱包成为了连接用户与区块链的重要桥梁。作为存储、发送和接收加密资产的工具&#xff0c;数字货币钱包的安全性和效率至关重要。本文将介绍如何构建一个安全高效的数字货币钱包&#xff0c;并提供开发指南&#xff0c;帮助开发…

2024中国(江西)国际先进陶瓷材料及智能装备博览会

2024中国&#xff08;江西&#xff09;国际先进陶瓷材料及智能装备博览会 “中国&#xff08;江西&#xff09;国际先进陶瓷材料及智能装备博览会” 陶瓷三新展 &#xff08;新材料、新装备、新技术&#xff09; 绿色智能、引领未来 2024年11月1日-11月3日 中国江西 南昌…

深度学习系列64:数字人wav2lip详解

1. 整体流程 第一步&#xff0c;加载视频/图片和音频/tts。用melspectrogram将wav文件拆分成mel_chunks。 第二步&#xff0c;调用face_detect模型&#xff0c;给出人脸检测结果&#xff08;可以改造成从文件中读取&#xff09;&#xff0c;包装成4个数组batch&#xff1a;img…

html显示PDF并兼容IE浏览器的解决方案

方案一、vue-pdf插件 缺点&#xff1a;IE11显示空白&#xff0c;编译后的Edge测试环境可以正常线上&#xff0c;打到线上报错&#xff0c;谷歌和百分浏览器显示完美 1、vue 只显示核心代码&#xff0c;需要安装vue-pdf插件 <vue-pdf :src"ivcPdfUrl"></v…

Spring声明式事务(@Transactional)原理之-ProxyTransactionManagementConfiguration

文章目录 目录 文章目录 前言 一、切入点以及切面的匹配规则 1.1 TransactionAttributeSourcePointcut事务的切入点匹配 二、TransactionInterceptor切面的具体逻辑 2.1 声明式事务实现的具体逻辑 总结 前言 上一篇文章已经说过了声明式事务的原理其实就是SpringAop动态…

C语言基础:初识指针(二)

当你不知道指针变量初始化什么时&#xff0c;可以初始化为空指针 int *pNULL; 我们看NULL的定义&#xff0c;可以看出NULL是0被强制转化为Void* 类型的0&#xff1b;实质还是个0&#xff1b; 如何避免野指针&#xff1a; 1. 指针初始化 2. 小心指针越界 3. 指针指向空间…

2024五一杯ABC题资料合集+代码+参考论文!!!

一.赛题思路 (赛题出来以后第一时间在群内分享) 二.比赛日期和时间 比赛开始时间:2024年5月1日(周三)10;00 比赛结束时间:2024年5月4日(周六&#xff09;12:00 三.将在明天分享的资料合集如下 4.我们这边会分享22年的题目完整全部的过程 5.数学建模的29个通用模型及MATLAB…

Python网络数据抓取(3):Beautiful Soup

Beautiful Soup 这个库通常被称为Beautiful Soup 4&#xff08;BS4&#xff09;。它主要用来从HTML或XML文件中抓取数据。此外&#xff0c;它也用于查询和修改HTML或XML文档中的数据。 现在&#xff0c;让我们来了解如何使用Beautiful Soup 4。我们将采用上一节中使用的HTML数据…

实验室温湿度设备巡检的意义

温湿度监控设备的正常运行对于实验室的科研工作来说至关重要&#xff0c;然而&#xff0c;我们常常会遇到设备出现故障或者探头不灵敏的情况&#xff0c;这给实验室的工作带来了诸多困扰&#xff0c;为了解决这些问题&#xff0c;对实验室温湿度设备进行巡检管理也是一种非常有…

前端vue如何生成二维码

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

数据结构 -- 双向链表

谁说我没有死过? 出生以前, 太阳已无数次起落. 悠久的时光被悠久的虚无吞没, 又以我生日的名义卷土重来. --史铁生 目录 1. 前言2. 双向链表的结构3. 双向链表的实现4. 完整代码5. 总结 正文开始 1. 前言 双向链表是一种常见的数据结构&#xff0c;它与单向链表相比&#x…

MacPro(M1,M2芯片)Java开发和常用工具开源软件合集

目录 Java开发软件1 IDE1.1 idea1.2 Vs Code 2 开发工具2.1 数据库数据库模型管理数据库连接客户端 2.2 SSH/Telnet/Serial/Shell/Sftp客户端2.3 MarkDown编辑器2.3 代码片段管理粘贴 3小工具3.1 截图贴图3.2 Mac下修改hosts文件的图形化界面软件 Java开发软件 1 IDE 1.1 ide…

如果把软路由的网段更换成169.254.0.0/16会咋样?

前言 这几天有小伙伴在折腾软路由系统&#xff0c;然后问题就来了。 他咨询的是&#xff1a;为啥电脑连接软路由之后&#xff0c;无法访问软路由的管理页&#xff1f; 嗯。。。确实不是什么大事。但不注意看&#xff0c;还以为软路由没有正常获取到ip。 熟悉网络的小伙伴们都…