国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
1 Introduction to Spring AI
2 Create a Spring Boot project and write an OpenAI controller example
3 Use the Prompt class to build prompt information
4 Testing the application
Home Technology peripherals AI A new programming paradigm, when Spring Boot meets OpenAI

A new programming paradigm, when Spring Boot meets OpenAI

Feb 01, 2024 pm 09:18 PM
ai spring openai

In 2023, AI technology has become a hot topic and has had a huge impact on various industries, especially in the programming field. People are increasingly aware of the importance of AI technology, and the Spring community is no exception.

With the continuous advancement of GenAI (General Artificial Intelligence) technology, it has become crucial and urgent to simplify the creation of applications with AI functions. Against this background, "Spring AI" emerged, aiming to simplify the process of developing AI functional applications, making it simple and intuitive and avoiding unnecessary complexity. Through "Spring AI", developers can more easily build applications with AI functions, making them easier to use and operate. This not only helps improve development efficiency, but also accelerates the popularization and application of AI technology. In short, "Spring AI" brings new possibilities to the development of AI applications, providing developers with simpler and more intuitive tools and frameworks.

This article will briefly introduce the Spring AI framework and some engineering tips for using the framework. Developers can use these tips to better structure prompt information and fully utilize the capabilities of Spring AI.

1 Introduction to Spring AI

編程新范式,當(dāng)Spring Boot遇上OpenAISpring AI is created and written by M K Pavan Kumar

Spring AI is a tool designed to simplify AI applications Developed project inspired by the Python projects LangChain and LlamaIndex. However, Spring AI is not a simple copy. Its core idea is to open generative AI applications to users of various programming languages, not just Python language enthusiasts. This means developers can build AI applications using a language they are familiar with without having to learn the Python language. With Spring AI, developers can more easily harness the power of AI to solve a variety of problems, regardless of which programming language they use. This will facilitate broader AI application development and provide developers with more flexibility and choice.

The core goal of Spring AI is to provide the basic building blocks for building AI-driven applications. These building blocks are highly flexible and components can be easily swapped with virtually no modifications to the code. One example is that Spring AI introduces a component called the ChatClient interface, which is compatible with OpenAI and Azure OpenAI technologies. This allows developers to switch between different AI service providers without changing the code, making development and integration more convenient.

At its core, Spring AI provides reliable building blocks for developing artificial intelligence-based applications. The elasticity of these modules enables smooth swapping of components without requiring extensive modifications to the coding. One example is Spring AI's introduction of the ChatClient interface, which is compatible with OpenAI and Azure OpenAI, allowing developers to easily talk to both platforms. This compatibility allows developers to choose the appropriate platform based on actual needs without having to rewrite code. With Spring AI, developers can build AI-driven applications more efficiently.

Spring AI goes beyond basic building blocks and focuses on providing more advanced solutions. For example, it can support typical scenarios such as "questions and answers about one's own documents" or "interactive chat using documents". As application needs grow, Spring AI plans to work closely with other components of the Spring ecosystem such as Spring Integration, Spring Batch and Spring Data to meet more complex business needs.

2 Create a Spring Boot project and write an OpenAI controller example

First generate the Spring Boot project in the IDE and keep the following content in the application.properties file:

spring.ai.openai.api-key=<YOUR\_OPENAI\_API\_KEY>

Below Write a controller named OpenAIController.java:

package com.vas.springai.controller;import org.springframework.ai.client.AiClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/api/v1")public class OpenAIController {private final AiClient aiClient;public OpenAIController(AiClient aiClient) {this.aiClient = aiClient;}}

3 Use the Prompt class to build prompt information

The prompt class is a structured holder of a sequence of message objects, each message represents a prompt a part of. These messages have different roles and purposes in the prompt, and their content varies. Includes user questions, AI-generated responses, relevant contextual details, and more. This setup facilitates complex and sophisticated human-computer interactions since the prompt consists of multiple messages with specific functions.

@GetMapping("/completion")public String completion(@RequestParam(value = "message") String message){return this.aiClient.generate(message);}

However, aiClient's generate method does not only accept plain text as a parameter, it can also accept objects of the Prompt class as parameters, as shown below. Now, this method returns an instance of type AiResponse, not simple text.

@GetMapping("/completion")public AiResponse completion(@RequestParam(value = "message") String message){ PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}"); Prompt prompt = promptTemplate.create(Map.of("query", message)); return this.aiClient.generate(prompt);}

In addition, the Prompt class also provides an overloaded constructor that can accept a sequence of Message type instances with different roles and intentions as parameters. This can better organize and manage prompt information and facilitate subsequent processing and use. Below is a sample code showing how to use this overloaded constructor to merge everything.

package com.vas.springai.controller;import org.springframework.ai.client.AiClient;import org.springframework.ai.client.Generation;import org.springframework.ai.prompt.Prompt;import org.springframework.ai.prompt.PromptTemplate;import org.springframework.ai.prompt.SystemPromptTemplate;import org.springframework.ai.prompt.messages.Message;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.List;import java.util.Map;@RestController@RequestMapping("/api/v1")public class OpenAIController {private final AiClient aiClient;public OpenAIController(AiClient aiClient) {this.aiClient = aiClient;}@GetMapping("/completion")public List<Generation> completion(@RequestParam(value = "message") String message) {String systemPrompt = """You are a helpful AI assistant that helps people translate given text from english to french.Your name is TranslateProYou should reply to the user's request with your name and also in the style of a professional.""";SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemPrompt);Message systemMessage = systemPromptTemplate.createMessage();PromptTemplate promptTemplate = new PromptTemplate("translate the given english sentence sentence into french {query}");Message userMessage = promptTemplate.createMessage(Map.of("query", message));Prompt prompt = new Prompt(List.of(systemMessage, userMessage));return this.aiClient.generate(prompt).getGenerations();}}

4 Testing the application

You can use any open tool available on the market to test the application, such as postman, insomnia, Httpie, etc.

編程新范式,當(dāng)Spring Boot遇上OpenAI picture

The above is the detailed content of A new programming paradigm, when Spring Boot meets OpenAI. 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 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)

2025 Stablecoin Investment Tutorial How to Choose a Safe Stablecoin Platform 2025 Stablecoin Investment Tutorial How to Choose a Safe Stablecoin Platform Jul 07, 2025 pm 09:09 PM

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.

How to avoid risks in the turmoil in the currency circle? The TOP3 stablecoin list is revealed How to avoid risks in the turmoil in the currency circle? The TOP3 stablecoin list is revealed Jul 08, 2025 pm 07:27 PM

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.

Global stablecoin market value PK! Who is the gold substitute in the bear market Global stablecoin market value PK! Who is the gold substitute in the bear market Jul 08, 2025 pm 07:24 PM

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.

The popularity of the currency circle has returned, why do smart people have begun to quietly increase their positions? Look at the trend from the on-chain data and grasp the next round of wealth password! The popularity of the currency circle has returned, why do smart people have begun to quietly increase their positions? Look at the trend from the on-chain data and grasp the next round of wealth password! Jul 09, 2025 pm 08:30 PM

As the market conditions pick up, more and more smart investors have begun to quietly increase their positions in the currency circle. Many people are wondering what makes them take decisively when most people wait and see? This article will analyze current trends through on-chain data to help readers understand the logic of smart funds, so as to better grasp the next round of potential wealth growth opportunities.

Review of the most complete historical price of Ethereum ETH 2010-2025 (the latest version in 2025) Review of the most complete historical price of Ethereum ETH 2010-2025 (the latest version in 2025) Jul 07, 2025 pm 09:00 PM

Ethereum price has gone through several critical stages, from $0.70 in 2015 to $3,050 in 2025. 1) From 2015 to 2016, ETH rose from $0.70 to $20.64 in mid-2016; 2) from 2017 to 2018, driven by the ICO boom, reached $1,417 in early 2018, and then fell to $80 due to regulatory concerns; 3) from 2019 to 2020, and rose to $737 under DeFi; 4) from 2021, hit a new high of $4,864, and then fell to $1,200-2,000 due to PoS transformation; 5) from 2023 to 2024 to about $3,000

What is a stablecoin? What are the types of stable currencies? Is it related to US Treasury bonds? What is a stablecoin? What are the types of stable currencies? Is it related to US Treasury bonds? Jul 07, 2025 pm 08:36 PM

Stable coins are digital currencies that maintain stable value by anchoring specific assets. They are mainly divided into three categories: fiat currency collateral, crypto asset collateral and algorithmic. Among them, fiat currency collateral such as USDT and USDC are widely used, and their reserves are often invested in US Treasury bonds, forming a close connection with the traditional financial system.

Virtual Currency Stable Coins Ranking Which is the 'safe haven' in the currency circle Virtual Currency Stable Coins Ranking Which is the 'safe haven' in the currency circle Jul 08, 2025 pm 07:30 PM

This article will introduce several mainstream stablecoins and explain in depth how to evaluate the security of a stablecoin from multiple dimensions such as transparency and compliance, so as to help you understand which stablecoins are generally considered relatively reliable choices in the market, and learn how to judge their "hazard-haven" attributes on your own.

Bitcoin breaks new highs, Dogecoin rebounds strongly, will Ethereum keep up with the pace Bitcoin breaks new highs, Dogecoin rebounds strongly, will Ethereum keep up with the pace Jul 09, 2025 pm 08:24 PM

Recently, Bitcoin hit a new high, Dogecoin ushered in a strong rebound and the market was hot. Next, we will analyze the market drivers and technical aspects to determine whether Ethereum still has opportunities to follow the rise.

See all articles