2026-01-21 10:54:19 +08:00
|
|
|
|
<!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>
|
|
|
|
|
|
.bg-primary { background-color: #1890ff; }
|
|
|
|
|
|
.text-primary { color: #1890ff; }
|
|
|
|
|
|
:root { --primary: #1890ff; }
|
|
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
/* 卡片内容区域滚动条样式 */
|
|
|
|
|
|
.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; }
|
|
|
|
|
|
|
2026-01-21 10:54:19 +08:00
|
|
|
|
/* 流式输出光标 */
|
|
|
|
|
|
.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 h-screen 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>
|
2026-01-22 14:09:25 +08:00
|
|
|
|
console.log('>>> model-compare-result.html 脚本开始加载');
|
2026-01-26 14:33:00 +08:00
|
|
|
|
// API 基础地址 - 使用 config.yaml 中的 app.port (7861)
|
2026-01-21 10:54:19 +08:00
|
|
|
|
const getApiBase = () => {
|
|
|
|
|
|
const protocol = window.location.protocol;
|
|
|
|
|
|
const hostname = window.location.hostname;
|
2026-01-26 14:33:00 +08:00
|
|
|
|
return `${protocol}//${hostname}:7861/api`;
|
2026-01-21 10:54:19 +08:00
|
|
|
|
};
|
|
|
|
|
|
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 = '';
|
2026-01-22 14:09:25 +08:00
|
|
|
|
let chatResults = [];
|
|
|
|
|
|
let useLocalStorageResults = false; // 是否使用localStorage中的结果
|
2026-01-21 10:54:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 模拟回复内容
|
|
|
|
|
|
const mockResponses = [
|
|
|
|
|
|
"这是一个基于深度学习的自然语言处理模型回答。我可以处理各种复杂的问题,包括代码编写、文本分析、知识问答等多种任务。我的训练数据涵盖了广泛的领域知识,能够提供准确和有用的信息。",
|
|
|
|
|
|
"您好!我是一个人工智能语言模型,很高兴为您服务。我可以帮助您解答问题、提供建议、进行创意写作等。如果您有任何需要,请随时告诉我,我会尽力提供帮助。",
|
|
|
|
|
|
"这是一个很有趣的问题!从技术角度来看,我们需要考虑多个因素:数据质量、模型架构、训练策略等。深度学习在近年来取得了巨大进展,但仍然存在一些挑战需要解决。",
|
|
|
|
|
|
"根据我的理解,这个问题涉及到以下几个方面:首先,需要明确问题的具体背景;其次,要分析相关的技术方案;最后,需要评估实施的成本和收益。建议您先收集更多信息再做决定。"
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// 页面初始化
|
|
|
|
|
|
async function initPage() {
|
2026-01-22 14:09:25 +08:00
|
|
|
|
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()]);
|
2026-01-21 10:54:19 +08:00
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
// 初始化输出卡片(显示加载中状态)
|
|
|
|
|
|
initializeOutputCards(true);
|
2026-01-21 10:54:19 +08:00
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
// 读取 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>`;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-21 10:54:19 +08:00
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
// 调用批量对话 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
|
|
|
|
|
|
})
|
|
|
|
|
|
});
|
2026-01-21 10:54:19 +08:00
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
const result = await response.json();
|
|
|
|
|
|
if (result.code === 0 && result.data) {
|
|
|
|
|
|
chatResults = result.data;
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('调用API失败:', error);
|
|
|
|
|
|
}
|
2026-01-21 10:54:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 加载对比任务数据
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化输出卡片
|
2026-01-22 14:09:25 +08:00
|
|
|
|
function initializeOutputCards(showLoading = false) {
|
2026-01-21 10:54:19 +08:00
|
|
|
|
const grid = document.getElementById('outputGrid');
|
|
|
|
|
|
const allAvailableModels = [...allModels, ...mockModels];
|
|
|
|
|
|
|
|
|
|
|
|
// 如果没有选择模型,使用前4个演示模型
|
|
|
|
|
|
const displayModelIds = selectedModelIds.length > 0
|
|
|
|
|
|
? selectedModelIds.slice(0, 4)
|
|
|
|
|
|
: [1, 2, 3, 4];
|
|
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
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>';
|
|
|
|
|
|
|
2026-01-21 10:54:19 +08:00
|
|
|
|
grid.innerHTML = displayModelIds.map((modelId, index) => {
|
2026-01-22 14:21:48 +08:00
|
|
|
|
// 支持 ID 数字或模型名称字符串匹配
|
|
|
|
|
|
const model = allAvailableModels.find(m =>
|
|
|
|
|
|
m.id == modelId || m.id === modelId || m.name === modelId
|
|
|
|
|
|
) || { name: modelId };
|
2026-01-21 10:54:19 +08:00
|
|
|
|
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}">
|
2026-01-22 14:09:25 +08:00
|
|
|
|
<div class="flex items-center justify-between px-5 py-3 border-b border-gray-100 flex-shrink-0">
|
2026-01-21 10:54:19 +08:00
|
|
|
|
<span class="px-3 py-1 rounded text-sm font-medium ${colorClass}">${model.name}</span>
|
2026-01-22 14:09:25 +08:00
|
|
|
|
<span id="status-${modelId}" class="text-xs ${statusClass} flex items-center">
|
|
|
|
|
|
<i class="fa ${statusIcon} mr-1"></i>
|
|
|
|
|
|
${statusText}
|
2026-01-21 10:54:19 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
</div>
|
2026-01-22 14:09:25 +08:00
|
|
|
|
<div id="content-${modelId}" class="markdown-content flex-1 p-8 text-base text-gray-600 leading-relaxed">
|
|
|
|
|
|
${contentText}
|
2026-01-21 10:54:19 +08:00
|
|
|
|
</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);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
// 获取模型对应的真实响应
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 10:54:19 +08:00
|
|
|
|
// 流式输出单个模型
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
// 获取响应内容(真实数据或模拟数据)
|
|
|
|
|
|
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;
|
2026-01-21 10:54:19 +08:00
|
|
|
|
let charIndex = 0;
|
|
|
|
|
|
|
|
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
|
|
if (charIndex < response.length) {
|
2026-01-22 14:09:25 +08:00
|
|
|
|
// 记录第一个字符输出的时间
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2026-01-21 10:54:19 +08:00
|
|
|
|
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');
|
|
|
|
|
|
|
2026-01-22 14:09:25 +08:00
|
|
|
|
// 计算统计信息
|
|
|
|
|
|
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');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 10:54:19 +08:00
|
|
|
|
// 所有模型完成时启用按钮
|
|
|
|
|
|
if (currentStreamingIntervals.length === 0) {
|
|
|
|
|
|
const btn = document.getElementById('restartBtn');
|
|
|
|
|
|
if (btn) {
|
|
|
|
|
|
btn.disabled = false;
|
|
|
|
|
|
btn.classList.remove('opacity-50', 'cursor-not-allowed');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-22 14:09:25 +08:00
|
|
|
|
}, needMarkdown ? 20 : 30 + Math.random() * 40);
|
2026-01-21 10:54:19 +08:00
|
|
|
|
|
|
|
|
|
|
currentStreamingIntervals.push(interval);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 重新提问
|
|
|
|
|
|
function restartQuestion() {
|
|
|
|
|
|
window.location.href = `main.html?page=model-compare-chat&id=${compareTaskId}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 页面加载完成后初始化
|
2026-01-22 14:09:25 +08:00
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
initPage().catch(err => console.error('初始化失败:', err));
|
|
|
|
|
|
}, 50);
|
2026-01-21 15:33:43 +08:00
|
|
|
|
|
|
|
|
|
|
const restartBtn = document.getElementById('restartBtn');
|
|
|
|
|
|
if (restartBtn) {
|
|
|
|
|
|
restartBtn.onclick = restartQuestion;
|
2026-01-21 10:54:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
</body>
|
|
|
|
|
|
</html>
|