微信公众号开发网站开发,女生wordpress网站适合,沛县网站定制,wordpress 预约系统背景 接上一篇《LLM大模型统一封装接口解决方案》架构确定后#xff0c;流式方案非常规请求#xff0c;需要特殊处理。 本解决方案就是针对上一篇中所需要的流式#xff08;打字机效果进行编码#xff09; 什么是SSE SSE#xff08;Server-Sent Events#xff0c;服务器发…背景 接上一篇《LLM大模型统一封装接口解决方案》架构确定后流式方案非常规请求需要特殊处理。 本解决方案就是针对上一篇中所需要的流式打字机效果进行编码 什么是SSE SSEServer-Sent Events服务器发送事件是一种基于HTTP的服务器到客户端的单向通信技术用于实现服务器向客户端推送数据的功能。SSE协议标准由HTML5规范定义并且其定义被包含在HTML Living Standard中。 SSE允许服务器通过HTTP连接向客户端发送数据而无需客户端发起请求。这使得SSE非常适合于实时通信或推送通知给客户端的应用程序例如实时股票报价、即时通讯、实时监控等场景。 基本上SSE由以下要素组成 服务器负责向客户端发送事件流的HTTP服务器。客户端通过浏览器中的EventSource API与服务器建立连接接收服务器发送的事件。事件流Event Stream服务器向客户端发送的数据流格式为纯文本使用一种特定的格式进行编码例如MIME类型为text/event-stream。 SSE的优点包括简单易用、实现方便、跨浏览器支持良好等。然而它也有一些限制例如不能支持双向通信与WebSocket相比SSE的实时性稍逊一筹。 Java框架说明 pom 文件引入的核心依赖包 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.0/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdaip.com/groupIdartifactIdaip-com/artifactIdversion0.0.1/versionnameaip-com/namedescriptionaip com project for Spring Boot/descriptionpropertiesjava.version1.8/java.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactId/dependencydependencygroupIdio.reactivex.rxjava2/groupIdartifactIdrxjava/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project
Java后端核心代码 本方法是标准的SSE协议标准 private final ExecutorService executorService Executors.newFixedThreadPool(5);/*** 会话请求** return String*/PostMapping(value /completions, consumes MediaType.APPLICATION_JSON_VALUE)Operation(summary 会话请求)public SseEmitter completions(RequestBody CompletionRequest completionRequest) {response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);SseEmitter emitter new SseEmitter();executorService.execute(() - {try {for (int i 0; i 10; i) {// 向客户端发送事件emitter.send(SseEmitter.event().name(message).data(JsonHelper.toJSONString(new StreamCompletionResult.Builder().ended(false).message(String.valueOf(i)).build())));Thread.sleep(1000);}emitter.complete();} catch (Exception e) {emitter.completeWithError(e);}});return emitter;/*** 会话请求** return String*/GetMapping(value /stream)Operation(summary 会话请求)public SseEmitter stream() {response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);SseEmitter emitter new SseEmitter();executorService.execute(() - {try {for (int i 0; i 10; i) {// 向客户端发送事件emitter.send(SseEmitter.event().name(message).data(JsonHelper.toJSONString(new StreamCompletionResult.Builder().ended(false).message(String.valueOf(i)).build())));Thread.sleep(1000);}emitter.complete();} catch (Exception e) {emitter.completeWithError(e);}});return emitter;Flux 和 Flowable 对比 Flux 和 Flowable 都是响应式编程库中的数据流类型用于处理异步和基于事件的流式数据。它们分别来自于不同的库Flux 是 Reactor 库的一部分而 Flowable 则是 RxJava 库的一部分。以下是它们之间的一些区别 库的来源 Flux 来自于 Reactor 库是 Reactor 的核心组件之一React的核心模块用于基于反应式流规范处理数据流。Flowable 来自于 RxJava 库是 RxJava 的核心类之一RxJava 是 Java 平台的反应式扩展库用于处理异步和基于事件的编程。 背压策略 Flux 默认采用背压策略为 BUFFER可以通过 onBackpressureBuffer、onBackpressureDrop、onBackpressureLatest 等方法来指定不同的背压策略。Flowable 默认也是支持背压的但是相比 FluxFlowable 提供了更多的背压策略如 BUFFER、DROP、LATEST、ERROR、MISSING。 反应式规范 Flux 遵循 Reactor 库的反应式流规范使用 Mono 和 Flux 来表示异步流和单个结果。Flowable 遵循 RxJava 库的反应式流规范使用 Observable 和 Flowable 来表示异步流和单个结果。 生态系统 Reactor 生态系统主要用于基于 Reactor 的应用程序。RxJava 生态系统则更广泛它是 ReactiveX 的一部分支持多种语言和平台并有许多衍生项目。 总的来说Flux 和 Flowable 在概念上很相似都用于处理异步和基于事件的流式数据但它们来自于不同的库并且有一些细微的区别如背压策略和生态系统支持。您可以根据项目需求选择适合的库和数据流类型。 Java后端Flowable方式 本方法是Flowable方式非标准流式规则 /*** 会话请求** return String*/GetMapping(value /stream)Operation(summary 会话请求)public FlowableString stream() {response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);FlowableString typingFlow Flowable.create(emitter - {executorService.execute(() - {try {for (int i 0; i 10; i) {emitter.onNext(JsonHelper.toJSONString(new StreamCompletionResult.Builder().ended(false).message(String.valueOf(i)).build()));Thread.sleep(1000);}emitter.onComplete();} catch (Exception e) {}});}, BackpressureStrategy.BUFFER);return typingFlow;}Java后端Flux方式 本方法是Flux方式非标准流式规则 /*** 会话请求** return String*/GetMapping(value /stream)Operation(summary 会话请求)public FluxString stream() {response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);FluxString typingFlow Flux.create(emitter - {executorService.execute(() - {try {for (int i 0; i 10; i) {emitter.next(JsonHelper.toJSONString(new StreamCompletionResult.Builder().ended(false).message(String.valueOf(i)).build()));Thread.sleep(1000);}emitter.complete();} catch (Exception e) {}});}, FluxSink.OverflowStrategy.BUFFER);return typingFlow;}
}HTML 客户端接收示例程序 function EventSourceGetRequest() SSE 默认方法只支持GET请求适合演示用途以及后端包装好服务 function fetchPostRequest() fetch POST 请求实现SSE支持所有请求(POST,GET等)以及传递参数 sse.html 内容 !DOCTYPE html
html langen
headmeta charsetUTF-8titleSEE Example/titlescript// SSE 默认方法只支持GET请求function EventSourceGetRequest() {if(typeof(EventSource)!undefined){var eventSource new EventSource(http://127.0.0.1:8090/v1/chat/stream);eventSource.onmessage function(event){document.getElementById(result).insertAdjacentHTML(beforeend, ${event.data}br/br/);console.log(event)};}else{document.getElementById(result).innerHTML抱歉你的浏览器不支持 server-sent 事件...;}}// fetch POST 请求实现SSEfunction fetchPostRequest() {fetch(http://127.0.0.1:8090/v1/chat/completions, {method: POST,headers: {Content-Type: application/json},body: JSON.stringify({}),}).then(response {// 检查响应是否成功if (!response.ok) {throw new Error(Network response was not ok);}// 返回 ReadableStream 对象return response.body;}).then(stream {// 创建一个新的文本解码器const decoder new TextDecoder();// 获取一个 reader 对象const reader stream.getReader();let chunk // 逐块读取数据function read() {reader.read().then(({ done, value }) {if (done) {document.getElementById(result).insertAdjacentHTML(beforeend, ${chunk}hr/);console.log(Stream has ended);return;}// 将数据块转换为字符串并显示const tmp decoder.decode(value, { stream: true });if (tmp.startsWith(event:) chunk!) {document.getElementById(result).insertAdjacentHTML(beforeend, ${chunk}hr/);chunk tmp}else{chunk chunk tmp}// 继续读取下一块数据read();});}// 开始读取数据read();}).catch(error {// 处理错误console.error(There was a problem with the fetch operation:, error);});}// EventSourceGetRequest();fetchPostRequest();/script
/head
bodyh1SEE result/h1div idresult/div
/body
/html标准SSE示例 扩展SSE 文章转载自: http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.grwgw.cn.gov.cn.grwgw.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.bfkrf.cn.gov.cn.bfkrf.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.bhdyr.cn.gov.cn.bhdyr.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.ygqhd.cn.gov.cn.ygqhd.cn http://www.morning.rkmhp.cn.gov.cn.rkmhp.cn http://www.morning.tfsyk.cn.gov.cn.tfsyk.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.frzdt.cn.gov.cn.frzdt.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.dkzrs.cn.gov.cn.dkzrs.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.wknj.cn.gov.cn.wknj.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.wknbc.cn.gov.cn.wknbc.cn http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn http://www.morning.rxfgh.cn.gov.cn.rxfgh.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.mzydm.cn.gov.cn.mzydm.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.xjnw.cn.gov.cn.xjnw.cn http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.brbnc.cn.gov.cn.brbnc.cn http://www.morning.ksjnl.cn.gov.cn.ksjnl.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.ywpwg.cn.gov.cn.ywpwg.cn http://www.morning.qrmry.cn.gov.cn.qrmry.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.rgrys.cn.gov.cn.rgrys.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.qstjr.cn.gov.cn.qstjr.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.pltbd.cn.gov.cn.pltbd.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.mgskc.cn.gov.cn.mgskc.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.nfyc.cn.gov.cn.nfyc.cn http://www.morning.4q9h.cn.gov.cn.4q9h.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.kpxzq.cn.gov.cn.kpxzq.cn http://www.morning.yrnll.cn.gov.cn.yrnll.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.kjrp.cn.gov.cn.kjrp.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.htrzp.cn.gov.cn.htrzp.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.lqlfj.cn.gov.cn.lqlfj.cn http://www.morning.wspjn.cn.gov.cn.wspjn.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.wlxfj.cn.gov.cn.wlxfj.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn