Bootstrap

Spring AI应用开发流程

在本文中,将深入探讨如何利用 Spring 框架结合当下最前沿的人工智能技术,实现智能化的企业级应用开发。随着人工智能技术的快速发展,越来越多的企业开始探索将 AI 集成到现有的业务流程中,以提高效率、增强用户体验以及创造新的商业模式。Spring 框架作为 Java 世界中最受欢迎的企业级开发框架之一,提供了丰富的工具和库,支持开发人员构建复杂、高效且可扩展的应用程序。本文将详细介绍如何利用 Spring 的特性,例如依赖注入、AOP(面向切面编程)等,与 AI 技术相结合,例如自然语言处理、机器学习模型部署等,来解决实际问题。

无论是对 Spring 框架有所了解但对 AI 有兴趣的开发者,还是已经具备 AI 技术背景希望拓展至企业应用领域的专业人士,都能从中获得有价值的知识和实践指导。

前期准备

1.本机电脑要可以访问OpenAl网站 https://openai.com/;(科学上网)
2.要有OpenAl的API Key;(注册账号或者购买)

开发Spring AI应用程序

Spring Al应用程序也是基于Spring Boot进行开发;
1、建项目:创建一个Spring Boot项目;
2、加依赖:加入spring-ai-openai-spring-boot-starter依赖:
AI依赖:

<dependency>
        <groupld>org.springframework.ai</groupld>

        <artifactld>spring-ai-openai-spring-boot-starter</artifactld>

</dependency>

创建项目工程

新建SpringBoot模块时注意添加OpenAI的依赖:

项目依赖

注意:由于SpringAI是新出来的项目,所以Spring Boot最好用比较新的版本,这里使用的是Spring Boot版本号是3.2.4,JDK17

项目配置

spring:
    application:
        name:spring-ai-01-chat
ai:
    openai:
        api-key: ${spring.ai.openai.api-key}
        base-url: ${spring.ai.openai.base-url}

AI聊天程序

/**
* spring-ai 自动装配的,可以直接注入使用
*/
@Resource 
private OpenAichatclient openAichatClient;
/**
* 调用openAI的接囗
*
*@param msg 我们提的问题
*@return
*/
@RequestMapping(value = "/ai/chat")
public string chat(@RequestParam(value = "msg") string msg){
    String called =openAichatclient.call(msg);
    return called;
}
/**
* 调用openAI的接囗
*
*@param msg 我们提的问题
*@return
*/
@RequestMapping(value = "/ai/chat")
public string chat(@RequestParam(value = "msg") string msg){
    ChatResponse chatResponse =openAichatclient.call(new Prompt(msg));
    return chatResponse;
}

可选参数:

/**
* 调用openAI的接囗
*
*@param msg 我们提的问题
*@return
*/
@RequestMapping(value = "/ai/chat")
public string chat(@RequestParam(value = "msg") string msg){
    ChatResponse chatResponse =openAichatclient.call(new Prompt(msg) openAichatoptions.builder()
        .withModel("gpt-4-32k")
        .withTemperature(0.4F) //温度越高,回答得比较有创新性,但是准确率会下降、温度越低,回答的准确率会更好);
        .build()))
    return chatResponse;
}

AI程序聊天Stream式API

@RequestMapping(value ="/ai/chat")
public Object chat(@RequestParam(value = "msg") String msg){
//可选参数在配置文件中配置了、在代码中也配置了,那么以代码的配置为准,也就是代码的配置会覆盖掉配置文件中的配置
    Flux<ChatResponse> flux = openAichatclient.stream(new Prompt(msg, OpenAichatOptions.builder()
        .withModel("gpt-4-32k")//gpt的版本,32k是参数最
        .withTemperature(0.4F)//温度越高,回答得比较有创新性,但是准确奉会下降,温度越低,回答的准确率会更好
        .build()));
    flux.tostream().forEach(chatResponse->{
        System.out.println(chatResponse.getResult().getOutput().getContent());
    });
    return flux.collectList();//数据的序列,一字列的数据,一个一个的数据返回
}

AI图像程序

@RequestMapping(value ="/ai/image")
public Object Image(@RequestParam(value = "msg") String msg){
    ImageResponse imageResponse = openAiImageclient.call(new ImagePrompt(msg, OpenAiImageOptions.builder()
        .withQualit("hd")//高清图像
        .withN(1)//生成图片数量
        .withHeight(1024)//图片高度
        .build()));
    System.out.println(imageResponse);
    return imageResponse.getResult().getOutput();
}

AI音频转文本程序

@Resource 1usage
private OpenAiAudioTranscriptionclient openAiAudioTranscriptionclient;
@RequestMapping(valve ="/ai/transcription")
public Object transcription(){
    //org.springframework.core.io.Resource audioFile = new ClassPathResource("jfk.flac");
    org.springframework.core.io.Resource audioFile = new classPathResource("cat.mp3");
    String called = openAiAudioTranscriptionclient.call(audioFile);System.out.println(called);
    return called;
}

AI文本程序转语音

@Resource
private OpenAiAudioSpeechClient openAiAudiospeechclient,
@RequestMapping(value ="/ai/tts")
public object tts(){
    String text ="2023年全球汽车销量区回9000万辆大关,同比2022年增长11%";
    byte [] bytes = openAiAudiospeechclient.call(text);
    save2File(fname:"D:\\springAI\test.mp3",bytes);
    return "OK";
}

;