编程新范式,当Spring Boot遇上OpenAI

人工智能
随着GenAI(I(General Artificial Intelligence))技术的不断发展,简化具有AI功能的应用程序的创建成为一个非常重要的课题和迫切需求。“Spring AI”就是在这种背景下诞生的,旨在简化具有AI功能应用程序的开发,使其成为简单直观的过程,避免不必要的复杂性。

2023年,AI技术已经成为一个热点话题,影响了许多领域,特别是编程领域。人们越来越意识到AI技术的重要性,包括Spring社区在内。

随着GenAI(I(General Artificial Intelligence))技术的不断发展,简化具有AI功能的应用程序的创建成为一个非常重要的课题和迫切需求。“Spring AI”就是在这种背景下诞生的,旨在简化具有AI功能应用程序的开发,使其成为简单直观的过程,避免不必要的复杂性。

本文介绍Spring AI和使用Spring AI的一些提示工程技巧,帮助开发人员在使用Spring AI框架中时更好地构建提示信息,以便利用好Spring AI的功能。

1 Spring AI介绍

Spring AI由M K Pavan Kumar创建和撰写Spring AI由M K Pavan Kumar创建和撰写

Spring AI是一个旨在简化AI应用程序开发的项目,它借鉴了已知的Python项目LangChain和LlamaIndex的经验。然而,Spring AI不只是这些项目的复制品。Spring AI的核心理念是,未来的生成式AI应用将扩展到各种编程语言的用户群体,不再只局限于Python语言的爱好者。这意味着,开发人员无需专门学习Python语言就可以开发AI应用,可以使用他们熟悉的语言来构建AI应用。

Spring AI的核心是提供构建AI驱动应用程序的基本构建块。这些构建块具有弹性,可以轻松交换组件,几乎不需要对代码进行任何修改。例如,Spring AI引入了兼容OpenAI和Azure OpenAI的ChatClient接口。

Spring AI的核心是为创建AI驱动的应用程序提供基本的构建块。这些构建块具有弹性,允许组件的平滑交换,几乎不需要对编码进行任何修改。例如,Spring AI引入了兼容OpenAI和Azure OpenAI的ChatClient接口。

但Spring AI不仅仅是这些基本构建块,还关注提供更高级的解决方案,例如“关于自己文档的问答”或“使用文档进行交互式聊天”等典型场景。随着应用程序需求的增长,Spring AI计划与Spring生态系统的其他部分密切合作,包括Spring Integration,Spring Batch和Spring Data等。

2 创建Spring Boot项目和编写OpenAI控制器示例

先在IDE中生成Spring Boot项目,在application.properties文件中保留以下内容:

spring.ai.openai.api-key=<YOUR\_OPENAI\_API\_KEY>

下面编写名为OpenAIController.java的控制器:

package com.vas.springai.controller;

import org.springframework.ai.client.AiClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
public class OpenAIController {

    private final AiClient aiClient;


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

3 使用Prompt类构建提示信息

提示类是一个消息对象序列的结构化持有者,每个消息都代表提示的一部分。这些消息在提示中扮演着不同的角色和目的,内容也各不相同。包括用户问题、AI生成的响应以及相关上下文细节等等。这种设置有助于进行复杂和精细的人机交互,因为提示由多个具有特定功能的消息组成。

@GetMapping("/completion")
public String completion(@RequestParam(value = "message") String message){
  return this.aiClient.generate(message);
}

然而,aiClient的generate方法并不仅仅接受纯文本作为参数,它也可以接受Prompt类的对象作为参数,如下所示。现在,这个方法返回的是AiResponse类型的实例,不是简单的文本。

@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);
}

此外,Prompt类还提供了一个重载的构造函数,可以接受不同角色和意图的Message类型实例序列作为参数。这样可以更好地组织和管理提示信息,方便后续的处理和使用。下面是一个示例代码,展示了如何使用这个重载构造函数来合并所有内容。

package com.vas.springai.controller;

import org.springframework.ai.client.AiClient;
import org.springframework.ai.client.Generation;
import org.springframework.ai.prompt.Prompt;
import org.springframework.ai.prompt.PromptTemplate;
import org.springframework.ai.prompt.SystemPromptTemplate;
import org.springframework.ai.prompt.messages.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class OpenAIController {

    private final AiClient aiClient;


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

    @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();
    }
}

4 测试应用程序

可以使用市场上任何可用的开放工具来测试应用程序,例如postman、insomnia和Httpie等等。

图片 图片

责任编辑:武晓燕 来源: Java学研大本营
相关推荐

2021-10-23 09:20:39

AI

2016-10-21 15:57:39

Rust编辑语言Fedora

2013-05-22 09:33:09

交互设计设计时间

2022-02-24 16:15:16

OpenHarmon鸿蒙OpenEuler

2023-04-27 07:40:08

Spring框架OpenAI

2017-06-28 11:34:55

锐捷 医疗 物联网

2015-01-07 15:49:21

大数据SSD

2017-08-18 14:47:31

DDD微服务架构

2017-05-16 14:38:25

2016-10-21 09:45:20

RustFedoraJava

2011-03-16 14:51:35

2013-08-22 11:08:27

大数据商业只能Hadoop

2013-11-08 09:15:32

大数据平板电脑

2017-09-11 13:55:30

前端JavaScript物联网

2009-03-21 16:43:29

SOA虚拟化IT

2022-06-27 13:56:10

设计模式缓存分布式系统

2017-11-01 14:09:30

大数据心理学新零售

2020-12-22 09:31:43

区块链安全食品

2019-11-19 09:54:59

NBA机器学习数据

2020-03-05 16:35:06

人脸识别口罩人工智能
点赞
收藏

51CTO技术栈公众号