|
|
|
@ -3,7 +3,7 @@
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
|
<title>教育知识问答</title>
|
|
|
|
|
<title>小学数学大模型问答</title>
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
font-family: Arial, sans-serif;
|
|
|
|
@ -32,12 +32,14 @@
|
|
|
|
|
padding: 15px;
|
|
|
|
|
min-height: 300px;
|
|
|
|
|
max-height: 400px;
|
|
|
|
|
min-width: 600px; /* Added to ensure sufficient width */
|
|
|
|
|
width: 100%; /* Ensure it takes full available width */
|
|
|
|
|
overflow-y: auto;
|
|
|
|
|
background-color: #f8f9fa;
|
|
|
|
|
font-family: 'Courier New', monospace;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
line-height: 1.6;
|
|
|
|
|
white-space: pre-line;
|
|
|
|
|
white-space: normal;
|
|
|
|
|
word-wrap: break-word;
|
|
|
|
|
overflow-wrap: break-word;
|
|
|
|
|
margin-bottom: 20px;
|
|
|
|
@ -92,11 +94,36 @@
|
|
|
|
|
.status.completed {
|
|
|
|
|
color: #17a2b8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.model {
|
|
|
|
|
margin-bottom: 30px;
|
|
|
|
|
padding: 15px;
|
|
|
|
|
border-left: 4px solid #007bff;
|
|
|
|
|
background: #f8f9fa;
|
|
|
|
|
word-wrap: break-word;
|
|
|
|
|
white-space: pre-wrap;
|
|
|
|
|
overflow-wrap: break-word;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.model-title {
|
|
|
|
|
color: #007bff;
|
|
|
|
|
margin-bottom: 15px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.model-definition {
|
|
|
|
|
font-weight: bold;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.model-expression {
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
background: #e9ecef;
|
|
|
|
|
padding: 5px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div class="container">
|
|
|
|
|
<h1>教育知识问答</h1>
|
|
|
|
|
<h1>小学数学大模型问答</h1>
|
|
|
|
|
|
|
|
|
|
<div id="status" class="status">准备就绪</div>
|
|
|
|
|
|
|
|
|
@ -135,7 +162,7 @@
|
|
|
|
|
statusDiv.textContent = '正在连接...';
|
|
|
|
|
statusDiv.className = 'status connecting';
|
|
|
|
|
|
|
|
|
|
// 使用fetch发送POST请求并处理SSE流
|
|
|
|
|
// 使用fetch发送POST请求并获取完整响应
|
|
|
|
|
fetch('/api/rag', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
@ -147,55 +174,18 @@
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
|
}
|
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
|
|
statusDiv.textContent = '连接成功,等待回答...';
|
|
|
|
|
statusDiv.className = 'status connected';
|
|
|
|
|
|
|
|
|
|
function readStream() {
|
|
|
|
|
reader.read().then(({ done, value }) => {
|
|
|
|
|
if (done) {
|
|
|
|
|
statusDiv.textContent = '回答完成';
|
|
|
|
|
statusDiv.className = 'status completed';
|
|
|
|
|
submitBtn.disabled = false;
|
|
|
|
|
submitBtn.textContent = '提问';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const chunk = decoder.decode(value, { stream: true });
|
|
|
|
|
// SSE数据通常以 'data: ' 开头,并以 '\n\n' 结束
|
|
|
|
|
const lines = chunk.split('\n').filter(line => line.trim() !== '');
|
|
|
|
|
for (const line of lines) {
|
|
|
|
|
if (line.startsWith('data: ')) {
|
|
|
|
|
let data = line.substring(6);
|
|
|
|
|
if (data.trim() === '[DONE]') {
|
|
|
|
|
return response.json();
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
|
|
|
|
dataArea.innerHTML = data.data;
|
|
|
|
|
statusDiv.textContent = '回答完成';
|
|
|
|
|
statusDiv.className = 'status completed';
|
|
|
|
|
submitBtn.disabled = false;
|
|
|
|
|
submitBtn.textContent = '提问';
|
|
|
|
|
reader.cancel(); // 停止读取流
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
textBuffer += data;
|
|
|
|
|
dataArea.textContent = textBuffer;
|
|
|
|
|
dataArea.scrollTop = dataArea.scrollHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
readStream(); // 继续读取下一块
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
console.error('SSE连接错误:', error);
|
|
|
|
|
statusDiv.textContent = `连接错误或已断开: ${error.message}`;
|
|
|
|
|
statusDiv.className = 'status error';
|
|
|
|
|
submitBtn.disabled = false;
|
|
|
|
|
submitBtn.textContent = '提问';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
readStream();
|
|
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
console.error('Fetch错误:', error);
|
|
|
|
|
statusDiv.textContent = `请求发送失败: ${error.message}`;
|
|
|
|
|
console.error('请求错误:', error);
|
|
|
|
|
statusDiv.textContent = `请求错误: ${error.message}`;
|
|
|
|
|
statusDiv.className = 'status error';
|
|
|
|
|
submitBtn.disabled = false;
|
|
|
|
|
submitBtn.textContent = '提问';
|
|
|
|
|