之前分享了關(guān)于Spring新項目Spring AI
的介紹視頻。視頻里演示了關(guān)于使用Spring AI將Open AI的能力整合到Spring應(yīng)用中的操作,但有不少讀者提到是否有博客形式的學習內(nèi)容。所以,本文就將具體介紹如何使用 Spring AI 快速讓您的Spring應(yīng)用擁有生成式AI的強大能力。
動手試試
第一步:使用你最喜歡的IDE來生成一個基礎(chǔ)的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>
第四步:創(chuàng)建OpenAIController.java
@RestController
@RequestMapping("/api/v1")
public class OpenAIController {
private final AiClient aiClient;
public OpenAIController(AiClient aiClient) {
this.aiClient = aiClient;
}
}
第五步:使用AiClient
對象來根據(jù)接口輸入返回內(nèi)容:
@GetMapping("/completion")
public String completion(@RequestParam(value = "message") String message){
return this.aiClient.generate(message);
}
這是一個最簡單的例子,而實際真正應(yīng)用的時候,我們還需要Prompt
來獲得更精準的結(jié)果。比如,下面這樣:
@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
創(chuàng)建一個模版,然后根據(jù)用戶輸入使用模版來創(chuàng)建具體的Prompt
生成結(jié)果。
這里我們提到的Prompt
類,其實是一系列Message
對象的結(jié)構(gòu)化持有者,每個對象代表完整Prompt
的一部。每個Message
都有著不同的內(nèi)容和目的,這種設(shè)置有助于與人工智能模型進行復雜而細致的交流,因為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模版:SystemPromptTemplate
和PromptTemplate
,以完成更好的生成結(jié)果。
完成這幾個API的構(gòu)建之后,您可以嘗試啟動它,并用API測試工具調(diào)用試試,體驗一下生成式AI的強大能力。
好了,今天的分享就到這里,感謝閱讀!如果您學習過程中如遇困難?可以加入我們超高質(zhì)量的Spring技術(shù)交流群,參與交流與討論,更好的學習與進步!更多Spring Boot教程可以點擊直達!,歡迎收藏與轉(zhuǎn)發(fā)支持!文章來源:http://www.zghlxwxcb.cn/news/detail-777081.html
歡迎關(guān)注我的公眾號:程序猿DD。第一時間了解前沿行業(yè)消息、分享深度技術(shù)干貨、獲取優(yōu)質(zhì)學習資源文章來源地址http://www.zghlxwxcb.cn/news/detail-777081.html
到了這里,關(guān)于使用Spring AI讓你的Spring Boot應(yīng)用快速擁有生成式AI能力的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!