日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

使用Spring AI让你的Spring Boot应用快速拥有生成式AI能力

發布時間:2024/1/11 javascript 73 coder
生活随笔 收集整理的這篇文章主要介紹了 使用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。第一時間了解前沿行業消息、分享深度技術干貨、獲取優質學習資源

總結

以上是生活随笔為你收集整理的使用Spring AI让你的Spring Boot应用快速拥有生成式AI能力的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。