gemini-2.5-flash-preview-04-17· Google LMArena Human Preference 140K
帮我设计一个网页前端 并且这个网页能实现功能: 将一段用户输入的文字 用类似大模型token-by-token的方式呈现出来 类似chatgpt前端那样自然且有美感
- User
帮我设计一个网页前端 并且这个网页能实现功能: 将一段用户输入的文字 用类似大模型token-by-token的方式呈现出来 类似chatgpt前端那样自然且有美感
- Assistant
好的,没问题。我来帮你设计一个网页前端,实现将输入的文本以类似大模型生成的方式(token-by-token,这里简化为字符或词语逐渐显示)呈现在页面上,并且看起来自然有美感。
我们会使用 HTML, CSS 和 JavaScript 来完成。
核心思路:
- HTML: 创建输入区域(一个文本框或文本域)、一个触发按钮、一个显示输出的区域。
- CSS: 美化页面布局,让输入、按钮、输出区域看起来像一个简单的聊天界面或控制台输出。
- JavaScript:
- 获取用户输入的文本。
- 监听按钮点击事件。
- 点击按钮后,清空输出区域。
- 将输入的文本分解成小的单元(最简单是字符,也可以是词语)。
- 使用
setTimeout函数定时逐个将这些单元添加到输出区域,模拟逐字或逐词生成的效果。
下面是代码实现:
1. HTML (index.html)
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Token-by-Token Text Display</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="container"> <h1>模拟大模型输出</h1> <div class="input-area"> <textarea id="userInput" placeholder="请输入您想展示的文本..." rows="5"></textarea> <button id="startButton">开始生成</button> </div> <div class="output-area" id="outputArea"> <!-- 输出文本将在这里逐字显示 --> </div> </div> <script src="script.js"></script> </body> </html>说明:
- 一个
.containerdiv 包裹所有内容,用于居中和设置最大宽度。 <h1>标题。.input-area包裹<textarea>(输入框) 和<button>(按钮)。<textarea id="userInput">用于用户输入文本。rows="5"设置默认行数。<button id="startButton">触发显示过程。
.output-area包裹将要显示文本的区域。使用<div id="outputArea">。<link rel="stylesheet" href="style.css">引入 CSS 文件。<script src="script.js"></script>引入 JavaScript 文件,放在<body>底部确保 DOM 元素加载完毕。
2. CSS (style.css)
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f4f7f6; margin: 0; padding: 20px; display: flex; justify-content: center; align-items: flex-start; /* Align items to the top */ min-height: 100vh; /* Ensure body covers at least viewport height */ } .container { background-color: #ffffff; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); padding: 25px; width: 100%; max-width: 700px; /* Limit content width */ box-sizing: border-box; /* Include padding in width */ } h1 { text-align: center; color: #333; margin-bottom: 20px; } .input-area { display: flex; flex-direction: column; gap: 15px; /* Space between textarea and button */ margin-bottom: 25px; } textarea { width: 100%; padding: 12px; border: 1px solid #ccc; border-radius: 4px; font-size: 1rem; resize: vertical; /* Allow vertical resizing only */ box-sizing: border-box; transition: border-color 0.3s ease; /* Smooth transition on focus */ } textarea:focus { border-color: #007bff; /* Highlight on focus */ outline: none; box-shadow: 0 0 5px rgba(0, 123, 255, 0.2); } button { display: inline-block; padding: 10px 20px; background-color: #007bff; color: white; border: none; border-radius: 4px; font-size: 1rem; cursor: pointer; transition: background-color 0.3s ease, opacity 0.3s ease; align-self: flex-end; /* Align button to the right */ } button:hover { background-color: #0056b3; } button:disabled { background-color: #cccccc; cursor: not-allowed; opacity: 0.6; } .output-area { min-height: 150px; /* Give it a minimum height */ padding: 15px; border: 1px solid #ddd; border-radius: 4px; background-color: #e9ecef; /* Light grey background */ color: #333; font-size: 1rem; line-height: 1.6; /* Improve readability */ overflow-y: auto; /* Add scroll if content exceeds height */ white-space: pre-wrap; /* Preserve whitespace and wrap text */ word-wrap: break-word; /* Break long words */ box-sizing: border-box; }说明:
- 设置页面背景、字体和基本布局。
.container添加背景、圆角、阴影,使其看起来像一个内容卡片。.input-area使用 flexbox 将输入框和按钮垂直排列。- 美化
<textarea>和<button>的样式,添加 hover 和 focus 效果。禁用状态的按钮样式也加上。 .output-area设置最小高度、内边距、边框、背景色,使其看起来像一个输出框。overflow-y: auto在内容溢出时显示滚动条。white-space: pre-wrap和word-wrap: break-word保证文本正确换行和显示空格。
3. JavaScript (script.js)
document.addEventListener('DOMContentLoaded', () => { const userInput = document.getElementById('userInput'); const startButton = document.getElementById('startButton'); const outputArea = document.getElementById('outputArea'); // 定义一个异步函数来模拟逐字/逐token输出 async function typeText(text, element, delay = 50) { // 清空输出区域 element.textContent = ''; startButton.disabled = true; // 禁用按钮,避免重复点击 // 将文本分割成字符数组 const characters = text.split(''); for (let i = 0; i < characters.length; i++) { // 添加当前字符到输出区域 element.textContent += characters[i]; // 等待指定的延迟时间 await new Promise(resolve => setTimeout(resolve, delay)); // 可以选择性地在某些字符后增加延迟,模拟思考 // 例如:在句号、逗号、换行后增加延迟 if (characters[i] === '.' || characters[i] === ',' || characters[i] === '!' || characters[i] === '?' || characters[i] === '\n') { await new Promise(resolve => setTimeout(resolve, delay * 4)); // 增加4倍的延迟 } } startButton.disabled = false; // 输出完成后启用按钮 } // 监听按钮点击事件 startButton.addEventListener('click', () => { const textToDisplay = userInput.value.trim(); // 获取用户输入的文本并去除首尾空格 if (textToDisplay) { // 只有当输入不为空时才开始 typeText(textToDisplay, outputArea, 30); // 调用函数开始输出,设置每字符延迟为30ms } else { outputArea.textContent = '请输入一些文本再点击开始。'; // 如果输入为空,给出提示 } }); // 可选: 在输入框按回车键也触发生成 (如果textarea没有阻止默认行为的话) // userInput.addEventListener('keypress', function(event) { // // Check if the key pressed was 'Enter' and if Ctrl or Shift was not pressed (to allow multiline input) // if (event.key === 'Enter' && !event.shiftKey && !event.ctrlKey) { // event.preventDefault(); // Prevent default Enter key behavior (like new line) // startButton.click(); // Programmatically click the button // } // }); });说明:
document.addEventListener('DOMContentLoaded', ...)确保在 DOM 完全加载后再执行脚本。- 获取对输入框、按钮和输出区域的引用。
typeText函数是一个async函数,用来处理逐字输出的逻辑。- 它接受要显示的文本、要输出到的元素和每个字符之间的延迟作为参数。
element.textContent = '';清空输出区域。startButton.disabled = true;禁用按钮,防止用户在生成过程中多次点击。text.split('')将字符串分割成字符数组。for循环遍历字符数组。element.textContent += characters[i];将当前字符添加到输出区域。await new Promise(resolve => setTimeout(resolve, delay));这是实现延迟的关键。setTimeout在指定延迟后执行resolve,await会暂停函数执行直到 Promise 被 resolve。- 添加了一个可选的逻辑,在遇到标点符号或换行时增加额外的延迟,这能更好地模拟思考或停顿的感觉。
- 循环结束后,
startButton.disabled = false;重新启用按钮。
- 为
startButton添加点击事件监听器。 - 在点击事件中,获取输入框的值,使用
trim()去除首尾空白。 - 检查输入是否为空,不为空则调用
typeText函数开始生成。 - 如果输入为空,在输出区域给出提示。
- 注释掉的部分是如何实现按回车键触发,如果你需要这个功能可以取消注释(请注意,对于
textarea按回车默认是换行,需要阻止默认行为)。
如何使用:
- 创建一个文件夹,比如
text-display。 - 在该文件夹中创建三个文件:
index.html,style.css,script.js。 - 将上面的 HTML 代码复制到
index.html文件中。 - 将上面的 CSS 代码复制到
style.css文件中。 - 将上面的 JavaScript 代码复制到
script.js文件中。 - 用浏览器打开
index.html文件即可。
现在,你可以在文本框中输入任何文本,然后点击 "开始生成" 按钮,文本就会在下方的区域中逐字显示出来,带有一定的延迟,模拟出大模型输出的效果。你可以调整
typeText函数中的delay参数来改变输出速度。进阶思路 (如果想做得更像):
- 词语/标点分割: 不按字符分割,而是按词语或标点符号分割。例如,使用正则表达式将文本分割成词语和标点组成的数组,然后逐个输出这些“词语/标点 token”。这样速度会更快,更接近实际 LLM 的输出单位(LLM 的 token 不完全等同于一个词,可能是词的一部分,或几个字符,或一个标点)。
- 光标效果: 在输出文本的末尾添加一个模拟的光标(一个闪烁的
|或_),在文本生成过程中一直显示,生成完毕后消失。这可以通过在 JS 中动态添加/移除一个带有 CSS 动画的元素来实现。 - 更复杂的延迟: 根据词语长度、标点符号等设置更复杂的随机或变化的延迟,让输出看起来更自然。
- 中止生成: 添加一个“中止”按钮,可以在生成过程中停止输出。
这个基础版本已经能够实现核心的逐字显示效果,并且通过简单的 CSS 美化后看起来比较舒服。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.