01–SpringAI接入大模型,chatgpt,Java接入人工智能大模型
文章目录
一、准备工作?
①:环境准备
- jdk版本:jdk17
- idea版本:idea2024.1
- 要有一个 open ai-key
- 能【ke】【学】【上】【wang】
二、创建一个springAI项目
①:创建一个根项目

- 把jdk17添加到项目结构中
(不然后面可能会报错 setSdk: sdk '17' type 'JavaSDK' is not registered in ProjectJdkTable)

②:创建一个SpringAI模块




01.解决下载spring-ai依赖报错问题


- 然后在重新加载maven
- 如果还不行,就
重新创建项目或者取消maven链接在将项目添加为maven

02. 添加api-key配置(yml)
spring:
application:
name: spring-ai-01-chat
ai:
openai:
api-key: ${open-ai-key}
base-url: ${open-ai-uri}
server:
port: 8899
03.添加控制层简单测试
@RequestMapping("/ai/chat")
public String chat(@RequestParam(value = "msg") String msg) {
return openAiChatModel.call(msg);
}
04.测试

3️⃣:测试使用gpt-4模型
方法一 添加控制层代码
@RequestMapping("/ai/chat2")
public Object chat2(@RequestParam(value = "msg") String msg) {
ChatResponse response = openAiChatModel.call(new Prompt(msg, OpenAiChatOptions.builder()
.withModel("gpt-4-32k")
.withTemperature(0.4F)
.build()));
return response.getResult().getOutput().getContent();
}
方法二 配置文件中配置
spring:
application:
name: spring-ai-01-chat
ai:
openai:
api-key: ${open-ai-key}
base-url: ${open-ai-uri}
chat:
options:
model: gpt-4-32k
temperature: 0.3
server:
port: 8899
02.测试

4️⃣:使用Stream方式一个一个的返回
@RequestMapping("/ai/chat3")
public Object chat3(@RequestParam(value = "msg") String msg) {
Flux<ChatResponse> stream = openAiChatModel.stream(new Prompt(msg, OpenAiChatOptions.builder()
.withTemperature(0.3F)
.build()));
stream.toStream().forEach(res -> {
System.out.println(res.getResult().getOutput().getContent());
});
return stream.collectList();
}
- 测试


三、Ai图像程序API结构
1️⃣:方式一
01. 代码
@RestController
public class ImgController {
@Resource
private OpenAiImageModel openAiImageModel;
@RequestMapping("/ai/img")
public Object getImg(String msg) {
ImageResponse imageResponse = openAiImageModel.call(new ImagePrompt(msg));
System.out.println("imageResponse" + imageResponse);
return imageResponse.getResult().getOutput();
}
}
02. 测试

2️⃣: 方式二(设置图片属性)
01. 代码
@RequestMapping("/ai/img2")
public Object getImg2(String msg) {
ImageResponse imageResponse = openAiImageModel.call(new ImagePrompt(msg, OpenAiImageOptions.builder()
.withQuality("hd")
.withN(1)
.withWidth(1024)
.withHeight(1024)
.build())
);
System.out.println("imageResponse" + imageResponse);
return imageResponse.getResult().getOutput().getUrl();
}
02. 测试

四、音频转文字
①:方式一
01. 代码:
@RestController
public class TranscriptionController {
@Resource
private OpenAiAudioTranscriptionModel openAiAudioTranscriptionModel;
@RequestMapping("/ai/audio")
public Object audio() {
ClassPathResource resource = new ClassPathResource("20240705.mp3");
return openAiAudioTranscriptionModel.call(resource);
}
}

02. 测试
五、文字转语言
①:方式一
01. 代码
@RestController
public class SpeechController {
@Resource
private OpenAiAudioSpeechModel openAiAudioSpeechModel;
@RequestMapping("/ai/speech")
public Object audio(String msg) {
try {
byte[] bytes = openAiAudioSpeechModel.call(msg);
String filePath = "D:\\KuGou\\KugouMusic\\audiofile.mp3";
FileUtil.writeBytesToFile(bytes, filePath);
return "转换成功";
} catch (IOException e) {
e.printStackTrace();
return "转换失败";
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class FileUtil {
public static void writeBytesToFile(byte[] bytes, String filePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(bytes);
}
}
public static void writeBytesToFileNIO(byte[] bytes, String filePath) throws IOException {
Files.write(Paths.get(filePath), bytes);
}
}
02.测试
六、多模态API
①:方式一
01. 代码
@RestController
public class MultiModelController {
@Resource
private ChatClient chatModel;
@RequestMapping("/ai/multi")
public Object multi(String msg, String imageUrl) {
var userMessage = new UserMessage(msg,
List.of(new Media(MimeTypeUtils.IMAGE_PNG, imageUrl)));
ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),
OpenAiChatOptions.builder().withModel(OpenAiApi.ChatModel.GPT_4_VISION_PREVIEW.getValue()).build()));
return response.getResult().getOutput();
}
}