Use Spring AI to quickly equip your Spring Boot application with generative AI capabilities

I previously shared Spring AIan introductory video about Spring’s new project . The video demonstrates how to use Spring AI to integrate Open AI capabilities into Spring applications, but many readers mentioned whether there is learning content in the form of blogs. Therefore, this article will introduce in detail how to use Spring AI to quickly equip your Spring application with the powerful capabilities of generative AI.

Give it a try

Step 1: Use your favorite IDE to generate a basic Spring Boot project. If you don’t know this yet, it is recommended to go to Spring Boot Quick Start to learn first.

Step 2: pom.xmlIntroduce dependencies. There are currently two, Azure OpenAI and OpenAI, just choose which one you are using.

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

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

In addition, because you are using the SNAPSHOT version, remember to configure:

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

Step 3: Open application.propertiesand configure youropenai api key

spring.ai.openai.api-key=<YOUR_OPENAI_API_KEY>

Step 4: CreateOpenAIController.java

@RestController
@RequestMapping("/api/v1")
public class OpenAIController {

    private final AiClient aiClient;

    public OpenAIController(AiClient aiClient) {
        this.aiClient = aiClient;
    }
}

Step 5: Use AiClientobjects to return content based on interface input:

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

This is the simplest example, but in actual application, we need to Promptobtain more accurate results. For example, as follows:

@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);
}

Create a template by using PromptTemplateit, and then use the template to create specific Promptgenerated results based on user input.

The class we mentioned here Promptis actually Messagea structured holder of a series of objects, each object representing a complete Promptpart. Each Messagehas a different content and purpose, and this setup facilitates complex and nuanced communication with the AI ​​model, as it is Promptcomposed of various messages, each of which is assigned a specific function within the conversation.

Here's a more complex way to use it:

@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 TranslatePro
            You 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();
}

A List type Message is used here Prompt, including multiple different levels of Prompt templates: SystemPromptTemplateand PromptTemplate, to achieve better generation results.

After completing the construction of these APIs, you can try to start it and try calling it with the API testing tool to experience the powerful capabilities of generative AI.

Okay, that’s it for today’s sharing, thanks for reading! What if you encounter difficulties while studying? You can join our super-high-quality Spring technology exchange group to participate in exchanges and discussions for better learning and progress! For more Spring Boot tutorials, click here! , welcome to collect and forward for support!

Welcome to follow my public account: Programmer DD. If we had known about cutting-edge technology, there would be hope for overtaking on curves! To accumulate overtaking capital, start by paying attention to DD!

おすすめ

転載: blog.csdn.net/dyc87112/article/details/135465773