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
|
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 {
@Resource private OpenAiChatClient openAiChatClient;
@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
|
@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
|
@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
|
@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
|
@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) .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); 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(); }
|