什么是Spring WebFlux?
Spring WebFlux是一种用于构建响应式Web应用程序的模块。它是Spring 5及更高版本引入的新特性,旨在支持响应式编程范式。
响应式编程是一种编程范式,强调通过异步数据流来构建应用程序。与传统的基于线程的同步编程不同,响应式编程侧重于通过异步事件流来处理数据。这种编程风格在处理高并发和大规模数据时特别有用,因为它可以更好地利用资源,并具有更好的伸缩性。
如果使用spring-boot-starter-webflux模块,那么默认的服务器是Netty,使用的是异步非阻塞,可以使用少量资源来获取更高的性能
性能测试:https://zhuanlan.zhihu.com/p/557216826
官方描述:https://spring.io/reactive
什么是ChatGPT?
ChatGPT是OpenAI开发的一种人工智能语言模型,基于GPT)架构。GPT代表"Generative Pre-trained Transformer"(生成式预训练转换器),这意味着它是一个通过预先训练大量数据而得到的模型,可以用于自然语言处理任务。
ChatGPT旨在通过与用户进行对话来提供有意义的回复。它可以理解和生成人类语言,以自然的方式回应用户的提问和输入。这使得ChatGPT成为一个非常强大的工具,可用于各种任务,包括回答问题、提供解释、作为语言翻译工具、自动化写作、编程辅助等。
由于GPT-3.5的先进技术,ChatGPT能够在语义和语法上更好地理解上下文,并生成更加准确、流畅的回答。然而,需要注意的是,它的回答仍然是基于其训练数据,而非真正理解问题。因此,在使用ChatGPT时,用户应该谨慎对待其提供的信息,并将其作为一个辅助工具,而非完全依赖于它的答案。
接口:https://platform.openai.com/docs/api-reference/chat
使用Spring WebFlux+WebClient 响应式请求openai 接口
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.32</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
ChatGPTController
@Slf4j
@RestController
public class ChatGPTController {
@GetMapping("/test")
public Flux<String> stringFlux(String c) {
return webClient().post()
.accept(MediaType.TEXT_EVENT_STREAM) //接收text/event-stream流的数据
.body(BodyInserters.fromValue(jsonObject(c))) //参数
.retrieve()
.bodyToFlux(String.class) //输出格式
.map(s -> {
if (!Objects.equals(s, "[DONE]")) {
JSONObject jo = JSON.parseObject(s).getJSONArray("choices").getJSONObject(0).getJSONObject("delta");
String content = jo.getString("content");
if(content != null){
return content;
}
}
return "";
})
.onErrorResume(WebClientResponseException.class, ex -> Flux.just(ex.getResponseBodyAsString())) //请求失败
.doFinally(signalType -> log.info("完成")); //请求完成后
}
//参数
private JSONObject jsonObject(String content){
JSONObject jsonObject = new JSONObject();
JSONObject userMessage = new JSONObject();
userMessage.put("role","user");
userMessage.put("content",content);
JSONArray jsonArray = new JSONArray();
jsonArray.add(userMessage);
jsonObject.put("model", "gpt-3.5-turbo-16k-0613"); //速度快,价格高
jsonObject.put("messages", jsonArray);
jsonObject.put("stream", true);
return jsonObject;
}
private WebClient webClient(){
return WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create().proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host("127.0.0.1").port(1080)) //代理
))
.defaultHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.defaultHeader("Authorization", "Bearer token") //令牌
.baseUrl("https://api.openai.com/v1/chat/completions") //请求地址
.build();
}
}