Table of Contents
Purpose
Before implementation
Prompt words
概念验证
项目设置
代码实现
Spring AI 集成
客户端 - Spring Boot应用程序通信
客户端 - Open AI 通信
调整和观察
结论
Home Technology peripherals AI Use Spring Boot and Spring AI to build generative artificial intelligence applications

Use Spring Boot and Spring AI to build generative artificial intelligence applications

Apr 28, 2024 am 11:46 AM
ai spring spring framework gen ai

As an industry leader, Spring AI provides leading solutions for various industries through its powerful, flexible API and advanced functions. In this topic, we will delve into examples of Spring AI applications in various fields. Each case will show how Spring AI meets specific needs, achieves goals, and extends these LESSONS LEARNED to a wider range of applications. I hope this topic can inspire you to understand and utilize the infinite possibilities of Spring AI more deeply.

The Spring framework has been in the field of software development for more than 20 years, and it has been 10 years since the Spring Boot 1.0 version was released. Now, no one can dispute that Spring has created a unique style that frees developers from repetitive tasks and focuses on delivering business value. Over time, Spring's technical depth has continued to increase, covering a wide range of development areas and technologies. On the other hand, its technical breadth continues to expand as more specialized solutions are tried, proofs of concepts are created, and ultimately promoted under the umbrella of the project.

The Spring AI project is an example of a project that, according to its reference documentation, aims to simplify the development process when the need for a generative artificial intelligence layer is introduced into an application. Developers are once again freed from repetitive tasks and can interact directly with pre-trained models (including the actual processing algorithms) through a simple interface.

By interacting with Generative Pretrained Transformers (GPTs) directly through Spring AI programmatically, users (developers) do not need to have extensive machine learning knowledge. But as an engineer, I strongly feel that even though these developer tools can be easy and fast to use and produce results, I suggest that we need to restrain ourselves and be vigilant in trying to understand the basic concepts first. Additionally, by following this path, the output is likely to be more valuable.

Purpose

This article introduces how to integrate Spring AI into Spring Boot applications and programmatically interact with OpenAI. We assume that design is generally a state-of-the-art activity. Therefore, the prompts used during the experiment are instructive but not very applicable. The focus here is on the communication interface, the Spring AI API.

Before implementation

First of all, you must clarify your reasons for using GPT solutions. In addition to hoping to provide better quality, save time and reduce costs .

Production AI is said to be good at performing a large number of time-consuming tasks, producing results faster and more efficiently. Furthermore, the likelihood of obtaining useful results increases if these results are further verified by experienced and intelligent humans.

Next, resist the temptation to jump right into implementation and at least take some time to familiarize yourself with the general concepts. An in-depth exploration of the concept of generative AI is well beyond the scope of this article. However, the "main actors" present in the interaction are briefly described below.

Stage - Generative artificial intelligence is a part of artificial intelligence
Input - provided data (input)
Output - calculation result (output)
Large language model (LLM) - a fine-tuned algorithm that produces output based on interpreted input
Prompt words - a state-of-the-art interface through which input is passed into the model
Prompt word templates - components that allow the construction of structured parameterized prompts
Tokens - the algorithm internally converts inputs into tokens, then uses these tokens to compile results and ultimately constructs outputs from them
Model's environment window - the threshold by which the model limits the number of tokens per call (usually , the more tokens used, the more expensive the operation)

Finally, the implementation can begin, but as the implementation proceeds, it is recommended to review and optimize the first two steps.

Prompt words

In this exercise, we request the following:

Write {count = three} reasons why people in {location = Romania} should consider a {job = software architect} job. These reasons need to be short, so they fit on a poster. For instance, "{job} jobs are rewarding."

上面内容代表了提示词模板。按照建议,应作为提示词的一部分提供清晰的主题,清晰的任务含义以及额外的有用信息,以提高结果的准确性。

提示词包含三个参数

count - 希望作为输出的原因数量
job - 感兴趣的领域或工作
location - 工作申请者所在的国家,城镇,地区等

概念验证

在这篇文章中,简单的概念验证目标如下:

将 Spring AI 集成到Spring Boot应用程序并使用它
允许客户端通过应用程序与 Open AI 进行通信
客户端向应用程序发出参数化的HTTP请求
应用程序使用一个提示词来创建输入,发送给 Open AI 并获取输出
应用程序将响应发送给客户端

利用Spring Boot以及Spring AI构建生成式人工智能应用图片

项目设置

Java 21
Maven 3.9.2
Spring Boot – v. 3.2.2
Spring AI – v. 0.8.0-SNAPSHOT (仍在开发,实验性)

代码实现

Spring AI 集成

通常,这是一个基本步骤,不一定值得一提。然而,因为 Spring AI 目前以快照形式发布,为了能够集成 Open AI 自动配置依赖,你需要添加一个引用到 Spring 快照仓库。

<repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/snapshot</url><releases><enabled>false</enabled></releases></repository></repositories>

下一步是添加 spring-ai-openai-spring-boot-starter Maven 依赖项。

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

Open AI ChatClient 现在是应用程序类路径的一部分。它是用来向 Open AI 发送输入并获取输出的组件。

为了能够连接到AI模型,需要在 application.properties 文件中设置 spring.ai.openai.api-key 属性。

spring.ai.openai.api-key = api-key-value

它的值代表了用户的有效API密钥,用户将通过此密钥进行通信。通过访问[资源2],可以注册或登录并生成一个。

客户端 - Spring Boot应用程序通信

概念验证的第一部分是客户端应用程序(例如浏览器,curl等)与开发的应用程序之间的通信。这是通过一个 REST 控制器实现的,可以通过HTTP GET请求访问。

URL路径是 /job-reasons,还有之前在定义提示时概述的三个参数,这将导致如下格式:

/job-reasons?count={count}&job={job}&locatinotallow={location}

和相应的控制器:

@RestControllerpublic class OpenAiController { @GetMapping("/job-reasons")public ResponseEntity<String> jobReasons(@RequestParam(value = "count", required = false, defaultValue = "3") int count, @RequestParam("job") String job, @RequestParam("location") String location) {return ResponseEntity.ok().build();}}

由于来自 Open AI 的响应将是一个字符串,因此控制器返回一个封装了字符串的ResponseEntity。如果我们运行应用程序并发出请求,当前响应体部分没有返回任何内容。

客户端 - Open AI 通信

Spring AI 目前主要关注处理语言并产生语言或数字的AI模型。在前一类别中, Open AI 模型的例子包括GPT4-openai或GPT3.5-openai。

为了与这些AI模型(实际上是指 Open AI 算法)进行交互, Spring AI 提供了一个统一的接口。

ChatClient接口目前支持文本输入和输出,并具有简单的契约。

@FunctionalInterfacepublic interface ChatClient extends ModelClient<Prompt, ChatResponse> {default String call(String message) {Prompt prompt = new Prompt(new UserMessage(message));return call(prompt).getResult().getOutput().getContent();} ChatResponse call(Prompt prompt);}

确实如此,功能接口的实际方法通常是被使用的方法。

在我们的概念验证中,这正是我们所需要的,一种调用 Open AI 并发送目标参数化 Prompt 作为参数的方式。我们定义了以下的OpenAiService,在其中注入了一个 ChatClient 的实例。

@Servicepublic class OpenAiService { private final ChatClient client; public OpenAiService(OpenAiChatClient aiClient) {this.client = aiClient;} public String jobReasons(int count, String domain, String location) {final String promptText = """Write {count} reasons why people in {location} should consider a {job} job.These reasons need to be short, so they fit on a poster.For instance, "{job} jobs are rewarding.""""; final PromptTemplate promptTemplate = new PromptTemplate(promptText);promptTemplate.add("count", count);promptTemplate.add("job", domain);promptTemplate.add("location", location); ChatResponse response = client.call(promptTemplate.create());return response.getResult().getOutput().getContent();}}

如果应用程序正在运行,那么可以从浏览器执行以下请求:

http://localhost:8080/gen-ai/job-reasons?count=3&job=software%20architect&locatinotallow=Romania

这下面的结果被检索出来的结果:

利润丰裕的职业:软件架构师的工作提供了有竞争力的薪酬和出色的增长机会,确保在罗马尼亚的财务稳定和成功。
热门职业:随着技术需求的持续增长,软件架构师在罗马尼亚和全世界都备受追捧,提供了丰富的就业前景和就业保障。
创造性问题解决:软件架构师在设计和开发创新软件解决方案中扮演着至关重要的角色,使他们可以释放他们的创造力,并对各种行业产生重大影响。

这就是我们所期望的——一个简易的接口,通过它,可以要求 Open AI GPT模型写出一些原因,解释为何在特定地点的特定工作具有吸引力。

调整和观察

到目前为止,开发的简单概念验证主要使用了默认的配置。

ChatClient实例可以通过各种属性根据所需需要来配置。虽然这超出了本文的范围,但在这里举两个例子。

spring.ai.openai.chat.options.model 指定要使用的AI模型。默认为'gpt-35-turbo',但'gpt-4'和'gpt-4-32k'指定的是最新版本。虽然这些版本都是可用的,但你可能无法使用按使用量付费的计划来访问这些版本,但 Open AI 网站上有更多的信息可以帮助你了解如何适应它。

另一个值得一提的属性是 spring.ai.openai.chat.options.temperature。根据参考文档,采样温度控制了“回应的创新性”。据说,较高的值会让输出“更随机”,而较低的值会“更专注和决定性”。默认值为0.8,如果我们将其降低到0.3,重启应用并再次使用相同的请求参数询问,下面的结果将被检索出来。

有利可图的职业机会:罗马尼亚的软件架构师工作提供有竞争力的薪水和极好的成长前景,对于寻求财务稳定和职业发展的个人来说,这是一个吸引人的职业选择。
具有挑战性和智能刺激的工作:作为一名软件架构师,你将负责设计和实现复杂的软件系统,解决复杂的技术问题,并与有才华的团队合作。这个角色提供了持续的学习机会和在尖端技术上工作的机会。
高需求和工作保障:随着对技术和数字化转型的依赖增加,各行各业对熟练软件架构师的需求在上升。选择在罗马尼亚的软件架构师工作确保了工作安全和广泛的就业选择,无论是在本地还是国际上。

可以看出,这种情况下的输出更具描述性。

最后一个考虑因素是与获取的输出的结构相关的。拥有将实际接收的有效载荷映射到Java对象(例如,类或记录)的能力将非常方便。截至目前,表示形式是文本形式,实现也是如此。输出解析器可能实现这一点,类似于Spring JDBC的映射结构。

在这个概念验证中,我们使用了一个BeanOutputParser,它允许直接将结果反序列化到Java记录中,如下所示:

public record JobReasons(String job, String location, List<String> reasons) {}

通过将 {format} 作为提示文本的一部分,并将其作为指示提供给 AI 模型。

OpenAiService 方法变为:

public JobReasons formattedJobReasons(int count, String job, String location) {final String promptText = """Write {count} reasons why people in {location} should consider a {job} job.These reasons need to be short, so they fit on a poster.For instance, "{job} jobs are rewarding."{format}"""; BeanOutputParser<JobReasons> outputParser = new BeanOutputParser<>(JobReasons.class); final PromptTemplate promptTemplate = new PromptTemplate(promptText);promptTemplate.add("count", count);promptTemplate.add("job", job);promptTemplate.add("location", location); promptTemplate.add("format", outputParser.getFormat());promptTemplate.setOutputParser(outputParser); final Prompt prompt = promptTemplate.create(); ChatResponse response = client.call(prompt);return outputParser.parse(response.getResult().getOutput().getContent());}

再次调用时,输出如下:

{"job":"software architect","location":"Romania","reasons":["High demand","Competitive salary","Opportunities for growth"]}

格式符合预期,但解释的原因似乎较少,这意味着需要进行额外的调整以达到更好的可用性。然而,从概念验证的角度来看,这是可接受的,因为焦点是形式。

结论

提示设计是任务的重要部分 - 提示越清晰,输入越好,输出的质量也就越高。

使用 Spring AI 与各种聊天模型进行集成非常简单 - 本篇文章展示了一个 Open AI 的集成。

然而,对于 Gen AI 或者几乎任何技术来说,首先至少要熟悉基本概念是非常重要的。然后,尝试理解如何进行通讯的魔法,最后再开始编写“生产”代码。

最后但同样重要的是,建议进一步探索 Spring AI API,以了解实现并随着其不断发展和改进保持最新状态。


The above is the detailed content of Use Spring Boot and Spring AI to build generative artificial intelligence applications. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Beginner's Guide to RimWorld: Odyssey
1 months ago By Jack chen
PHP Variable Scope Explained
3 weeks ago By 百草
Commenting Out Code in PHP
3 weeks ago By 百草
Tips for Writing PHP Comments
3 weeks ago By 百草

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1508
276
August cryptocurrency exchange discount evaluation: new user rewards and handling fee reductions August cryptocurrency exchange discount evaluation: new user rewards and handling fee reductions Aug 08, 2025 pm 11:15 PM

Table of Contents: August Binance (Binance) Exchange Discounts: August Bybit Exchange Discounts: August MEXC Matcha Exchange Discounts: August Bitfinex (Green Leaf) Exchange Discounts: Cryptocurrency Exchange Ranking This article will compile the latest offers from major cryptocurrency exchanges in August 2025, and one article allows readers to enjoy the best benefits. What many newbies in the currency circle don’t know is that most exchanges have hidden application offers, which include: fee reduction (10-20% reduction) new account bonus (can serve as margin, use

What is the future price of MemeFi (MEMEFI currency)? Price forecast for 2025, 2026, 2027-2030 What is the future price of MemeFi (MEMEFI currency)? Price forecast for 2025, 2026, 2027-2030 Aug 08, 2025 pm 11:09 PM

What is the MemeFi currency in the directory? MemeFi gameplay introduction MemeFi (MEMEFI) price forecast MemeFi (MEMEFI) price forecast: EMA cluster and Bollinger band extrusion breakthrough MemeFi (MEMEFI) price forecast: RSI and direction trend momentum MemeFi (MEMEFI) price forecast from 2025 to 2030 MemeFi (MEMEFI) price forecast from 2026 MemeFi (MEMEFI) price forecast from 2027 MemeFi (MEMEFI) price forecast from 2028 MemeFi (MEMEFI) 2

Huobi HTX's new assets in one week (7.28-8.4): Multi-track resonance Meme and AI concepts lead the market Huobi HTX's new assets in one week (7.28-8.4): Multi-track resonance Meme and AI concepts lead the market Aug 08, 2025 pm 11:03 PM

Table of Contents Meme's popularity remains: VINE and DONKEY continue to rise. Technical narrative heats up: AI and privacy computing are popular across chains, RWA and regional narrative: OMNI's emerging star Huobi HTX wealth effect continues to be released. Regarding Huobi HTX From July 28 to August 4, the global crypto market maintained a volatile pattern, and the pace of hot spot rotation accelerated. Among the assets launched by Huobi HTX this week, Meme, AI, privacy computing, cross-chain and RWA have advanced together, and the market wealth effect continues to appear. This is also the fifth consecutive week since July that Huobi HTX has achieved collective increase in new assets, further verifying its forward-looking nature in cutting-edge project mining and ecological layout, and continuing to provide strong support for users to grasp the new round of market cycle. Huobi (HTX

Bitcoin (BTC) short-term profit settlement 'cooled' and prices remain firmly at the $115,000 mark Bitcoin (BTC) short-term profit settlement 'cooled' and prices remain firmly at the $115,000 mark Aug 08, 2025 pm 11:00 PM

Table of Contents Markets are in a “relative equilibrium state” for the rest of 2025 Bitcoin Outlook is positive Although Bitcoin prices have fallen from all-time highs, Glassnode points out that the current market has entered a “relative equilibrium position”. According to analysis by on-chain data platform Glassnode, as Bitcoin price gradually rebounds after a local low of $112,000, the selling pressure of short-term holders (STH) in profitable state is weakening. In a market report released on Wednesday, Glassnode said that short-term holders (referring to investors who have held the currency for less than 155 days) have significantly "cooled". Data shows that the "spending output profit margin" (SOPR) measuring the selling ratio of recent buy and profitable investors has declined

What is Bitcoin (BTC)? A brief introduction to what is Bitcoin What is Bitcoin (BTC)? A brief introduction to what is Bitcoin Aug 07, 2025 pm 10:48 PM

Bitcoin (BTC) is a digital asset created and run based on cryptography principles. It does not rely on specific central institutions, such as banks or governments, to issue and manage. Its concept was first proposed in 2008 by an individual or group named "Satoshi Nakamoto" in a paper titled "Bitcoin: A peer-to-peer electronic cash system."

How to use 5,000 yuan to earn 500,000 yuan in the currency circle? How to use 5,000 yuan to earn 500,000 yuan in the currency circle? Aug 07, 2025 pm 08:42 PM

In the field of digital currency, a full range of variables and opportunities, increasing the principal of 5,000 to 500,000 means that one hundred times the asset appreciation needs to be achieved. This is not a simple math game, but a comprehensive test involving cognition, strategy, mentality and execution. It requires participants not to rely solely on luck, but also to have keen market insight and extraordinary risk management capabilities.

SOL price trend forecast: Will it explode again in 2025? SOL price trend forecast: Will it explode again in 2025? Aug 07, 2025 pm 08:06 PM

Yes, SOL may explode again in 2025. 1) Technology upgrades such as Firedancer launch, which is expected to increase TPS to one million level; 2) New DePIN and AI narratives promote ecological development; 3) On-chain data continues to recover, TVL exceeds US$2 billion; 4) Institutional funds return combined with ETF expectations; 5) If fundamentals and market sentiment cooperate, the price is expected to hit US$250, but you need to be wary of the history of downtime, intensified competition and risk of token selling pressure. Investors should build positions in batches and combine on-chain data dynamic adjustment strategies. The final outbreak depends on the synergistic effect of technology implementation, ecological activity and capital resonance.

What are stablecoins? What are the top 10 stablecoins in market value? What are stablecoins? What are the top 10 stablecoins in market value? Aug 07, 2025 pm 10:57 PM

Stable coins are cryptocurrencies whose value is linked to stable assets such as the US dollar or gold. They aim to solve the problem of large price fluctuations in currencies such as Bitcoin. They achieve price stability through an anchoring mechanism and are mainly divided into three categories: 1. Stable coins with legal currency collateral, such as USDT and USDC, are supported by US dollar reserves, and users can exchange 1:1; 2. Stable coins with crypto assets collateral, such as DAI and crvUSD, are generated by over-collateralized digital assets such as Ethereum, and have decentralized characteristics; 3. Algorithmic stable coins, such as USDD, rely on algorithms to adjust supply and demand to maintain currency value, and have no direct asset collateral, and are at a high risk. The top 10 stablecoins currently ranked in market capitalization include: 1. USDT, the earliest and most liquid dollar stablecoins; 2. USDC, to comply with and

See all articles