之前分享了关于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模版:SystemPromptTemplate和PromptTemplate,以完成更好的生成结果。
完成这几个API的构建之后,您可以尝试启动它,并用API测试工具调用试试,体验一下生成式AI的强大能力。
好了,今天的分享就到这里,感谢阅读!如果您学习过程中如遇困难?可以加入我们超高质量的Spring技术交流群,参与交流与讨论,更好的学习与进步!更多Spring Boot教程可以点击直达!,欢迎收藏与转发支持!
欢迎关注我的公众号:程序猿DD。第一时间了解前沿行业消息、分享深度技术干货、获取优质学习资源
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |