赞
踩
我们先创建一个根项目、之后在根项目中创建AI模块
(不然后面可能会报错 setSdk: sdk '17' type 'JavaSDK' is not registered in ProjectJdkTable)
mirror
注释掉使用原生的即可重新创建项目
或者取消maven链接在将项目添加为maven
spring:
application:
name: spring-ai-01-chat
ai:
openai:
api-key: ${open-ai-key}
base-url: ${open-ai-uri}
server:
port: 8899
@RequestMapping("/ai/chat")
public String chat(@RequestParam(value = "msg") String msg) {
return openAiChatModel.call(msg);
}
/**
* 调用chat2
*
* @param msg
* @return
*/
@RequestMapping("/ai/chat2")
public Object chat2(@RequestParam(value = "msg") String msg) {
ChatResponse response = openAiChatModel.call(new Prompt(msg, OpenAiChatOptions.builder()
.withModel("gpt-4-32k") // 模型名称 gpt的版本,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
/**
* 调用chat3(使用stream流方式)
*
* @param msg
* @return
*/
@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(); // 数据的序列,一序列的数据,一个一个的数据返回
}
@RestController
public class ImgController {
@Resource
private OpenAiImageModel openAiImageModel;
/**
* 生成图片(方式一)
* @param msg
* @return
*/
@RequestMapping("/ai/img")
public Object getImg(String msg) {
ImageResponse imageResponse = openAiImageModel.call(new ImagePrompt(msg));
System.out.println("imageResponse" + imageResponse);
return imageResponse.getResult().getOutput();
}
}
/**
* 生成图片(方式二)设置图片属性
* @param msg
* @return
*/
@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();
}
@RestController
public class TranscriptionController {
@Resource
private OpenAiAudioTranscriptionModel openAiAudioTranscriptionModel;
/**
* 语言转文本(方式一)
*
* @return
*/
@RequestMapping("/ai/audio")
public Object audio() {
ClassPathResource resource = new ClassPathResource("20240705.mp3");
return openAiAudioTranscriptionModel.call(resource);
}
}
@RestController
public class SpeechController {
@Resource
private OpenAiAudioSpeechModel openAiAudioSpeechModel;
/**
* 文本转语音(方式一)
*
* @return
*/
@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 {
/**
* 将字节数组写入指定路径的文件中
*
* @param bytes 字节数组
* @param filePath 文件路径
* @throws IOException 如果写入过程中发生错误
*/
public static void writeBytesToFile(byte[] bytes, String filePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(bytes);
}
}
/**
* 使用 Java NIO 的 Files 类将字节数组写入文件
*
* @param bytes 字节数组
* @param filePath 文件路径
* @throws IOException 如果写入过程中发生错误
*/
public static void writeBytesToFileNIO(byte[] bytes, String filePath) throws IOException {
Files.write(Paths.get(filePath), bytes);
}
}
@RestController
public class MultiModelController {
@Resource
private ChatClient chatModel;
/**
* 多模态(方式一)
*
* @return
*/
@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();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。