当前位置: 首页 > news >正文

vue Ai 流试回答实现打字效果

注意需要下载两个依赖

//它能将markdown语法,实时转换为Html
import { marked } from "marked";
import { EventSourcePolyfill } from "event-source-polyfill";
<template><div v-html="valueContent"></div>
</template>
  let speed = 20; // 字符显示速度(毫秒/字符)let lastTimestamp = 0;let AIstatus = 0; // 0 未开始 1 进行中 2 结束 let thinkContent = ""; //问题思考let valueContent = ""; //总结结果let output = "";let eventSource = null;let typingQueue: any = [];let animationFrameId: number | null = null; // 用于保存requestAnimationFrame的IDcloseSSE() {this.stopStreaming();this.AIstatus = 0;}get_thinkContent_valueContent(html: string) {let isf = false;if (html.indexOf("</think>") !== -1) {isf = true;}if (!isf) {//将markdown语法,实时转换为Htmlthis.thinkContent = marked(html).replace(/<pre>/g, "<pre class='hljs'>");} else {//获取到总结结果this.loading = false;let x = html.indexOf("</think>") || 0;if (x !== -1) {x += 8;}const x2 = html.substr(x);//将markdown语法,实时转换为Htmlthis.valueContent = marked(x2).replace(/<pre>/g, "<pre class='hljs'>");}}typeNextInQueue() {if (this.typingQueue && this.typingQueue.length) {const htmlContent = this.typingQueue[0] || "";this.output += htmlContent;this.get_thinkContent_valueContent(this.output);this.typingQueue.shift();}}typeWriter(timestamp: any) {if (!this.lastTimestamp) {this.lastTimestamp = timestamp;}const elapsed = timestamp - this.lastTimestamp;if (elapsed >= this.speed) {this.lastTimestamp = timestamp;//结束打印if (this.typingQueue.length === 0 && this.AIstatus === 2) {this.$nextTick(() => {this.initEcharts();this.initEcharts2();this.initEcharts3();});console.log(this.output);return; // 结束动画}this.typeNextInQueue();}this.animationFrameId = requestAnimationFrame(this.typeWriter);}processChunk(event: any) {if (!event.data) return;try {const decodedData = JSON.parse(event.data);const answer = decodedData.answer;const type = decodedData.event;if (type === "message_end") {this.AIstatus = 2;this.loading = false;}if (type === "message") {this.typingQueue.push(answer);}} catch (error) {}}stopStreaming() {if (this.eventSource) {this.eventSource?.close();this.eventSource = null;}}//点击开始搜索async getAiLiveHoodAiQaStream(content: any) {this.valueContent = "";this.output = "";this.typingQueue = [];this.AIstatus = 0;this.eventSource ? this.eventSource.close() : "";this.eventSource = null;console.log(content);const url = `${baseURL}/*****?content=${encodeURIComponent(content)}`;setTimeout(() => {this.loading = true;}, 50);const source = new EventSourcePolyfill(url, {headers: {"content-type": "application/json",token: ''}});requestAnimationFrame(this.typeWriter);this.eventSource = source;// 监听数据流source.addEventListener("message", event => {this.processChunk(event);});// 监听错误source.addEventListener("error", event => {console.log(event);this.stopStreaming();});// 连接错误处理source.onerror = error => {console.log(event);this.stopStreaming();};}//关闭弹窗handleClose(){this.valueContent = "";this.output = "";this.typingQueue = [];this.AIstatus = 2;this.eventSource ? this.eventSource.close() : "";this.eventSource = null;// 暂停requestAnimationFrame动画if (this.animationFrameId) {cancelAnimationFrame(this.animationFrameId);this.animationFrameId = null;}this.visibles = false;}

接口返回的格式,根据以上代码可以实现GPT打字效果。
在这里插入图片描述

http://www.dtcms.com/a/393841.html

相关文章:

  • 【架构】面向对象六大设计原则
  • ✅ 基于OpenCV与HyperLPR的车牌识别系统 PyQt5可视化 支持多种输入 深度学习毕业设计
  • 深度学习在计算机视觉中的最新进展:范式转变与前沿探索
  • 本地免费使用网页表格控件websheet
  • Spring Boot集成MQTT与单片机通信
  • 【Axios 】web异步请求
  • FreeRTOS实战指南 — 6 临界段保护
  • 关于CFS队列pick_next_task_fair选取下一个任务的分析
  • 【算法笔记】链表相关的题目
  • Netty从0到1系列之Recycler对象池技术【3】
  • 网页开发入门:CSS与JS基础及BS/CS架构解析
  • css单位换算及适配
  • Java制作双脑同步 Hemi-Sync 音频
  • webrtc弱网-ProbeBitrateEstimator类源码分析与算法原理
  • 在OpenHarmony上适配图形显示【4】——rk3568_4.0r_mesa3d适配
  • 嵌入式(3)——RTC实时时钟
  • 内核模块组成和裁剪参考表
  • 140-understanding_the_armv8.x_and_armv9.x_extensions_guide
  • 【序列晋升】40 Spring Data R2DBC 轻量异步架构下的数据访问最佳实践
  • TGRS | 视觉语言模型 | 语言感知领域泛化实现高光谱跨场景分类, 代码开源!
  • Oracle / MySQL / MariaDB / SQL Server 常用连接与基础查询(Linux操作系统上)
  • 将 Jupyter Notebook 转换为 PDF
  • torchvision 编译安装 nano
  • 华为昇腾 910 到 950 系列 NPU 深度解析
  • 设计模式---门面模式
  • SQL Server从入门到项目实践(超值版)读书笔记 26
  • Datawhale学习笔记——深度语义匹配模型DSSM详解、实战与FAQ
  • 一文了解瑞萨MCU常用的芯片封装类型
  • LeetCode:44.二叉搜索树中第K小的元素
  • 初学者如何系统性地学习Linux?