Spring AI简单使用
https://github.com/chatanywhere/GPT_API_free
AI框架之Spring AI与Spring Cloud Alibaba AI使用讲解
超级详细Spring AI+ChatGPT(java接入OpenAI大模型)
Spring Cloud Alibaba AI 速通版!保姆级教程!
Java大模型应用开发,微服务整合DeepSeek,LangChain大型语言模型LLM实战 - 尚硅谷
文章目录
- pom.xml
- OpenAiApp
- AiController
- application.yml
pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>3.2.4</version>
</parent>
<groupId>org.example</groupId>
<artifactId>demo-openai</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>1.0.0-M6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
</dependencies>
</project>
OpenAiApp
@SpringBootApplication
public class OpenAiApp {
public static void main(String[] args) {
/*
// 设置代理
String proxy ="127.0.0.1"; //100.100.101.235 8811 示例,里面填具体的代理ip
int port = 7890; //代理的端口,
System.setProperty("proxyType","4");
System.setProperty("proxyPort"Integer.toString(port));
System.setProperty("proxyHost",proxy);
System.setProperty("proxySet","true");
*/
SpringApplication.run(OpenAiApp.class, args);
}
}
AiController
import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.model.Media;
import org.springframework.ai.openai.*;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.ai.openai.audio.speech.SpeechPrompt;
import org.springframework.ai.openai.audio.speech.SpeechResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Flux;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
https://github.com/chatanywhere/GPT_API_free
转发Host1: https://api.chatanywhere.tech (国内中转,延时更低)
转发Host2: https://api.chatanywhere.org (国外使用)
AiCore API https://api.xty.app/token
OpenAI购买平台 https://eylink.cn/
spring ai 使用官方示例代码 https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/test
*/
@SuppressWarnings("all")
@Controller
public class AiController {
@Autowired(required = false)
private ChatClient.Builder builder;
// 一次返回
@RequestMapping("test01")
@ResponseBody
public String test01(@RequestParam("prompt") String prompt) {
ChatClient chatClient = builder.defaultSystem("You are a helpful assistant.").build();
String response = chatClient.prompt(prompt).system("You are also talented").call().content();
// String response = chatClient.prompt().user(prompt).call().content();
return response;
}
// 逐词返回
@RequestMapping(value = "test02", produces = "text/html;charset=utf-8")
@ResponseBody
public Flux<String> test02(@RequestParam("prompt") String prompt) {
ChatClient chatClient = builder.build();
Flux<String> flux = chatClient.prompt(prompt).stream().content();
// flux.subscribe(System.out::println);
return flux;
}
// @see OpenAiAutoConfiguration
@Autowired
private ChatModel chatModel;
@RequestMapping(value = "test03")
@ResponseBody
public String test03(@RequestParam("prompt") String prompt) {
ChatResponse chatResponse = chatModel.call(
new Prompt(
new UserMessage(prompt),
OpenAiChatOptions.builder()
.model("deepseek-reasoner")
.temperature(0.8)
.build()
)
);
return chatResponse.getResult().getOutput().getText();
}
@RequestMapping(value = "test04", produces = "text/html;charset=utf-8")
@ResponseBody
public Flux<String> test04(@RequestParam("prompt") String prompt) {
return chatModel.stream(
new Prompt(
new UserMessage(prompt),
OpenAiChatOptions.builder()
.model("deepseek-chat")
.temperature(0.8)
.build()
))
.flatMap(chatResponse -> Flux.just(chatResponse.getResult().getOutput().getText()));
}
@Autowired
private OpenAiImageModel openaiImageModel;
// 文生图
@RequestMapping(value = "test05")
@ResponseBody
public String test05(@RequestParam("prompt") String prompt) {
ImageResponse response = openaiImageModel.call(
new ImagePrompt("draw a cat",
OpenAiImageOptions.builder()
.quality("hd")
.withModel(OpenAiImageApi.DEFAULT_IMAGE_MODEL)
.N(1)
.height(1024)
.width(1024).build())
);
return response.getResult().getOutput().getUrl();
}
@Autowired
private OpenAiAudioSpeechModel openAiAudioSpeechModel;
// 文生语音
@RequestMapping(value = "test06")
@ResponseBody
public void test06(@RequestParam("prompt") String prompt) throws IOException {
OpenAiAudioSpeechOptions speechOptions = OpenAiAudioSpeechOptions.builder()
.model(OpenAiAudioApi.TtsModel.TTS_1.value)
.voice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
.responseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
.speed(1.0f)
.build();
SpeechPrompt speechPrompt = new SpeechPrompt("大家好,欢迎来到这里,希望大家开心!", speechOptions);
SpeechResponse response = openAiAudioSpeechModel.call(speechPrompt);
StreamUtils.copy(response.getResult().getOutput(), new FileOutputStream(new File(System.getProperty("user.dir") + "\\test06.mp3")));
}
@Autowired
private OpenAiAudioTranscriptionModel openAiTranscriptionModel;
// 语音转文本
@RequestMapping(value = "test07")
@ResponseBody
public void test07(@RequestParam("prompt") String prompt) throws IOException {
OpenAiAudioTranscriptionOptions transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
.responseFormat(OpenAiAudioApi.TranscriptResponseFormat.TEXT)
.temperature(0f)
.build();
FileSystemResource audioFile = new FileSystemResource(new File(System.getProperty("user.dir") + "\\test06.mp3"));
AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(audioFile, transcriptionOptions);
AudioTranscriptionResponse response = openAiTranscriptionModel.call(transcriptionRequest);
System.out.println(response.getResult().getOutput());
}
// 多模态
@RequestMapping(value = "test08")
@ResponseBody
public void test08(@RequestParam("prompt") String prompt) throws IOException {
ClassPathResource imageResource = new ClassPathResource("multimodal.test.png");
UserMessage userMessage = new UserMessage(
"Explain what do you see in this picture?", // content
new Media(MimeTypeUtils.IMAGE_PNG, imageResource)); // media
ChatResponse response = chatModel.call(
new Prompt(
userMessage,
OpenAiChatOptions.builder()
// 指定模型
.model(OpenAiApi.ChatModel.GPT_4_TURBO_PREVIEW)
.build()
)
);
System.out.println(response.getResult().getOutput().getText());
}
}
application.yml
spring:
ai:
openai:
# base-url: https://api.chatanywhere.tech
# base-url: https://api.chatanywhere.org
base-url: https://api.xty.app # 中转站
api-key: sk-xxx
chat:
options:
# model: gpt-3.5-turbo
# model: deepseek-reasoner
# model: deepseek-chat
model: gpt-4