stream_test.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>流式接口Markdown渲染测试</title>
  7. <!-- Vditor CSS -->
  8. <link rel="stylesheet" href="https://unpkg.com/vditor/dist/index.css" />
  9. <style>
  10. body {
  11. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  12. margin: 0;
  13. padding: 20px;
  14. background-color: #f5f5f5;
  15. }
  16. .container {
  17. max-width: 1200px;
  18. margin: 0 auto;
  19. background: white;
  20. border-radius: 8px;
  21. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  22. overflow: hidden;
  23. }
  24. .header {
  25. background: #2c3e50;
  26. color: white;
  27. padding: 20px;
  28. text-align: center;
  29. }
  30. .content {
  31. padding: 20px;
  32. }
  33. .input-section {
  34. margin-bottom: 20px;
  35. padding: 20px;
  36. background: #f8f9fa;
  37. border-radius: 6px;
  38. }
  39. .input-group {
  40. margin-bottom: 15px;
  41. }
  42. label {
  43. display: block;
  44. margin-bottom: 5px;
  45. font-weight: 600;
  46. color: #2c3e50;
  47. }
  48. input[type="text"], textarea {
  49. width: 100%;
  50. padding: 10px;
  51. border: 1px solid #ddd;
  52. border-radius: 4px;
  53. font-size: 14px;
  54. box-sizing: border-box;
  55. }
  56. textarea {
  57. height: 80px;
  58. resize: vertical;
  59. }
  60. .button-group {
  61. display: flex;
  62. gap: 10px;
  63. margin-top: 15px;
  64. }
  65. button {
  66. padding: 10px 20px;
  67. border: none;
  68. border-radius: 4px;
  69. cursor: pointer;
  70. font-size: 14px;
  71. font-weight: 600;
  72. transition: background-color 0.3s;
  73. }
  74. .btn-primary {
  75. background: #3498db;
  76. color: white;
  77. }
  78. .btn-primary:hover {
  79. background: #2980b9;
  80. }
  81. .btn-secondary {
  82. background: #95a5a6;
  83. color: white;
  84. }
  85. .btn-secondary:hover {
  86. background: #7f8c8d;
  87. }
  88. .btn-danger {
  89. background: #e74c3c;
  90. color: white;
  91. }
  92. .btn-danger:hover {
  93. background: #c0392b;
  94. }
  95. .status {
  96. padding: 10px;
  97. margin: 10px 0;
  98. border-radius: 4px;
  99. font-weight: 600;
  100. }
  101. .status.info {
  102. background: #d1ecf1;
  103. color: #0c5460;
  104. border: 1px solid #bee5eb;
  105. }
  106. .status.success {
  107. background: #d4edda;
  108. color: #155724;
  109. border: 1px solid #c3e6cb;
  110. }
  111. .status.error {
  112. background: #f8d7da;
  113. color: #721c24;
  114. border: 1px solid #f5c6cb;
  115. }
  116. .output-section {
  117. margin-top: 20px;
  118. }
  119. .output-tabs {
  120. display: flex;
  121. border-bottom: 1px solid #ddd;
  122. margin-bottom: 20px;
  123. }
  124. .tab {
  125. padding: 10px 20px;
  126. cursor: pointer;
  127. border-bottom: 2px solid transparent;
  128. transition: all 0.3s;
  129. }
  130. .tab.active {
  131. border-bottom-color: #3498db;
  132. color: #3498db;
  133. font-weight: 600;
  134. }
  135. .tab-content {
  136. display: none;
  137. }
  138. .tab-content.active {
  139. display: block;
  140. }
  141. .raw-output {
  142. background: #f8f9fa;
  143. border: 1px solid #e9ecef;
  144. border-radius: 4px;
  145. padding: 15px;
  146. font-family: 'Courier New', monospace;
  147. font-size: 12px;
  148. white-space: pre-wrap;
  149. max-height: 400px;
  150. overflow-y: auto;
  151. }
  152. .markdown-output {
  153. border: 1px solid #e9ecef;
  154. border-radius: 4px;
  155. min-height: 200px;
  156. }
  157. .loading {
  158. display: none;
  159. text-align: center;
  160. padding: 20px;
  161. color: #666;
  162. }
  163. .loading.show {
  164. display: block;
  165. }
  166. .spinner {
  167. border: 3px solid #f3f3f3;
  168. border-top: 3px solid #3498db;
  169. border-radius: 50%;
  170. width: 30px;
  171. height: 30px;
  172. animation: spin 1s linear infinite;
  173. margin: 0 auto 10px;
  174. }
  175. @keyframes spin {
  176. 0% { transform: rotate(0deg); }
  177. 100% { transform: rotate(360deg); }
  178. }
  179. </style>
  180. </head>
  181. <body>
  182. <div class="container">
  183. <div class="header">
  184. <h1>流式接口Markdown渲染测试</h1>
  185. <p>测试 /apiv1/stream/chat 接口的流式响应和Markdown渲染效果</p>
  186. </div>
  187. <div class="content">
  188. <div class="input-section">
  189. <div class="input-group">
  190. <label for="apiUrl">API地址:</label>
  191. <input type="text" id="apiUrl" value="http://localhost:22000/apiv1/stream/chat" placeholder="输入流式接口地址">
  192. </div>
  193. <div class="input-group">
  194. <label for="message">测试消息:</label>
  195. <textarea id="message" placeholder="输入要测试的消息内容">你好,请介绍一下施工现场的安全防护要求</textarea>
  196. </div>
  197. <div class="input-group">
  198. <label for="model">模型名称 (可选):</label>
  199. <input type="text" id="model" placeholder="模型名称,留空使用默认">
  200. </div>
  201. <div class="button-group">
  202. <button class="btn-primary" onclick="startStreamTest()">开始流式测试</button>
  203. <button class="btn-secondary" onclick="clearOutput()">清空输出</button>
  204. <button class="btn-danger" onclick="stopStream()">停止流式</button>
  205. </div>
  206. </div>
  207. <div id="status" class="status info" style="display: none;"></div>
  208. <div class="loading" id="loading">
  209. <div class="spinner"></div>
  210. <div>正在接收流式数据...</div>
  211. </div>
  212. <div class="output-section">
  213. <div class="output-tabs">
  214. <div class="tab active" onclick="switchTab('raw')">原始输出</div>
  215. <div class="tab" onclick="switchTab('markdown')">Markdown渲染</div>
  216. </div>
  217. <div id="rawTab" class="tab-content active">
  218. <div class="raw-output" id="rawOutput"></div>
  219. </div>
  220. <div id="markdownTab" class="tab-content">
  221. <div class="markdown-output" id="markdownOutput"></div>
  222. </div>
  223. </div>
  224. </div>
  225. </div>
  226. <!-- Vditor JavaScript -->
  227. <script src="https://unpkg.com/vditor/dist/index.min.js"></script>
  228. <script>
  229. let vditor = null;
  230. let eventSource = null;
  231. let isStreaming = false;
  232. // 初始化Vditor
  233. function initVditor() {
  234. if (vditor) {
  235. vditor.destroy();
  236. }
  237. vditor = new Vditor('markdownOutput', {
  238. height: 400,
  239. mode: 'wysiwyg',
  240. toolbar: ['emoji', 'headings', 'bold', 'italic', 'strike', 'link', '|', 'list', 'ordered-list', 'check', 'outdent', 'indent', '|', 'quote', 'line', 'code', 'inline-code', 'insert-before', 'insert-after', '|', 'table', '|', 'undo', 'redo', '|', 'fullscreen', 'edit-mode', '|', 'content-theme', 'code-theme', '|', 'preview', 'export'],
  241. after: () => {
  242. console.log('Vditor初始化完成');
  243. }
  244. });
  245. }
  246. // 切换标签页
  247. function switchTab(tabName) {
  248. // 隐藏所有标签内容
  249. document.querySelectorAll('.tab-content').forEach(content => {
  250. content.classList.remove('active');
  251. });
  252. // 移除所有标签的active类
  253. document.querySelectorAll('.tab').forEach(tab => {
  254. tab.classList.remove('active');
  255. });
  256. // 显示选中的标签内容
  257. document.getElementById(tabName + 'Tab').classList.add('active');
  258. // 添加active类到选中的标签
  259. event.target.classList.add('active');
  260. }
  261. // 显示状态信息
  262. function showStatus(message, type = 'info') {
  263. const statusEl = document.getElementById('status');
  264. statusEl.textContent = message;
  265. statusEl.className = `status ${type}`;
  266. statusEl.style.display = 'block';
  267. setTimeout(() => {
  268. statusEl.style.display = 'none';
  269. }, 5000);
  270. }
  271. // 清空输出
  272. function clearOutput() {
  273. document.getElementById('rawOutput').textContent = '';
  274. if (vditor) {
  275. vditor.setValue('');
  276. }
  277. showStatus('输出已清空', 'success');
  278. }
  279. // 停止流式
  280. function stopStream() {
  281. if (eventSource) {
  282. eventSource.close();
  283. eventSource = null;
  284. }
  285. isStreaming = false;
  286. document.getElementById('loading').classList.remove('show');
  287. showStatus('流式连接已停止', 'info');
  288. }
  289. // 开始流式测试
  290. function startStreamTest() {
  291. const apiUrl = document.getElementById('apiUrl').value.trim();
  292. const message = document.getElementById('message').value.trim();
  293. const model = document.getElementById('model').value.trim();
  294. if (!apiUrl || !message) {
  295. showStatus('请填写API地址和测试消息', 'error');
  296. return;
  297. }
  298. // 停止之前的连接
  299. stopStream();
  300. // 清空输出
  301. clearOutput();
  302. // 显示加载状态
  303. document.getElementById('loading').classList.add('show');
  304. isStreaming = true;
  305. showStatus('正在连接流式接口...', 'info');
  306. // 构建请求数据
  307. const requestData = {
  308. message: message
  309. };
  310. if (model) {
  311. requestData.model = model;
  312. }
  313. console.log('发送请求:', requestData);
  314. // 使用fetch发送POST请求
  315. fetch(apiUrl, {
  316. method: 'POST',
  317. headers: {
  318. 'Content-Type': 'application/json',
  319. },
  320. body: JSON.stringify(requestData)
  321. })
  322. .then(response => {
  323. if (!response.ok) {
  324. throw new Error(`HTTP错误: ${response.status} ${response.statusText}`);
  325. }
  326. if (!response.body) {
  327. throw new Error('响应体为空');
  328. }
  329. showStatus('连接成功,开始接收流式数据...', 'success');
  330. // 处理流式响应
  331. const reader = response.body.getReader();
  332. const decoder = new TextDecoder();
  333. let buffer = '';
  334. let rawContent = '';
  335. let markdownContent = '';
  336. function readStream() {
  337. reader.read().then(({ done, value }) => {
  338. if (done) {
  339. console.log('流式数据接收完成');
  340. document.getElementById('loading').classList.remove('show');
  341. isStreaming = false;
  342. showStatus('流式数据接收完成', 'success');
  343. return;
  344. }
  345. // 解码数据
  346. const chunk = decoder.decode(value, { stream: true });
  347. buffer += chunk;
  348. // 处理完整的数据行
  349. const lines = buffer.split('\n');
  350. buffer = lines.pop() || ''; // 保留最后一个不完整的行
  351. for (const line of lines) {
  352. if (line.trim() === '') continue;
  353. console.log('收到数据行:', line);
  354. if (line.startsWith('data: ')) {
  355. const data = line.substring(6);
  356. if (data === '[DONE]') {
  357. console.log('流式结束');
  358. document.getElementById('loading').classList.remove('show');
  359. isStreaming = false;
  360. showStatus('流式数据接收完成', 'success');
  361. return;
  362. }
  363. // 尝试解析JSON
  364. try {
  365. const jsonData = JSON.parse(data);
  366. if (jsonData.error) {
  367. showStatus(`错误: ${jsonData.error}`, 'error');
  368. document.getElementById('loading').classList.remove('show');
  369. isStreaming = false;
  370. return;
  371. }
  372. } catch (e) {
  373. // 不是JSON,直接作为文本内容处理
  374. console.log('收到文本内容:', data);
  375. // 添加到原始输出
  376. rawContent += data;
  377. document.getElementById('rawOutput').textContent = rawContent;
  378. // 添加到markdown内容
  379. markdownContent += data;
  380. // 实时更新Vditor
  381. if (vditor) {
  382. vditor.setValue(markdownContent);
  383. }
  384. }
  385. }
  386. }
  387. // 继续读取
  388. readStream();
  389. }).catch(error => {
  390. console.error('读取流式数据时出错:', error);
  391. showStatus(`读取数据出错: ${error.message}`, 'error');
  392. document.getElementById('loading').classList.remove('show');
  393. isStreaming = false;
  394. });
  395. }
  396. readStream();
  397. })
  398. .catch(error => {
  399. console.error('请求失败:', error);
  400. showStatus(`请求失败: ${error.message}`, 'error');
  401. document.getElementById('loading').classList.remove('show');
  402. isStreaming = false;
  403. });
  404. }
  405. // 页面加载完成后初始化
  406. document.addEventListener('DOMContentLoaded', function() {
  407. initVditor();
  408. showStatus('页面加载完成,可以开始测试', 'success');
  409. });
  410. // 页面卸载时清理
  411. window.addEventListener('beforeunload', function() {
  412. stopStream();
  413. });
  414. </script>
  415. </body>
  416. </html>