模拟类似 DeepSeek 的对话

server/2025/3/15 7:17:27/

以下是一个完整的 JavaScript 数据流式获取实现方案,模拟类似 DeepSeek 的对话式逐段返回效果。包含前端实现、后端模拟和详细注释:

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>流式对话演示</title><style>#output {border: 1px solid #ccc;padding: 20px;margin: 20px;min-height: 200px;font-family: monospace;white-space: pre-wrap;}.loading {display: none;color: #666;}</style>
</head>
<body><button onclick="startStream()">开始对话</button><div id="output"></div><div id="loading" class="loading">思考中...</div><script>// 流式对话核心逻辑class StreamDialog {constructor() {this.output = document.getElementById('output');this.loading = document.getElementById('loading');this.controller = null;  // 用于中止请求}// 开始流式对话async start() {try {this.showLoading();this.output.textContent = '';// 创建中止控制器this.controller = new AbortController();// 发起 fetch 请求const response = await fetch('/stream', {signal: this.controller.signal,headers: {'Content-Type': 'application/json'}});if (!response.ok) throw new Error(response.statusText);if (!response.body) throw new Error("ReadableStream not supported");// 创建读取器const reader = response.body.getReader();const decoder = new TextDecoder();// 持续读取流数据while (true) {const { done, value } = await reader.read();if (done) break;// 解析数据块const chunk = decoder.decode(value);const lines = chunk.split('\n').filter(line => line.trim());for (const line of lines) {try {const event = JSON.parse(line.replace('data: ', ''));this.appendContent(event.content);} catch (e) {console.error('解析错误:', e);}}}} catch (error) {if (error.name !== 'AbortError') {this.appendContent('\n[对话异常终止]');}} finally {this.hideLoading();this.controller = null;}}// 中止对话abort() {if (this.controller) {this.controller.abort();}}// 添加内容到输出区appendContent(text) {// 模拟打字机效果let currentIndex = 0;const animate = () => {if (currentIndex < text.length) {this.output.textContent += text[currentIndex];currentIndex++;requestAnimationFrame(animate);this.output.scrollTop = this.output.scrollHeight; // 自动滚动}};animate();}showLoading() {this.loading.style.display = 'block';}hideLoading() {this.loading.style.display = 'none';}}// 初始化对话实例const dialog = new StreamDialog();// 启动对话function startStream() {dialog.start();}// 中止对话function stopStream() {dialog.abort();}</script>
</body>
</html>

后端 Node.js 实现(Express):

const express = require('express');
const app = express();
const port = 3000;// 允许跨域
app.use((req, res, next) => {res.header('Access-Control-Allow-Origin', '*');next();
});// 流式端点
app.get('/stream', (req, res) => {// 设置流式响应头res.writeHead(200, {'Content-Type': 'text/event-stream','Cache-Control': 'no-cache','Connection': 'keep-alive'});// 模拟的对话数据const responseText = ["你好!我是 DeepSeek 的智能助手。","我正在分析您的问题...","根据现有数据,建议如下:\n","1. 首先检查网络连接\n","2. 验证 API 密钥有效性\n","3. 查看服务状态面板\n","\n需要更详细的帮助吗?"];// 发送流式数据let index = 0;const sendChunk = () => {if (index < responseText.length) {const chunk = {content: responseText[index],finished: index === responseText.length - 1};// SSE 格式要求res.write(`data: ${JSON.stringify(chunk)}\n\n`);index++;setTimeout(sendChunk, 500); // 控制发送速度} else {res.end();}};sendChunk();// 处理客户端断开连接req.on('close', () => {console.log('客户端断开连接');res.end();});
});app.listen(port, () => {console.log(`服务运行在 http://localhost:${port}`);
});

核心实现原理:

  1. 前端实现:
  • 使用 Fetch API + ReadableStream 处理流式数据
  • 支持请求中止(AbortController)
  • 实现打字机动画效果
  • 自动滚动保持内容可见
  • 加载状态指示
  1. 后端实现:
  • 使用 Server-Sent Events (SSE) 协议
  • 分块发送 JSON 格式数据
  • 模拟真实响应延迟(500ms/块)
  • 支持客户端断开检测
  1. 数据格式:
{"content": "当前内容片段","finished": false
}

优化扩展建议:

  1. 性能优化:
// 添加节流控制
const throttle = (func, limit) => {let lastFunc;let lastRan;return function(...args) {if (!lastRan) {func.apply(this, args);lastRan = Date.now();} else {clearTimeout(lastFunc);lastFunc = setTimeout(() => {if ((Date.now() - lastRan) >= limit) {func.apply(this, args);lastRan = Date.now();}}, limit - (Date.now() - lastRan));}};
};// 在 appendContent 中使用
this.appendContent = throttle(this.appendContent, 50);
  1. 错误处理增强:
// 前端添加错误处理
async start() {try {// ...原有逻辑...} catch (error) {if (error.name === 'AbortError') {this.appendContent('\n[对话已中止]');} else {this.appendContent(`\n[错误: ${error.message}]`);console.error('Stream Error:', error);}}
}// 后端添加错误模拟
app.get('/stream', (req, res) => {// 10% 概率模拟错误if (Math.random() < 0.1) {res.writeHead(500);res.end();return;}// ...原有逻辑...
});
  1. 功能扩展:
// 添加 Markdown 支持
appendContent(text) {// 简单 Markdown 解析const parsed = text.replace(/#{3}/g, '<h3>').replace(/#{2}/g, '<h2>').replace(/#{1}/g, '<h1>').replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');// 使用 DocumentFragment 优化渲染const fragment = document.createDocumentFragment();const span = document.createElement('span');span.innerHTML = parsed;fragment.appendChild(span);this.output.appendChild(fragment);
}

这个实现方案完整展示了:

  1. 前后端流式通信的全流程
  2. 实时内容渲染优化技巧
  3. 完整的错误处理机制
  4. 可扩展的架构设计
  5. 用户体验优化细节

实际部署时,建议:

  1. 使用 WebSocket 替代 SSE 实现双向通信
  2. 添加 JWT 鉴权
  3. 实现速率限制(Rate Limiting)
  4. 部署到支持 HTTP/2 的服务器
  5. 添加前端缓存策略

http://www.ppmy.cn/server/175089.html

相关文章

数据分析入门:从数据探索到洞察真相

数据分析入门&#xff1a;从数据探索到洞察真相 在大数据时代&#xff0c;数据无处不在。从日常生活中的购物记录&#xff0c;到企业运营中的物流数据&#xff0c;数据分析已经成为每个人必须掌握的一项技能。作为一名“大数据”领域的创作者&#xff0c;今天我想以通俗的方式…

解锁STM32外设:开启嵌入式开发新世界

✨✨✨这里是小韩学长yyds的BLOG(喜欢作者的点个关注吧) ✨✨✨想要了解更多内容可以访问我的主页 小韩学长yyds-CSDN博客 目录 探索 STM32 强大的外设家族 初窥门径&#xff1a;STM32 外设开发基础 开发方式与工具 外设配置基础步骤 深入剖析&#xff1a;常见外设应用实例…

【实战解析】smallredbook.item_get_video API:小红书视频数据获取与电商应用指南

一、API功能定位 ​​​​smallredbook.item_get_video​​​​ 是小红书官方开放的笔记视频详情接口&#xff0c;核心能力包括&#xff1a; 获取视频直链&#xff08;无水印&#xff09;、封面图、时长等元数据提取笔记文本描述、标签、互动数据&#xff08;点赞/收藏/评论&…

一窥DeepSeek开源EPLB项目:揭开技术背后的面纱

摘要 在DeepSeek开源DualPipe项目的同一天&#xff0c;EPLB项目也正式对外公开。EPLB&#xff08;Enhanced Pipeline Balancing&#xff09;并非一蹴而就的奇迹&#xff0c;而是经过长时间的研发与优化。该项目旨在通过改进管道平衡机制&#xff0c;提升系统的稳定性和效率。本…

提升模型准确性的关键技术与实践指南

在当今数据驱动的时代&#xff0c;机器学习和深度学习模型已经成为解决复杂问题的核心工具。然而&#xff0c;无论模型的设计多么精巧&#xff0c;其准确性始终是衡量模型性能的关键指标之一。提升模型的准确性不仅需要对算法有深入的理解&#xff0c;还需要结合数据、特征工程…

RocketMQ 性能优化与调优策略(二)

实战演练&#xff1a;全方位调优策略 &#xff08;一&#xff09;细致入微的配置优化 生产者配置&#xff1a; sendMsgTimeout&#xff1a;此参数定义了生产者发送消息时等待 Broker 返回确认的最长时间&#xff0c;默认值为 3000 毫秒。若在该时间段内未收到确认&#xff0c…

Pandas数据清洗实战之清洗猫眼电影

本次案例所需要用到的模块 pandas(文件读取保存 操作表格的模块) 将上次Scrapy爬取下来的文件 做个数据清洗 变成我们想要的数据 确定目的&#xff1a;将此文件中的duration字段中的分钟 和publisher_time上映去掉 只保留纯数值 数据清洗题目如下: 修复 publish_time列中的错…

给AI编程泼一盆冷水

AI确实扩大了普通人的能力边界&#xff0c;但是如果你连自己想要什么都描述不清楚&#xff0c;更不知道AI干了什么&#xff0c;你最好停下来认真的学习一下。 AI并没有消除认知差距&#xff0c;而是让人与人的认知差距急剧拉大了。 一、效率提升与隐性成本的博弈 AI编程工具如…