596 lines
28 KiB
HTML
596 lines
28 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>模型对比结果 - 远光软件微调平台</title>
|
||
<script src="../lib/tailwindcss/tailwind.js"></script>
|
||
<link href="../lib/font-awesome/css/font-awesome.min.css" rel="stylesheet">
|
||
<style>
|
||
.sidebar-section-title {
|
||
padding: 0.5rem 1rem;
|
||
font-size: 0.75rem;
|
||
color: rgba(191, 203, 217, 0.7);
|
||
font-weight: 500;
|
||
text-transform: uppercase;
|
||
letter-spacing: 0.05em;
|
||
}
|
||
.nav-link:hover {
|
||
background-color: rgba(0, 21, 41, 0.2);
|
||
}
|
||
.sidebar-item-active {
|
||
background-color: rgba(24, 144, 255, 0.1);
|
||
color: #1890ff;
|
||
border-left: 4px solid #1890ff;
|
||
}
|
||
.bg-primary { background-color: #1890ff; }
|
||
.text-primary { color: #1890ff; }
|
||
:root { --primary: #1890ff; }
|
||
|
||
/* 卡片内容区域滚动条样式 */
|
||
.markdown-content {
|
||
overflow-y: auto;
|
||
max-height: calc(100vh - 280px);
|
||
scrollbar-width: thin;
|
||
scrollbar-color: #d1d5db transparent;
|
||
}
|
||
.markdown-content::-webkit-scrollbar {
|
||
width: 6px;
|
||
}
|
||
.markdown-content::-webkit-scrollbar-track {
|
||
background: transparent;
|
||
}
|
||
.markdown-content::-webkit-scrollbar-thumb {
|
||
background-color: #d1d5db;
|
||
border-radius: 3px;
|
||
}
|
||
|
||
/* Markdown 基础样式 */
|
||
.markdown-content h1 { font-size: 1.5em; font-weight: bold; margin: 0.5em 0; border-bottom: 1px solid #e5e7eb; padding-bottom: 0.3em; }
|
||
.markdown-content h2 { font-size: 1.25em; font-weight: bold; margin: 0.5em 0; border-bottom: 1px solid #e5e7eb; padding-bottom: 0.2em; }
|
||
.markdown-content h3 { font-size: 1.1em; font-weight: bold; margin: 0.4em 0; }
|
||
.markdown-content p { margin: 0.5em 0; line-height: 1.6; }
|
||
.markdown-content ul, .markdown-content ol { margin: 0.5em 0; padding-left: 2em; }
|
||
.markdown-content li { margin: 0.25em 0; }
|
||
.markdown-content code { background-color: #f3f4f6; padding: 0.2em 0.4em; border-radius: 4px; font-family: monospace; font-size: 0.9em; }
|
||
.markdown-content pre { background-color: #1f2937; color: #f9fafb; padding: 1em; border-radius: 8px; overflow-x: auto; margin: 0.5em 0; }
|
||
.markdown-content pre code { background: none; padding: 0; color: inherit; }
|
||
.markdown-content blockquote { border-left: 4px solid #1890ff; padding-left: 1em; margin: 0.5em 0; color: #6b7280; }
|
||
.markdown-content table { border-collapse: collapse; width: 100%; margin: 0.5em 0; }
|
||
.markdown-content th, .markdown-content td { border: 1px solid #d1d5db; padding: 0.5em 1em; text-align: left; }
|
||
.markdown-content th { background-color: #f9fafb; font-weight: bold; }
|
||
.markdown-content tr:nth-child(even) { background-color: #f9fafb; }
|
||
.markdown-content a { color: #1890ff; text-decoration: none; }
|
||
.markdown-content a:hover { text-decoration: underline; }
|
||
.markdown-content hr { border: none; border-top: 1px solid #e5e7eb; margin: 1em 0; }
|
||
.markdown-content strong { font-weight: bold; }
|
||
.markdown-content em { font-style: italic; }
|
||
.markdown-content del { text-decoration: line-through; color: #9ca3af; }
|
||
|
||
/* 流式输出光标 */
|
||
.typing-cursor::after {
|
||
content: '|';
|
||
animation: blink 1s infinite;
|
||
color: var(--primary);
|
||
}
|
||
@keyframes blink {
|
||
0%, 50% { opacity: 1; }
|
||
51%, 100% { opacity: 0; }
|
||
}
|
||
</style>
|
||
</head>
|
||
<body class="antialiased bg-gray-50 flex h-screen overflow-hidden">
|
||
<!-- 侧边导航 -->
|
||
<aside class="w-64 text-[#bfcbd9] flex-shrink-0 hidden md:block flex flex-col h-full" style="background-color: #001529;">
|
||
<!-- 平台LOGO区域 -->
|
||
<div class="pt-5 pb-3 border-b border-[#001529]/30 flex items-center justify-center pl-2">
|
||
<img src="../assets/logo/logo.png" alt="Logo" class="w-8 h-8 object-contain mr-2">
|
||
<span class="text-white font-medium text-base">远光软件微调平台</span>
|
||
</div>
|
||
|
||
<!-- 导航主区域 -->
|
||
<nav class="flex-1 overflow-y-auto py-2 relative">
|
||
<!-- 第一分区:模型服务 -->
|
||
<div class="sidebar-section-title">模型服务</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="fine-tune" class="nav-link flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-cogs w-5 text-center"></i>
|
||
<span class="ml-2">模型调优</span>
|
||
</a>
|
||
</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="my-models" class="nav-link flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-database w-5 text-center"></i>
|
||
<span class="ml-2">我的模型</span>
|
||
</a>
|
||
</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="model-eval" class="nav-link flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-line-chart w-5 text-center"></i>
|
||
<span class="ml-2">模型评测</span>
|
||
</a>
|
||
</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="model-compare" class="nav-link sidebar-item-active flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-server w-5 text-center"></i>
|
||
<span class="ml-2">模型对比</span>
|
||
</a>
|
||
</div>
|
||
|
||
<!-- 第二分区:资源管理 -->
|
||
<div class="sidebar-section-title mt-6">资源管理</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="model-manage" class="nav-link flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-cube w-5 text-center"></i>
|
||
<span class="ml-2">模型管理</span>
|
||
</a>
|
||
</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="dataset-manage" class="nav-link flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-file-text w-5 text-center"></i>
|
||
<span class="ml-2">数据集管理</span>
|
||
</a>
|
||
</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="data-generate" class="nav-link flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-database w-5 text-center"></i>
|
||
<span class="ml-2">其他工具</span>
|
||
</a>
|
||
</div>
|
||
|
||
<!-- 第三分区:系统设置 -->
|
||
<div class="sidebar-section-title mt-6">系统设置</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="config" class="nav-link flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-bar-chart w-5 text-center"></i>
|
||
<span class="ml-2">平台性能</span>
|
||
</a>
|
||
</div>
|
||
<div class="nav-item-wrapper">
|
||
<a href="#" data-page="logs" class="nav-link flex items-center px-4 py-2.5 hover:bg-[#001529]/20 transition-colors">
|
||
<i class="fa fa-file-text w-5 text-center"></i>
|
||
<span class="ml-2">查看日志</span>
|
||
</a>
|
||
</div>
|
||
</nav>
|
||
|
||
<!-- 底部信息区域 -->
|
||
<div class="p-4 border-t border-[#001529]/30 text-xs mt-auto">
|
||
<div class="mb-2 text-[#bfcbd9]/80">默认业务空间</div>
|
||
<div class="flex items-center justify-between">
|
||
<span class="text-[#bfcbd9]">版本 v1.0.0</span>
|
||
<i class="fa fa-question-circle-o text-[#bfcbd9]/70"></i>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
|
||
<!-- 主内容区 -->
|
||
<div class="flex-1 flex flex-col overflow-hidden">
|
||
<!-- 顶部导航 -->
|
||
<header class="bg-white border-b border-gray-200 shadow-sm">
|
||
<div class="flex items-center justify-between px-6 h-14">
|
||
<div class="flex items-center space-x-6">
|
||
<button class="md:hidden text-gray-500 hover:text-gray-700">
|
||
<i class="fa fa-bars"></i>
|
||
</button>
|
||
</div>
|
||
<div class="flex items-center space-x-4">
|
||
<!-- 系统性能监控 -->
|
||
<a href="?page=config" class="flex items-center space-x-4 text-xs text-gray-500 hover:text-primary transition-colors">
|
||
<div class="flex items-center" title="CPU使用率">
|
||
<i class="fa fa-microchip mr-1 text-blue-500"></i>
|
||
<span id="cpuUsage">--</span>%
|
||
</div>
|
||
<div class="flex items-center" title="内存使用率">
|
||
<i class="fa fa-database mr-1 text-green-500"></i>
|
||
<span id="memUsage">--</span>%
|
||
</div>
|
||
<div class="flex items-center" title="磁盘使用率">
|
||
<i class="fa fa-hdd-o mr-1 text-orange-500"></i>
|
||
<span id="diskUsage">--</span>%
|
||
</div>
|
||
</a>
|
||
<div class="h-6 w-px bg-gray-200"></div>
|
||
<div class="relative group">
|
||
<img src="https://picsum.photos/id/1005/32/32" class="w-8 h-8 rounded-full cursor-pointer" alt="用户头像">
|
||
<div class="absolute right-0 top-full pt-2 hidden group-hover:block z-50">
|
||
<div class="bg-white rounded shadow-lg py-1 border border-gray-100 min-w-[140px]">
|
||
<a href="login.html" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-50 whitespace-nowrap">
|
||
<i class="fa fa-sign-out mr-1"></i>退出登录
|
||
</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
<!-- 页面内容 -->
|
||
<main class="flex-1 overflow-hidden flex flex-col">
|
||
<!-- 顶部操作栏 -->
|
||
<div class="bg-white border-b border-gray-200 px-6 py-2 flex items-center justify-end flex-shrink-0">
|
||
<button id="restartBtn" class="px-3 py-1 bg-primary text-white rounded text-sm hover:bg-primary/90 transition-colors">
|
||
<i class="fa fa-refresh mr-1"></i>重新提问
|
||
</button>
|
||
</div>
|
||
|
||
<!-- 用户提问显示区 -->
|
||
<div class="px-6 py-4 flex-shrink-0">
|
||
<div class="flex items-start">
|
||
<div class="flex-shrink-0 bg-green-500 rounded-full px-3 py-1 mr-3">
|
||
<span class="text-white text-xs font-medium">用户问题</span>
|
||
</div>
|
||
<div class="bg-green-100 rounded-2xl rounded-tl-sm px-5 py-3 max-w-3xl shadow-sm">
|
||
<span class="text-green-800 text-base leading-relaxed" id="questionText"></span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 模型输出网格(2x2) -->
|
||
<div id="outputGrid" class="grid grid-cols-2 gap-4 flex-1 min-h-0">
|
||
<!-- 输出卡片将通过JS动态生成 -->
|
||
</div>
|
||
|
||
<script>
|
||
console.log('>>> model-compare-result.html 脚本开始加载');
|
||
// API 基础地址 - 使用 config.yaml 中的 app.port (7861)
|
||
const getApiBase = () => {
|
||
const protocol = window.location.protocol;
|
||
const hostname = window.location.hostname;
|
||
return `${protocol}//${hostname}:7861/api`;
|
||
};
|
||
const API_BASE = getApiBase();
|
||
|
||
// 模拟模型数据(用于演示)
|
||
const mockModels = [
|
||
{ id: 1, name: 'GPT-4', type: 'LLM', description: 'OpenAI GPT-4 大语言模型' },
|
||
{ id: 2, name: 'Claude-3', type: 'LLM', description: 'Anthropic Claude-3 模型' },
|
||
{ id: 3, name: '文心一言', type: 'LLM', description: '百度文心一言大模型' },
|
||
{ id: 4, name: '通义千问', type: 'LLM', description: '阿里通义千问大模型' },
|
||
{ id: 5, name: 'ChatGLM', type: 'LLM', description: '智谱ChatGLM对话模型' },
|
||
{ id: 6, name: '星火认知', type: 'LLM', description: '讯飞星火认知大模型' }
|
||
];
|
||
|
||
let allModels = [];
|
||
let selectedModelIds = [];
|
||
let currentStreamingIntervals = [];
|
||
let compareTaskId = null;
|
||
let taskName = '';
|
||
let userQuestion = '';
|
||
let chatResults = [];
|
||
let useLocalStorageResults = false; // 是否使用localStorage中的结果
|
||
|
||
// 模拟回复内容
|
||
const mockResponses = [
|
||
"这是一个基于深度学习的自然语言处理模型回答。我可以处理各种复杂的问题,包括代码编写、文本分析、知识问答等多种任务。我的训练数据涵盖了广泛的领域知识,能够提供准确和有用的信息。",
|
||
"您好!我是一个人工智能语言模型,很高兴为您服务。我可以帮助您解答问题、提供建议、进行创意写作等。如果您有任何需要,请随时告诉我,我会尽力提供帮助。",
|
||
"这是一个很有趣的问题!从技术角度来看,我们需要考虑多个因素:数据质量、模型架构、训练策略等。深度学习在近年来取得了巨大进展,但仍然存在一些挑战需要解决。",
|
||
"根据我的理解,这个问题涉及到以下几个方面:首先,需要明确问题的具体背景;其次,要分析相关的技术方案;最后,需要评估实施的成本和收益。建议您先收集更多信息再做决定。"
|
||
];
|
||
|
||
// 页面初始化
|
||
async function initPage() {
|
||
try {
|
||
const urlParams = new URLSearchParams(window.location.search);
|
||
compareTaskId = urlParams.get('taskId');
|
||
taskName = urlParams.get('taskName') || '对比任务';
|
||
userQuestion = decodeURIComponent(urlParams.get('question') || '');
|
||
const needRealData = urlParams.get('real') === '1';
|
||
|
||
// 设置用户提问
|
||
const questionTextEl = document.getElementById('questionText');
|
||
if (questionTextEl) {
|
||
questionTextEl.textContent = userQuestion;
|
||
}
|
||
|
||
// 加载模型列表和任务数据
|
||
await Promise.all([loadModels(), loadCompareTask()]);
|
||
|
||
// 初始化输出卡片(显示加载中状态)
|
||
initializeOutputCards(true);
|
||
|
||
// 读取 localStorage 结果
|
||
let shouldUseStoredResults = false;
|
||
const storedResults = localStorage.getItem('chatResults');
|
||
|
||
if (storedResults && !needRealData) {
|
||
try {
|
||
const parsed = JSON.parse(storedResults);
|
||
const isRecent = (Date.now() - parsed.timestamp) < 5 * 60 * 1000;
|
||
if (parsed.results && isRecent) {
|
||
chatResults = parsed.results;
|
||
shouldUseStoredResults = true;
|
||
}
|
||
} catch (e) {
|
||
console.error('解析localStorage失败:', e);
|
||
}
|
||
}
|
||
|
||
// 如果需要真实数据或没有缓存结果,则调用API
|
||
if (needRealData || !shouldUseStoredResults) {
|
||
if (selectedModelIds.length > 0 && userQuestion) {
|
||
await fetchChatResults();
|
||
// 存储到 localStorage
|
||
localStorage.setItem('chatResults', JSON.stringify({
|
||
results: chatResults,
|
||
timestamp: Date.now()
|
||
}));
|
||
}
|
||
}
|
||
|
||
// 重新初始化卡片(移除加载状态)
|
||
initializeOutputCards(false);
|
||
|
||
// 开始流式输出
|
||
simulateStreaming();
|
||
} catch (err) {
|
||
console.error('初始化失败:', err);
|
||
const contentGrid = document.getElementById('outputGrid');
|
||
if (contentGrid) {
|
||
contentGrid.innerHTML = `<div class="text-red-500 p-8">初始化失败: ${err.message}</div>`;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 调用批量对话 API 获取结果
|
||
async function fetchChatResults() {
|
||
try {
|
||
const response = await fetch(`${API_BASE}/model-chat/batch`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({
|
||
model_ids: selectedModelIds,
|
||
system_prompt: '',
|
||
user_question: userQuestion,
|
||
temperature: 0.7,
|
||
max_tokens: 2048
|
||
})
|
||
});
|
||
|
||
const result = await response.json();
|
||
if (result.code === 0 && result.data) {
|
||
chatResults = result.data;
|
||
}
|
||
} catch (error) {
|
||
console.error('调用API失败:', error);
|
||
}
|
||
}
|
||
|
||
// 加载对比任务数据
|
||
async function loadCompareTask() {
|
||
if (!compareTaskId) return;
|
||
|
||
try {
|
||
const response = await fetch(`${API_BASE}/model-compare/${compareTaskId}`);
|
||
const result = await response.json();
|
||
|
||
if (result.code === 0) {
|
||
const taskData = Array.isArray(result.data)
|
||
? result.data.find(item => item && item.id == compareTaskId)
|
||
: result.data;
|
||
|
||
if (taskData) {
|
||
const modelsField = taskData.models;
|
||
if (modelsField) {
|
||
try {
|
||
if (typeof modelsField === 'string') {
|
||
selectedModelIds = JSON.parse(modelsField);
|
||
} else if (Array.isArray(modelsField)) {
|
||
selectedModelIds = modelsField;
|
||
}
|
||
} catch (e) {
|
||
console.error('解析模型列表失败:', e);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error('加载对比任务失败:', error);
|
||
}
|
||
}
|
||
|
||
// 加载模型列表
|
||
async function loadModels() {
|
||
try {
|
||
const response = await fetch(`${API_BASE}/model-manage`);
|
||
const result = await response.json();
|
||
if (result.code === 0) {
|
||
allModels = result.data || [];
|
||
}
|
||
} catch (error) {
|
||
console.error('加载模型失败:', error);
|
||
}
|
||
}
|
||
|
||
// 初始化输出卡片
|
||
function initializeOutputCards(showLoading = false) {
|
||
const grid = document.getElementById('outputGrid');
|
||
const allAvailableModels = [...allModels, ...mockModels];
|
||
|
||
// 如果没有选择模型,使用前4个演示模型
|
||
const displayModelIds = selectedModelIds.length > 0
|
||
? selectedModelIds.slice(0, 4)
|
||
: [1, 2, 3, 4];
|
||
|
||
const statusText = showLoading ? '加载中...' : '等待中';
|
||
const statusIcon = showLoading ? 'fa-spinner fa-spin' : 'fa-clock-o';
|
||
const statusClass = showLoading ? 'text-primary' : 'text-gray-400';
|
||
const contentText = showLoading ? '<span class="text-gray-400">正在调用模型API...</span>' : '<span class="text-gray-300">模型即将开始生成回答...</span>';
|
||
|
||
grid.innerHTML = displayModelIds.map((modelId, index) => {
|
||
// 支持 ID 数字或模型名称字符串匹配
|
||
const model = allAvailableModels.find(m =>
|
||
m.id == modelId || m.id === modelId || m.name === modelId
|
||
) || { name: modelId };
|
||
const colors = ['bg-blue-100 text-blue-700', 'bg-green-100 text-green-700', 'bg-purple-100 text-purple-700', 'bg-orange-100 text-orange-700'];
|
||
const colorClass = colors[index % colors.length];
|
||
return `
|
||
<div class="bg-white rounded-lg shadow-sm flex flex-col h-full" id="output-${modelId}">
|
||
<div class="flex items-center justify-between px-5 py-3 border-b border-gray-100 flex-shrink-0">
|
||
<span class="px-3 py-1 rounded text-sm font-medium ${colorClass}">${model.name}</span>
|
||
<span id="status-${modelId}" class="text-xs ${statusClass} flex items-center">
|
||
<i class="fa ${statusIcon} mr-1"></i>
|
||
${statusText}
|
||
</span>
|
||
</div>
|
||
<div id="content-${modelId}" class="markdown-content flex-1 p-8 text-base text-gray-600 leading-relaxed">
|
||
${contentText}
|
||
</div>
|
||
</div>
|
||
`;
|
||
}).join('');
|
||
}
|
||
|
||
// 模拟流式输出
|
||
function simulateStreaming() {
|
||
currentStreamingIntervals.forEach(interval => clearInterval(interval));
|
||
currentStreamingIntervals = [];
|
||
|
||
// 禁用重新提问按钮
|
||
const btn = document.getElementById('restartBtn');
|
||
if (btn) {
|
||
btn.disabled = true;
|
||
btn.classList.add('opacity-50', 'cursor-not-allowed');
|
||
}
|
||
|
||
const displayModelIds = selectedModelIds.length > 0
|
||
? selectedModelIds.slice(0, 4)
|
||
: [1, 2, 3, 4];
|
||
|
||
displayModelIds.forEach((modelId, index) => {
|
||
setTimeout(() => {
|
||
streamModelResponse(modelId, index);
|
||
}, index * 500);
|
||
});
|
||
}
|
||
|
||
// 获取模型对应的真实响应
|
||
function getRealResponse(modelId) {
|
||
if (chatResults.length === 0) return null;
|
||
const result = chatResults.find(r => r.model_id == modelId || r.modelId == modelId);
|
||
if (result && result.response && result.response.trim() !== '') {
|
||
return result.response;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// 获取错误信息
|
||
function getErrorMessage(modelId) {
|
||
if (chatResults.length === 0) return null;
|
||
const result = chatResults.find(r => r.model_id == modelId || r.modelId == modelId);
|
||
// 如果有 error 字段且 response 为空,返回错误信息
|
||
if (result && result.error && (!result.response || result.response.trim() === '')) {
|
||
return result.error;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
// 流式输出单个模型
|
||
function streamModelResponse(modelId, responseIndex) {
|
||
const contentEl = document.getElementById(`content-${modelId}`);
|
||
const statusEl = document.getElementById(`status-${modelId}`);
|
||
|
||
if (!contentEl || !statusEl) return;
|
||
|
||
statusEl.innerHTML = '<i class="fa fa-spinner fa-spin mr-1"></i> 生成中...';
|
||
statusEl.classList.remove('text-gray-400');
|
||
statusEl.classList.add('text-primary');
|
||
|
||
contentEl.innerHTML = '';
|
||
contentEl.classList.add('typing-cursor');
|
||
|
||
// 获取响应内容(真实数据或模拟数据)
|
||
const realResponse = getRealResponse(modelId);
|
||
const errorMessage = getErrorMessage(modelId);
|
||
|
||
let response;
|
||
let isRealData = false;
|
||
if (realResponse) {
|
||
response = realResponse;
|
||
isRealData = true;
|
||
} else if (errorMessage) {
|
||
response = `[错误] ${errorMessage}`;
|
||
} else {
|
||
response = mockResponses[responseIndex % mockResponses.length];
|
||
}
|
||
|
||
// 对于错误信息直接显示,不进行 Markdown 渲染
|
||
const isError = errorMessage && !realResponse;
|
||
// 标记是否需要 Markdown 渲染(真实数据需要)
|
||
const needMarkdown = isRealData && !isError;
|
||
|
||
// 统计信息
|
||
const startTime = Date.now();
|
||
let firstCharTime = null;
|
||
let charIndex = 0;
|
||
|
||
const interval = setInterval(() => {
|
||
if (charIndex < response.length) {
|
||
// 记录第一个字符输出的时间
|
||
if (charIndex === 0) {
|
||
firstCharTime = Date.now();
|
||
}
|
||
// 流式输出期间直接显示 Markdown 格式
|
||
const currentText = response.substring(0, charIndex + 1);
|
||
if (needMarkdown) {
|
||
contentEl.innerHTML = marked.parse(currentText);
|
||
} else {
|
||
contentEl.textContent = currentText;
|
||
}
|
||
charIndex++;
|
||
contentEl.scrollTop = contentEl.scrollHeight;
|
||
} else {
|
||
clearInterval(interval);
|
||
const idx = currentStreamingIntervals.indexOf(interval);
|
||
if (idx > -1) currentStreamingIntervals.splice(idx, 1);
|
||
contentEl.classList.remove('typing-cursor');
|
||
|
||
// 计算统计信息
|
||
const totalTime = (Date.now() - startTime) / 1000;
|
||
const firstCharLatency = firstCharTime ? ((firstCharTime - startTime) / 1000).toFixed(2) : 0;
|
||
const charCount = response.length;
|
||
const speed = totalTime > 0 ? (charCount / totalTime).toFixed(1) : 0;
|
||
|
||
// 根据状态更新UI
|
||
if (isError) {
|
||
statusEl.innerHTML = '<i class="fa fa-times-circle text-red-500 mr-1"></i> 失败';
|
||
statusEl.classList.remove('text-primary');
|
||
statusEl.classList.add('text-red-500');
|
||
} else {
|
||
statusEl.innerHTML = `<i class="fa fa-check-circle text-green-500 mr-1"></i> 完成 <span class="text-gray-400 ml-2">${speed} 字/秒 · ${totalTime.toFixed(1)}秒 · 首字 ${firstCharLatency}秒</span>`;
|
||
statusEl.classList.remove('text-primary');
|
||
statusEl.classList.add('text-green-500');
|
||
}
|
||
|
||
// 所有模型完成时启用按钮
|
||
if (currentStreamingIntervals.length === 0) {
|
||
const btn = document.getElementById('restartBtn');
|
||
if (btn) {
|
||
btn.disabled = false;
|
||
btn.classList.remove('opacity-50', 'cursor-not-allowed');
|
||
}
|
||
}
|
||
}
|
||
}, needMarkdown ? 20 : 30 + Math.random() * 40);
|
||
|
||
currentStreamingIntervals.push(interval);
|
||
}
|
||
|
||
// 重新提问
|
||
function restartQuestion() {
|
||
window.location.href = `main.html?page=model-compare-chat&id=${compareTaskId}`;
|
||
}
|
||
|
||
// 页面加载完成后初始化
|
||
setTimeout(() => {
|
||
initPage().catch(err => console.error('初始化失败:', err));
|
||
}, 50);
|
||
|
||
const restartBtn = document.getElementById('restartBtn');
|
||
if (restartBtn) {
|
||
restartBtn.onclick = restartQuestion;
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|