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

50f972e8173341bd1b2d44446e5875ed.png

之前分享了关于Spring新项目Spring AI的介绍视频:

视频里演示了关于使用Spring AI将Open AI的能力整合到Spring应用中的操作,但有不少读者提到是否有博客形式的学习内容。所以,本文就将具体介绍如何使用 Spring AI 快速让您的Spring应用拥有生成式AI的强大能力。

动手试试

第一步:使用你最喜欢的IDE来生成一个基础的Spring Boot项目。如果您还不会这个,建议先前往Spring Boot快速入门(https://www.didispace.com/spring-boot-2/1-2-quick-start.html)学习。

第二步:打开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的强大能力。

你还在购买国内的各种昂贵又低质的技术教程吗?这里给大家推荐下我们自研的Youtube视频语音转换插件(https://youtube-dubbing.com/),一键外语转中文,英语不好的小伙伴也可以轻松的学习油管上的优质教程了,下面是演示视频,可以直观的感受一下:

------

我们创建了一个高质量的技术交流群,与优秀的人在一起,自己也会优秀起来,赶紧点击加群,享受一起成长的快乐。

推荐阅读

··································

点击卡片关注我,分享一线前沿干货

点击阅读原文,直达Java新特性专栏

  • 23
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值