parent
3abdc92bc1
commit
4598a3132b
@ -1,10 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,206 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>教育知识问答</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
.container {
|
||||
background: white;
|
||||
padding: 30px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
.data-area {
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
padding: 15px;
|
||||
min-height: 300px;
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
background-color: #f8f9fa;
|
||||
font-family: 'Courier New', monospace;
|
||||
font-size: 14px;
|
||||
line-height: 1.6;
|
||||
white-space: pre-line;
|
||||
word-wrap: break-word;
|
||||
overflow-wrap: break-word;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.input-area {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
#questionInput {
|
||||
flex: 1;
|
||||
padding: 10px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
#submitBtn {
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 10px 20px;
|
||||
font-size: 16px;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
#submitBtn:hover {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
|
||||
.status {
|
||||
text-align: center;
|
||||
margin-bottom: 20px;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.status.connecting {
|
||||
color: #ffc107;
|
||||
}
|
||||
|
||||
.status.connected {
|
||||
color: #28a745;
|
||||
}
|
||||
|
||||
.status.error {
|
||||
color: #dc3545;
|
||||
}
|
||||
|
||||
.status.completed {
|
||||
color: #17a2b8;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>教育知识问答</h1>
|
||||
|
||||
<div id="status" class="status">准备就绪</div>
|
||||
|
||||
<div class="data-area" id="dataArea">等待问题...</div>
|
||||
|
||||
<div class="input-area">
|
||||
<input type="text" id="questionInput" placeholder="请输入您的问题,例如:小学数学的学习方法">
|
||||
<button id="submitBtn" onclick="submitQuestion()">提问</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
let eventSource = null;
|
||||
let textBuffer = '';
|
||||
|
||||
function submitQuestion() {
|
||||
const question = document.getElementById('questionInput').value.trim();
|
||||
if (!question) {
|
||||
alert('请输入问题!');
|
||||
return;
|
||||
}
|
||||
|
||||
const statusDiv = document.getElementById('status');
|
||||
const dataArea = document.getElementById('dataArea');
|
||||
const submitBtn = document.getElementById('submitBtn');
|
||||
|
||||
// 清空之前的数据
|
||||
dataArea.textContent = '';
|
||||
textBuffer = '';
|
||||
|
||||
// 禁用按钮
|
||||
submitBtn.disabled = true;
|
||||
submitBtn.textContent = '处理中...';
|
||||
|
||||
// 更新状态
|
||||
statusDiv.textContent = '正在连接...';
|
||||
statusDiv.className = 'status connecting';
|
||||
|
||||
// 使用fetch发送POST请求并处理SSE流
|
||||
fetch('/api/rag', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ query: question })
|
||||
})
|
||||
.then(response => {
|
||||
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]') {
|
||||
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}`;
|
||||
statusDiv.className = 'status error';
|
||||
submitBtn.disabled = false;
|
||||
submitBtn.textContent = '提问';
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in new issue