前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Spring AI让你的Spring Boot应用快速拥有生成式AI能力

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

作者头像
程序猿DD
发布2024-01-11 14:38:28
2790
发布2024-01-11 14:38:28
举报
文章被收录于专栏:程序猿DD程序猿DD

之前分享了关于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

代码语言:javascript
复制
spring.ai.openai.api-key=<YOUR_OPENAI_API_KEY>

第三步:创建OpenAIController.java

代码语言:javascript
复制
@RestController
@RequestMapping("/api/v1")
public class OpenAIController {

    private final AiClient aiClient;

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

第四步:使用AiClient对象来根据接口输入返回内容:

代码语言:javascript
复制
@GetMapping("/completion")
public String completion(@RequestParam(value = "message") String message){
  return this.aiClient.generate(message);
}

这是一个最简单的例子,而实际真正应用的时候,我们还需要Prompt来获得更精准的结果。比如,下面这样:

代码语言:javascript
复制
@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由各种消息组成,每条消息在对话中都指定了特定的功能。

下面是一个更复杂的使用方式:

代码语言:javascript
复制
@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的强大能力。

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2024-01-08,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 程序猿DD 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 动手试试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com