SpringAi入门篇

Spring Ai官网

文本输出

  1. 在pom.xml中引入依赖
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.bjpowernode</groupId>
<artifactId>spring-ai-01-chat</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>spring-ai-01-chat</name>
<description>spring-ai-01-chat</description>

<properties>
<java.version>17</java.version>
<spring-ai.version>1.0.0-SNAPSHOT</spring-ai.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--spring ai的starter依赖,启动依赖,起步依赖-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!--相当于是继承一个父项目:spring-ai-bom父项目-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

<!--配置本项目的仓库:因为maven中心仓库还没有更新spring ai的jar包-->
<repositories>
<!--快照版本的仓库-->
<repository>
<id>spring-snapshot</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

</project>

  1. 在application.yaml文件加入配置
1
2
3
4
5
6
7
8
9
spring:
ai:
openai:
api-key: #你的api-key,如果没有的话可以到淘宝买一个,不贵,两三块钱
base-url:#你的base-url,相当一个中转站吧,响应的速度快一些,在买api-key时,一般会有base-urld
chat:
options:
#model: gpt-3.5-turbo
temperature: 0.3F
  1. 在controller中
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@RestController
public class ChatController {

/**
* spring-ai 自动装配的,可以直接注入使用
*/
@Resource
private OpenAiChatClient openAiChatClient;

/**
* 调用OpenAI的接口
*
* @param msg 我们提的问题
* @return
*/
@RequestMapping(value = "/ai/chat")
public String chat(@RequestParam(value = "msg") String msg) {
return openAiChatClient.call(msg);
}

/**
* 调用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();
}

/**
* 调用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-4-32k") //gpt的版本,32k是参数量
.withTemperature(0.4F) // 温度越高,回答得比较有创新性,但是准确率会下降,温度越低,回答的准确率会更好
.build()));
return chatResponse.getResult().getOutput().getContent();
}

/**
* 调用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-4-32k") //gpt的版本,32k是参数量
.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
29
30
31
32
@RestController
public class ImageController {

@Resource
private OpenAiImageClient openAiImageClient;


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

@RequestMapping("/ai/image2")
private Object image2(@RequestParam(value = "msg") String msg) {
ImageResponse imageResponse = openAiImageClient.call(new ImagePrompt(msg, OpenAiImageOptions.builder()
.withQuality("hd") //高清图像
.withN(1) //生成1张图片
.withHeight(1024) //生成的图片高度
.withWidth(1024) //生成的图片宽度
.build()));
System.out.println(imageResponse);

String imageUrl = imageResponse.getResult().getOutput().getUrl();
//把图片进行业务处理
return imageResponse.getResult().getOutput();
}
}

语音转文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@RestController
public class TranscriptionController {

@Resource
private OpenAiAudioTranscriptionClient openAiAudioTranscriptionClient;

@RequestMapping(value = "/ai/transcription")
public Object transcription() {
//org.springframework.core.io.Resource audioFile = new ClassPathResource("jfk.flac");
// 将我们的音频放在resources目录下
org.springframework.core.io.Resource audioFile = new ClassPathResource("cat.mp3");
String called = openAiAudioTranscriptionClient.call(audioFile);
System.out.println(called);

return called;
}
}

文本转语音

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@RestController
public class TTSController {

@Resource
private OpenAiAudioSpeechClient openAiAudioSpeechClient;

@RequestMapping(value = "/ai/tts")
public Object tts() {
String text = "2023年全球汽车销量重回9000万辆大关,同比2022年增长11%。分区域看,西欧(14%)、中国(12%)两大市场均实现两位数增长。面对这样亮眼的数据,全球汽车行业却都对2024年的市场前景表示悲观,宏观数据和企业体感之前的差异并非中国独有,在汽车市场中,这是共性问题。";
byte[] bytes = openAiAudioSpeechClient.call(text);
FileUtils.save2File("D:\\SpringAI\\test.mp3", bytes);
return "OK";
}

@RequestMapping(value = "/ai/tts2")
public Object tts2() {
String text = "Spring AI is an application framework for AI engineering. Its goal is to apply to the AI domain Spring ecosystem design principles such as portability and modular design and promote using POJOs as the building blocks of an application to the AI domain.";
byte[] bytes = openAiAudioSpeechClient.call(text);
FileUtils.save2File("D:\\SpringAI\\test2.mp3", bytes);
return "OK";
}
}

多种方式输入(文本,图片分析)生成应答

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
/**
* 多模型控制器
*
* @author admin
* @date 2024/05/20
*/
@RestController
public class MultiModelController {

@Resource
private ChatClient chatClient;

@RequestMapping(value = "/ai/multi")
public Object multi(String msg, String imageUrl) {

UserMessage 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()));

System.out.println(response.getResult().getOutput());
return response.getResult().getOutput().getContent();
}
}