


Use Spring Boot and Spring AI to build generative artificial intelligence applications
Apr 28, 2024 am 11:46 AMAs 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."
上面內(nèi)容代表了提示詞模板。按照建議,應(yīng)作為提示詞的一部分提供清晰的主題,清晰的任務(wù)含義以及額外的有用信息,以提高結(jié)果的準(zhǔn)確性。
提示詞包含三個(gè)參數(shù)
count - 希望作為輸出的原因數(shù)量
job - 感興趣的領(lǐng)域或工作
location - 工作申請(qǐng)者所在的國(guó)家,城鎮(zhèn),地區(qū)等
概念驗(yàn)證
在這篇文章中,簡(jiǎn)單的概念驗(yàn)證目標(biāo)如下:
將 Spring AI 集成到Spring Boot應(yīng)用程序并使用它
允許客戶端通過(guò)應(yīng)用程序與 Open AI 進(jìn)行通信
客戶端向應(yīng)用程序發(fā)出參數(shù)化的HTTP請(qǐng)求
應(yīng)用程序使用一個(gè)提示詞來(lái)創(chuàng)建輸入,發(fā)送給 Open AI 并獲取輸出
應(yīng)用程序?qū)㈨憫?yīng)發(fā)送給客戶端
圖片
項(xiàng)目設(shè)置
Java 21
Maven 3.9.2
Spring Boot – v. 3.2.2
Spring AI – v. 0.8.0-SNAPSHOT (仍在開(kāi)發(fā),實(shí)驗(yàn)性)
代碼實(shí)現(xiàn)
Spring AI 集成
通常,這是一個(gè)基本步驟,不一定值得一提。然而,因?yàn)?Spring AI 目前以快照形式發(fā)布,為了能夠集成 Open AI 自動(dòng)配置依賴,你需要添加一個(gè)引用到 Spring 快照倉(cāng)庫(kù)。
<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 依賴項(xiàng)。
<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-openai-spring-boot-starter</artifactId><version>0.8.0-SNAPSHOT</version></dependency>
Open AI ChatClient 現(xiàn)在是應(yīng)用程序類(lèi)路徑的一部分。它是用來(lái)向 Open AI 發(fā)送輸入并獲取輸出的組件。
為了能夠連接到AI模型,需要在 application.properties 文件中設(shè)置 spring.ai.openai.api-key 屬性。
spring.ai.openai.api-key = api-key-value
它的值代表了用戶的有效API密鑰,用戶將通過(guò)此密鑰進(jìn)行通信。通過(guò)訪問(wèn)[資源2],可以注冊(cè)或登錄并生成一個(gè)。
客戶端 - Spring Boot應(yīng)用程序通信
概念驗(yàn)證的第一部分是客戶端應(yīng)用程序(例如瀏覽器,curl等)與開(kāi)發(fā)的應(yīng)用程序之間的通信。這是通過(guò)一個(gè) REST 控制器實(shí)現(xiàn)的,可以通過(guò)HTTP GET請(qǐng)求訪問(wèn)。
URL路徑是 /job-reasons,還有之前在定義提示時(shí)概述的三個(gè)參數(shù),這將導(dǎo)致如下格式:
/job-reasons?count={count}&job={job}&locatinotallow={location}
和相應(yīng)的控制器:
@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();}}
由于來(lái)自 Open AI 的響應(yīng)將是一個(gè)字符串,因此控制器返回一個(gè)封裝了字符串的ResponseEntity。如果我們運(yùn)行應(yīng)用程序并發(fā)出請(qǐng)求,當(dāng)前響應(yīng)體部分沒(méi)有返回任何內(nèi)容。
客戶端 - Open AI 通信
Spring AI 目前主要關(guān)注處理語(yǔ)言并產(chǎn)生語(yǔ)言或數(shù)字的AI模型。在前一類(lèi)別中, Open AI 模型的例子包括GPT4-openai或GPT3.5-openai。
為了與這些AI模型(實(shí)際上是指 Open AI 算法)進(jìn)行交互, Spring AI 提供了一個(gè)統(tǒng)一的接口。
ChatClient接口目前支持文本輸入和輸出,并具有簡(jiǎn)單的契約。
@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);}
確實(shí)如此,功能接口的實(shí)際方法通常是被使用的方法。
在我們的概念驗(yàn)證中,這正是我們所需要的,一種調(diào)用 Open AI 并發(fā)送目標(biāo)參數(shù)化 Prompt 作為參數(shù)的方式。我們定義了以下的OpenAiService,在其中注入了一個(gè) ChatClient 的實(shí)例。
@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();}}
如果應(yīng)用程序正在運(yùn)行,那么可以從瀏覽器執(zhí)行以下請(qǐng)求:
http://localhost:8080/gen-ai/job-reasons?count=3&job=software%20architect&locatinotallow=Romania
這下面的結(jié)果被檢索出來(lái)的結(jié)果:
利潤(rùn)豐裕的職業(yè):軟件架構(gòu)師的工作提供了有競(jìng)爭(zhēng)力的薪酬和出色的增長(zhǎng)機(jī)會(huì),確保在羅馬尼亞的財(cái)務(wù)穩(wěn)定和成功。
熱門(mén)職業(yè):隨著技術(shù)需求的持續(xù)增長(zhǎng),軟件架構(gòu)師在羅馬尼亞和全世界都備受追捧,提供了豐富的就業(yè)前景和就業(yè)保障。
創(chuàng)造性問(wèn)題解決:軟件架構(gòu)師在設(shè)計(jì)和開(kāi)發(fā)創(chuàng)新軟件解決方案中扮演著至關(guān)重要的角色,使他們可以釋放他們的創(chuàng)造力,并對(duì)各種行業(yè)產(chǎn)生重大影響。
這就是我們所期望的——一個(gè)簡(jiǎn)易的接口,通過(guò)它,可以要求 Open AI GPT模型寫(xiě)出一些原因,解釋為何在特定地點(diǎn)的特定工作具有吸引力。
調(diào)整和觀察
到目前為止,開(kāi)發(fā)的簡(jiǎn)單概念驗(yàn)證主要使用了默認(rèn)的配置。
ChatClient實(shí)例可以通過(guò)各種屬性根據(jù)所需需要來(lái)配置。雖然這超出了本文的范圍,但在這里舉兩個(gè)例子。
spring.ai.openai.chat.options.model 指定要使用的AI模型。默認(rèn)為'gpt-35-turbo',但'gpt-4'和'gpt-4-32k'指定的是最新版本。雖然這些版本都是可用的,但你可能無(wú)法使用按使用量付費(fèi)的計(jì)劃來(lái)訪問(wèn)這些版本,但 Open AI 網(wǎng)站上有更多的信息可以幫助你了解如何適應(yīng)它。
另一個(gè)值得一提的屬性是 spring.ai.openai.chat.options.temperature。根據(jù)參考文檔,采樣溫度控制了“回應(yīng)的創(chuàng)新性”。據(jù)說(shuō),較高的值會(huì)讓輸出“更隨機(jī)”,而較低的值會(huì)“更專(zhuān)注和決定性”。默認(rèn)值為0.8,如果我們將其降低到0.3,重啟應(yīng)用并再次使用相同的請(qǐng)求參數(shù)詢問(wèn),下面的結(jié)果將被檢索出來(lái)。
有利可圖的職業(yè)機(jī)會(huì):羅馬尼亞的軟件架構(gòu)師工作提供有競(jìng)爭(zhēng)力的薪水和極好的成長(zhǎng)前景,對(duì)于尋求財(cái)務(wù)穩(wěn)定和職業(yè)發(fā)展的個(gè)人來(lái)說(shuō),這是一個(gè)吸引人的職業(yè)選擇。
具有挑戰(zhàn)性和智能刺激的工作:作為一名軟件架構(gòu)師,你將負(fù)責(zé)設(shè)計(jì)和實(shí)現(xiàn)復(fù)雜的軟件系統(tǒng),解決復(fù)雜的技術(shù)問(wèn)題,并與有才華的團(tuán)隊(duì)合作。這個(gè)角色提供了持續(xù)的學(xué)習(xí)機(jī)會(huì)和在尖端技術(shù)上工作的機(jī)會(huì)。
高需求和工作保障:隨著對(duì)技術(shù)和數(shù)字化轉(zhuǎn)型的依賴增加,各行各業(yè)對(duì)熟練軟件架構(gòu)師的需求在上升。選擇在羅馬尼亞的軟件架構(gòu)師工作確保了工作安全和廣泛的就業(yè)選擇,無(wú)論是在本地還是國(guó)際上。
可以看出,這種情況下的輸出更具描述性。
最后一個(gè)考慮因素是與獲取的輸出的結(jié)構(gòu)相關(guān)的。擁有將實(shí)際接收的有效載荷映射到Java對(duì)象(例如,類(lèi)或記錄)的能力將非常方便。截至目前,表示形式是文本形式,實(shí)現(xiàn)也是如此。輸出解析器可能實(shí)現(xiàn)這一點(diǎn),類(lèi)似于Spring JDBC的映射結(jié)構(gòu)。
在這個(gè)概念驗(yàn)證中,我們使用了一個(gè)BeanOutputParser,它允許直接將結(jié)果反序列化到Java記錄中,如下所示:
public record JobReasons(String job, String location, List<String> reasons) {}
通過(guò)將 {format} 作為提示文本的一部分,并將其作為指示提供給 AI 模型。
OpenAiService 方法變?yōu)椋?/span>
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());}
再次調(diào)用時(shí),輸出如下:
{"job":"software architect","location":"Romania","reasons":["High demand","Competitive salary","Opportunities for growth"]}
格式符合預(yù)期,但解釋的原因似乎較少,這意味著需要進(jìn)行額外的調(diào)整以達(dá)到更好的可用性。然而,從概念驗(yàn)證的角度來(lái)看,這是可接受的,因?yàn)榻裹c(diǎn)是形式。
結(jié)論
提示設(shè)計(jì)是任務(wù)的重要部分 - 提示越清晰,輸入越好,輸出的質(zhì)量也就越高。
使用 Spring AI 與各種聊天模型進(jìn)行集成非常簡(jiǎn)單 - 本篇文章展示了一個(gè) Open AI 的集成。
然而,對(duì)于 Gen AI 或者幾乎任何技術(shù)來(lái)說(shuō),首先至少要熟悉基本概念是非常重要的。然后,嘗試?yán)斫馊绾芜M(jìn)行通訊的魔法,最后再開(kāi)始編寫(xiě)“生產(chǎn)”代碼。
最后但同樣重要的是,建議進(jìn)一步探索 Spring AI API,以了解實(shí)現(xiàn)并隨著其不斷發(fā)展和改進(jìn)保持最新?tīng)顟B(tài)。
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

As July 2025 approaches, the crypto market is hotly discussing which tokens may bring high returns. Are names like Pi, PEPE and FloppyPepe really worth the risky investment? Potential cryptocurrencies worth paying attention to in July 2025: virtual fire or real gold? As mid-2025, the heat of discussions on high-yield crypto assets continues to heat up. Bitcoin trends and "altcoin season" expectations have attracted investors' attention. Do tokens like PiNetwork, PEPE and FloppyPepe have the potential to bring considerable investment returns? Let's analyze its prospects one by one. Altcoin Market: Can July get what it wants? Against the backdrop of Bitcoin’s expected record of historical highs, the “altcoin season” seems to be brewing. Back

Contents 1. What is ICN? 2. ICNT latest updates 3. Comparison and economic model between ICN and other DePIN projects and economic models 4. Conclusion of the next stage of the DePIN track At the end of May, ICN (ImpossibleCloudNetwork) @ICN_Protocol announced that it had received strategic investment in NGPCapital with a valuation of US$470 million. Many people's first reaction was: "Has Xiaomi invested in Web3?" Although this was not Lei Jun's direct move, the one who had bet on Xiaomi, Helium, and WorkFusion

In today's era of rapid development of technology, the integration of artificial intelligence and blockchain is gradually becoming a new trend. The Sahara AI (SAHARA) project came into being, and it is committed to creating the first full-stack AI native blockchain platform, making the future of artificial intelligence more accessible, fair and just, and open to everyone.

BNB is a platform token issued by Binance and has now become a native functional token of the BNB Chain ecosystem. Its main uses include 1. Transaction fee discounts; 2. BNB Chain fuel fee; 3. Participate in the Launchpad project; 4. Payment and consumption. The recommended orders of top exchanges are: 1. Binance, providing the deepest BNB liquidity; 2. Ouyi, comprehensive product line; 3. Huobi, stable and safe operation; 4. Gate.io, rich currency selection; 5. KuCoin, many emerging projects; 6. Kraken, famous for its safety and compliance.

How do novice users choose a safe and reliable stablecoin platform? This article recommends the Top 10 stablecoin platforms in 2025, including Binance, OKX, Bybit, Gate.io, HTX, KuCoin, MEXC, Bitget, CoinEx and ProBit, and compares and analyzes them from dimensions such as security, stablecoin types, liquidity, user experience, fee structure and additional functions. The data comes from CoinGecko, DefiLlama and community evaluation. It is recommended that novices choose platforms that are highly compliant, easy to operate and support Chinese, such as KuCoin and CoinEx, and gradually build confidence through a small number of tests.

Against the backdrop of violent fluctuations in the cryptocurrency market, investors' demand for asset preservation is becoming increasingly prominent. This article aims to answer how to effectively hedge risks in the turbulent currency circle. It will introduce in detail the concept of stablecoin, a core hedge tool, and provide a list of TOP3 stablecoins by analyzing the current highly recognized options in the market. The article will explain how to select and use these stablecoins according to their own needs, so as to better manage risks in an uncertain market environment.

This article will discuss the world's mainstream stablecoins and analyze which stablecoins have the risk aversion attribute of "gold substitute" in the market downward cycle (bear market). We will explain how to judge and choose a relatively stable value storage tool in a bear market by comparing the market value, endorsement mechanism, transparency, and comprehensively combining common views on the Internet, and explain this analysis process.

Explore Remittix (RTX), Monero (XMR) and Crypto-Fiat Trends: How these projects shape the future of cryptocurrencies through practicality and community orientation. Remittix, Monero and Cryptocurrency Evolution: What is the hottest speculation? The crypto market is always in a dynamic change, and new and old projects are competing for investors' attention. Currently, Remittix (RTX), Monero (XMR) and crypto-fiat currency directions are becoming the focus of discussion. Let’s find out what driving forces are behind this wave of popularity? Remittix: The emerging token with emerging potential is gradually gaining market attention, and its development trajectory has been compared to the early stages of Bitcoin and Ethereum by some people. "CryptoR
