【流式传输】使用Spring Boot实现ChatGpt流式传输
引言
在ChatGpt火了这么久,他的那种单字单字返回的格式可能让很多朋友感到好奇,在之前我用c#写了一个版本的,同时支持IAsyncEnumerable以及SSE,今天把之前写的Java版本的也发出来,和大家一起学习,有不对的地方,欢迎各位大佬指正。
Code
我这边用的是JDK21版本,可以看到下面,我们实现了两种方式一种是WebFlux实现响应式返回,另外一种就是SSE的标准写法,有关SSE,大家可以百度去看看他的一些规则,需要设置一些Header,以及返回的数据格式都有特别的讲究。第一种,我们需要在Pom.xml里面引入WebFlux的包,然后才能在代码使用,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
@RestController
@RequestMapping("Hello")
public class HelloController {
@Autowired
private RestTemplate template;
public HelloController() {
}
private String Appid="408035";
private String Appsecret="PgZgD80aWLrQUxlhVD452aJl";
@GetMapping(value = "/GetHello", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> GetHello() {
return Flux.interval(Duration.ofSeconds(1))
.map(sequence -> "Event " + sequence);
}
@PreAuthorize()
@GetMapping(value = "/GetHellos", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public void GetHellos(HttpServletResponse response) throws Exception
{
if (response.containsHeader("Content-Type"))
{
response.setHeader("Content-Type","text/event-stream");
}
else
{
response.setHeader("Content-Type","text/event-stream");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Connection", "keep-alive");
}
String data ="id:"+new Random().nextInt() +" \n" +
"retry: "+new Random().nextInt(0,100)*30+"\n" +
"event: message\n" +
"data: "+new Random().nextInt()+"\n\n";
response.setCharacterEncoding("UTF-8");
response.getWriter().write(data);
}
}
下面是我们使用WebFlux实现流式传输的一种方式。

下面是使用SSE实现流式传输的一种,同时前端代码如下。
<!DOCTYPE html>
<html>
<head>
<title>SSE Example</title>
<script>
var eventSource = new EventSource("http://localhost:5203/WeatherForecast/Posta");
eventSource.addEventListener("message", function(event) {
var a=document.getElementById("aaa");
a.innerHTML+="<a>"+event.data+"</a><br>"
console.log("Received message: " + event.data);
});
eventSource.addEventListener("error", function(event) {
console.log("Error occurred");
});
</script>
</head>
<body>
<div id='aaa'></div>
</body>
</html>

结束
以上便是今天的所有内容,使用WebFlux以及原始SSE实现流式传输的效果。
【流式传输】使用Spring Boot实现ChatGpt流式传输的更多相关文章
- Spring Boot (十四): 响应式编程以及 Spring Boot Webflux 快速入门
1. 什么是响应式编程 在计算机中,响应式编程或反应式编程(英语:Reactive programming)是一种面向数据流和变化传播的编程范式.这意味着可以在编程语言中很方便地表达静态或动态的数据流 ...
- (转)Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门
http://www.ityouknow.com/springboot/2019/02/12/spring-boot-webflux.html Spring 5.0 中发布了重量级组件 Webflux ...
- Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门
Spring 5.0 中发布了重量级组件 Webflux,拉起了响应式编程的规模使用序幕. WebFlux 使用的场景是异步非阻塞的,使用 Webflux 作为系统解决方案,在大多数场景下可以提高系统 ...
- spring boot:用redis+lua实现基于ip地址的分布式流量限制(限流/简单计数器算法)(spring boot 2.2.0)
一,限流有哪些环节? 1,为什么要限流? 目的:通过对并发请求进行限速或者一个时间单位内的的请求进行限速,目的是保护系统可正常提供服务,避免被压力太大无法响应服务. 如果达到限制速率则可以采取预定的处 ...
- 基于Spring Boot的注解驱动式公众号极速开发框架FastBootWeixin
本框架基于Spring Boot实现,使用注解完成快速开发,可以快速的完成一个微信公众号,重新定义公众号开发. 在使用本框架前建议对微信公众号开发文档有所了解,不过在不了解公众号文档的情况下使用本框架 ...
- spring boot(一):入门篇
构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring Boot高级
Spring Boot高级内容概要一.Spring Boot与缓存二.Spring Boot与消息三.Spring Boot与检索四.Spring Boot与任务五.Spring Boot与安全六.S ...
- 20191114 Spring Boot官方文档学习(4.7)
4.7.开发Web应用程序 Spring Boot非常适合于Web应用程序开发.您可以使用嵌入式Tomcat,Jetty,Undertow或Netty创建独立的HTTP服务器.大多数Web应用程序都使 ...
- 实战基于Spring Boot 2的WebFlux和mLab搭建反应式Web
Spring Framework 5带来了新的Reactive Stack非阻塞式Web框架:Spring WebFlux.作为与Spring MVC并行使用的Web框架,Spring WebFlux ...
- 【开源免费】使用Spring Boot和Html实现ChatGPT,1:亿还原,将就看
highlight: a11y-dark 简介 前段时间写了一个Chatgpt的Java版SDK开源地址:chatgpt-java欢迎使用.但由于原来OpenAI 并没有支持官网的chatgpt模型, ...
随机推荐
- Blazor前后端框架Known-V1.2.14
V1.2.14 Known是基于C#和Blazor开发的前后端分离快速开发框架,开箱即用,跨平台,一处代码,多处运行. Gitee: https://gitee.com/known/Known Git ...
- python一键过杀软
python过杀软新 利用python加载shellcode过360.火绒等杀软 先上代码 将以下代码保存到 mt.py import base64 import os import shutil b ...
- 【接口自动化测试】Eolink Apilkit 安装部署,支持 Windows、Mac、Linux 等系统
Eolink Apikit 有三种客户端,可以依据自己的情况选择.三种客户端的数据是共用的,因此可以随时切换不同的客户端. 我们推荐使用新推出的 Apikit PC 客户端,PC 端拥有线上产品所有的 ...
- 每日一库:Prometheus
什么是 Prometheus Prometheus 是一个开源的系统监控和警报工具,最初由 SoundCloud 开发,并于 2012 年发布为开源项目.它是一个非常强大和灵活的工具,用于监控应用程序 ...
- Solution Set -「NOI Online R1」
NOI-Online-T1-序列 其实这道题是全场最难的-- 我这里给出一种并查集的做法. 首先我们把操作2中的 \(u\) 和 \(v\) 合并 对于操作1我们可以把他转化为操作2来做. 比如我们针 ...
- Record - Nov. 20th, 2020 - Exam. SOL
LOC 2020.11.20 - Prob. 1 Desc. & Link. \(C=2^{k}\bmod(a+b+c)\) #include <cstdio> typedef l ...
- Oracle中数据的约束
- oracle 12C提示:ORA-28001口令已经失效
oracle 12C 提示口令已经失效,此用户是pdb用户,解决办法:1 系统管理员身份登陆 sqlplus / as sysdba 2 转到对应的pdb容器中 alter session set c ...
- Codechef - Maximize Colours(IQ)
题目大意 有红绿蓝三种颜色,三种颜色当中任意两个颜色混合都可以产生出一个新的颜色(然而混合产生的颜色不能与任何其它的颜色进行混合).输入三个整数,分别代表红色,绿色,蓝色的颜色个数(每次混合各消耗 ...
- Shell 文件或目录操作符(-e、-d、-f、-r、-w、-x)
操作符 操作符 含义-e 判断对象是否存在(Exist),若存在则结果为真-d 判断对象是否为目录(Directory),是则为真-f 判断对象是否为一般文件(File),是则为真-r 判断对象是否有 ...