使用Spring AI讓你的Spring Boot應用快速擁有生成式AI能力

之前分享了關於Spring新項目Spring AI的介紹視頻。視頻裏演示了關於使用Spring AI將Open AI的能力整合到Spring應用中的操作,但有不少讀者提到是否有博客形式的學習內容。所以,本文就將具體介紹如何使用 Spring AI 快速讓您的Spring應用擁有生成式AI的強大能力。

動手試試

第一步:使用你最喜歡的IDE來生成一個基礎的Spring Boot項目。如果您還不會這個,建議先前往Spring Boot快速入門學習。

第二步:pom.xml中引入依賴。當前分爲兩個,Azure OpenAI和OpenAI,選擇其中一個你在用的即可。

<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>

另外,因爲用的是SNAPSHOT版本,記得配置:

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

第三步:打開application.properties,配置您的openai api key

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

第四步:創建OpenAIController.java

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

    private final AiClient aiClient;

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

第五步:使用AiClient對象來根據接口輸入返回內容:

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

這是一個最簡單的例子,而實際真正應用的時候,我們還需要Prompt來獲得更精準的結果。比如,下面這樣:

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

通過使用PromptTemplate創建一個模版,然後根據用戶輸入使用模版來創建具體的Prompt生成結果。

這裏我們提到的Prompt類,其實是一系列Message對象的結構化持有者,每個對象代表完整Prompt的一部。每個Message都有着不同的內容和目的,這種設置有助於與人工智能模型進行復雜而細緻的交流,因爲Prompt由各種消息組成,每條消息在對話中都指定了特定的功能。

下面是一個更復雜的使用方式:

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

這裏Prompt使用了List類型的Message,包含了多個不同級別的Prompt模版:SystemPromptTemplatePromptTemplate,以完成更好的生成結果。

完成這幾個API的構建之後,您可以嘗試啓動它,並用API測試工具調用試試,體驗一下生成式AI的強大能力。

好了,今天的分享就到這裏,感謝閱讀!如果您學習過程中如遇困難?可以加入我們超高質量的Spring技術交流羣,參與交流與討論,更好的學習與進步!更多Spring Boot教程可以點擊直達!,歡迎收藏與轉發支持!

歡迎關注我的公衆號:程序猿DD。第一時間瞭解前沿行業消息、分享深度技術乾貨、獲取優質學習資源

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章