SpringAI

SpringAI

学习了一下新出的SpringAI,这里简单的介绍一下SpringAI的使用方法,基本都是调用各个Spring写好的client的call方法,然后再自己设定一下参数。

创建项目时选择

  • Lombok
  • Spring Boot DevTools
  • Spring Web
  • OpenAl

application.yml配置

1
2
3
4
5
6
7
8
spring:  
application:
name:
spring-ai-chat
ai:
openai:
api-key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
base-url: https://api.openai.com/v1/chat/completions # 填你的key转发地址

ChatClient

call方法

ChatController代码

call方法直接传入string

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@RestController  
public class ChatController {
/**
* springai提供的,自动装配,直接使用
*/
@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;
}
}

在url栏直接输入问题测试代码

call方法的另一种参数Prompt

1
2
3
4
5
6
7
8
9
10
/**  
* 调用OpenAi接口
* @param msg 我们提的问题
* @return
*/
@RequestMapping(value = "/ai/chat2")
public Object chat2(@RequestParam(value = "msg") String msg) {
ChatResponse chatResponse = openAiChatClient.call(new Prompt(msg));
return chatResponse;
}

我们可以拿到这个对象之后,调用chatResponse的一些方法,拿到我们想要的结果,如:通过以下代码,达到和第一种方法一样的效果。

1
2
3
4
5
6
7
8
9
10
/**  
* 调用OpenAi接口
* @param msg 我们提的问题
* @return
*/
@RequestMapping(value = "/ai/chat2")
public Object chat2(@RequestParam(value = "msg") String msg) {
ChatResponse chatResponse = openAiChatClient.call(new Prompt(msg));
return chatResponse.getResult().getOutput().getContent();
}

prompt的可选参数

1
2
3
4
5
6
7
8
9
10
11
12
spring:  
application:
name:
spring-ai-chat
ai:
openai:
api-key: sk-xxxxxxxxxxxxxxxxxx
base-url: https://
chat:
options:
model: gpt-4-32k
temperature: 0.4F

可选参数在配置文件中配置了,在代码中也配置了,那么以代码的配置为准,也就是代码的配置会覆盖掉配置文件中的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**  
* 调用OpenAi接口
* @param msg 我们提的问题
* @return
*/
@RequestMapping(value = "/ai/chat3")
public Object chat3(@RequestParam(value = "msg") String msg) {
ChatResponse chatResponse = openAiChatClient.call(new Prompt(msg, OpenAiChatOptions.builder()
.withModel("gpt-3.5-turbo")
.withTemperature(0.4F) //温度越高,回答比较有创新,回答征准确率会下降,温度越低,回答的准确率更好
.build()));
return chatResponse.getResult().getOutput().getContent();
}
}

stream方法

stream方法,可以让AI流式输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**  
* 调用OpenAi接口
* @param msg 我们提的问题
* @return
*/
@RequestMapping(value = "/ai/chat4")
public Object chat4(@RequestParam(value = "msg") String msg) {
Flux<ChatResponse> flux = openAiChatClient.stream(new Prompt(msg, OpenAiChatOptions.builder()
.withModel("gpt-3.5-turbo")
.withTemperature(0.4F) //温度越高,回答比较有创新,回答征准确率会下降,温度越低,回答的准确率更好
.build()));
flux.toStream().forEach(chatResponse -> {
System.out.println(chatResponse.getResult().getOutput().getContent());
});
return flux.collectList();
}

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28








Java



















ImageClient

call方法

1
2
3
4
5
6
7
@RequestMapping("/ai/image")  
private Object image(@RequestParam(value = "msg") String msg){
ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(msg));
System.out.println(imageResponse);
String imageUrl = imageResponse.getResult().getOutput().getUrl();
return imageResponse.getResult().getOutput();
}

可以选择参数

1
2
3
4
5
ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(msg, OpenAiImageOptions.builder()  
.withQuality("hd") //高清
.withN(2) //生成2张图片
.withHeight(1024)
.withWidth(1024).build()));

同样可以在yml文件中配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:  
application:
name:
spring-ai-image
ai:
openai:
api-key: sk-xxxxxxxx
base-url: https://
image:
options:
model: gpt-4-dalle
n: 2
height: 1024
width: 1024
quality: hd

AudioTranscriptionClient

将音频转换为文字

call方法

1
2
3
4
5
6
@RequestMapping(value = "/ai/transcription")  
public Object transcription() {
org.springframework.core.io.Resource audiofile = new ClassPathResource("classpath:zz");
String called = openAiAudioTranscriptionClient.call(audiofile);
return called;
}

AudioSpeechClient

将文字转换为音频

call方法

1
2
3
4
5
6
7
@RequestMapping(value = "/ai/tts")  
public Object tts() {
String text = "";
byte[] bytes = openAiAudioSpeechClient.call(text);
saveFile("//usr/xx.mp3",bytes); //bytes存储成文件
return "ok";
}

MultiModel

多模态可以同时处理文本和图片

1
2
3
4
5
6
7
8
9
10
11
12
@RequestMapping(value = "/ai/multi")  
public Object multi(String msg,String imageUrl) {
var userMessage = new UserMessage(msg,
List.of(new Media(MimeTypeUtils.IMAGE_PNG,
imageUrl)));

ChatResponse response = chatClient.call(new Prompt(userMessage,
OpenAiChatOptions.builder()
.withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue())
.build()));
return response.getResult().getOutput().getContent();
}