vue3+fetch请求+接收到流式的markdown数据+一边gpt打字机式输出内容,一边解析markdown语法+highlight.js实现代码高亮

AI前端实战:流式处理、TypeScript类型安全与状态协同 AI前端并非简单调用大模型API,而是围绕流式响应(Streaming)与渐进式数据交付构建的新型前端范式。其核心原理在于打破传统HTTP‘请求-响应’单次闭环,转为持续的数据通道管理;技术价值体现在通过TypeScript类型守卫约束AI输出不确定性、借助状态机编排AI生命周期,从而保障UI响应性与逻辑鲁棒性;典型应用场景包括AI聊天界面、技术文档摘要助手、智能代码补全等需实时反馈的交互系统。本文聚焦AI前端三大交汇点——流式处理、TypeScript类型基建与AI感知型状态管理,提供可落地的工程实践路径 阅读详情
AI摘要

本文介绍如何在Vue3项目中实现流式数据的实时解析与渲染,特别是针对Unicode格式的Markdown文本,实现类似GPT的对话式输出效果。

这个问题终于解决了!好开心。

先看最终效果:

video_20240724_141543_edit

项目背景:vue3

场景:像gpt一样可以对话,当用户发送问题之后,ai回复,ai是一部分一部分回复,像打印机式输出。后端返回的是流式数据,且这个数据是如下格式:

data里的content存放的就是ai回复的一部分数据,但这个数据是Unicode格式的字符串,前端(我)需要做的就是实现视频中的效果:打印机式的输出+实时解析markdown文本并正确渲染到页面上。

如果你只是要实现打印机式的输出,直接看我之前的一篇文章就行:

像ChatGPT一样实现打印机式输出_vue 仿gpt打印显示-CSDN博客

如果你要实现:打印机式的输出+实时解析markdown文本并正确渲染到页

nodejs + vue3 模拟 fetchEventSouce进行sse流式请求 在GPT爆发的时候,各项目都想给自己的产品加上AI,蹭上AI的风口,因此在最近的一个需求,就想要给项目加入Ai的功能,原本要求的效果是,查询到对应的数据后,完全展示出来,也就是常规的post请求,后来这种效果遇到了一个很现实的问题:长时间的等待。我们需要在GPT返回全部数据后,前端才能接受并展示,一旦询问的时间过长,就会让用户等待很久,这时候我们需要将前端的展示效果改为想ChatGPT那样的打字机效果。 阅读详情

相关推荐

前端通信库fetch-event-source实现丰富的SSE

相比轮询,SSE显著降低延迟与资源消耗,是Web实时应用的优选方案之一。最终效果与使用原生EventSource一样,但是使用Fetch-Event-Source我们的可配置项非常多(只要Fetch API支持的都可以使用)。本篇文章我们将介绍一个开源组件fetch-event-source,该组件在发起事件源请求时,同时拥有Fetch API所提供的所有功能。/sse,该接口将通过fetch-event-source库来处理,注意这时候我们使用的是POST请求,并且还携带了请求body内容。

FeelTouch Labs 3791

vue实现post请求接口流式输出数据sse

参考git源码:https://github.com/Azure/fetch-event-source/tree/main。本地联通 发现数据并没有流式输出:vue代理需要关闭compress。使用fetchEventSource。研发阶段 需要配置nginx。

ErvinBao的博客 3076

Linux下XDMA驱动调试实战:从PCIe枚举到DMA性能调优

PCIe枚举是设备与主机通信的基础,涉及BAR空间映射和配置空间访问。XDMA作为Xilinx的PCIe DMA IP核,其驱动probe流程和DMA描述符机制直接影响数据传输效率。在FPGA加速卡和数据采集卡等场景中,掌握XDMA调试技巧能显著提升系统稳定性和吞吐量。本文围绕PCIe枚举、BAR映射、DMA描述符、MSI-X中断等关键环节,深入剖析Linux环境下XDMA驱动的常见问题与性能调优方法,帮助工程师快速定位硬件链路、IP配置及驱动层的故障。

weixin_29171129的博客 219

八股文(Unity篇)

在Unity中,协程是通过迭代器实现的,即将协程函数的返回值类型定义为IEnumerator,然后使用yield语句暂停协程的执行,并在需要恢复执行时通过MoveNext()方法继续执行。Unity中的协程是一种能够在主函数中暂停和恢复执行的功能,它使用了C#的迭代器。Resources方式:Resources方式:使用Resources.Load方法加载Resources文件夹下的资源,这种方式的好处是简单易用,无需手动打包资源,但是会导致应用包变得较大,加载速度较慢,不适合较大的资源文件。

m0_50816320的博客 1万+

华东师范大学2020机试题解

1.统计卡牌的值 #include<iostream> #include<string> #include<vector> //#include<algorithm> using namespace std; int main() { int n; cin>>n; int answer=0; for(int i=0;i<n;++i) { string str; ci

qq_42254342的博客 1911

Jetson Orin Nano@Ubuntu22.04:IMX219 CSI摄像头配置全流程与避坑指南

本文详细介绍了在Jetson Orin Nano@Ubuntu22.04系统上配置IMX219 CSI摄像头的全流程,包括硬件安装、驱动配置、常见问题排查及性能优化。特别强调了排线方向、驱动模式选择等关键细节,并提供了实用的命令和解决方案,帮助开发者高效完成摄像头部署与调试。

prometheus9mon的博客 325

从逻辑门到存储阵列:NAND与NOR闪存命名与结构原理详解

在数字电路与半导体存储器领域,逻辑门是构建一切复杂功能的基础单元,其输入输出关系定义了计算与存储的基本原理。与非门(NAND)和或非门(NOR)作为通用逻辑门,其真值表和行为模式深刻影响了芯片设计。基于这些原理,现代闪存技术发展出了两种核心架构:NAND Flash和NOR Flash。这两种闪存的命名直接源于其内部存储单元阵列的连接方式,分别模拟了与非门和或非门的逻辑关系。这种结构差异决定了它们根本的技术价值:NOR Flash采用并联结构,支持快速随机读取,适用于代码存储与原地执行(XiP);而NAND

weixin_33377602的博客 369

KT148A语音芯片SOP外挂功放芯片8002D的说明_V1

KT148A语音芯片内置8欧姆0.5W的功放,所以一般的应用场景,播放一些提示音音量是足够的。但是有的需求还需要驱动更大的扬声器,实现更大的播放音量,这里我们给出解决方案。KT148A+HAA8002D的组合

qyvhome的博客 2169

【基础演练】Playwright 极简复习:作为 AI-RPA 底座的 Playwright 核心用法

摘要: 2026年,Playwright已从测试工具升级为AI-RPA标准执行底座,周下载量超3300万,被browser-use等AI Agent项目广泛采用。其核心架构通过Playwright Server实现多浏览器协议适配,支持远程连接与高并发隔离(BrowserContext)。自动等待机制显著提升稳定性,但高频交互场景可能需直接使用CDP协议。Playwright凭借跨浏览器兼容性、轻量级隔离和微软官方支持,成为测试、RPA和AI Agent领域的事实标准,未来将主导对话式自动化市场。

ZDQ58818的博客 250

【图像超分】论文复现:新手入门!Pytorch实现SRCNN,数据预处理、模型训练、测试、评估全流程详解,注释详细,简单修改就可以训练你自己的图像数据,有训练好的模型下载地址,随取随用

【图像超分】论文精读:Image Super-Resolution Using Deep Convolutional Networks(SRCNN)请配合上述论文精读文章使用,效果更佳!图像超分辨率SRCNN和FSRCNN复现代码,除基本的网络实现外,还有特征图可视化,PSNR曲线图可视化,测试自己的图像数据等不想理解原理,希望直接跑通然后应用到自己的图像数据的同学,请直接下载上面的代码,有训练好的模型,直接用即可。具体使用方式见代码中的README!有问题来本文评论区留言!准备数据集,以及数据预处理。

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 2万+

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 后端传二进制文件流,vue前端接收并下载
下一篇: console.log、setTimeout和Promise的输出顺序
Now?!
博客等级 码龄5年 157粉丝 26原创
评论 22
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值