main
HuangHai 3 weeks ago
parent 4258f94fef
commit c300fab45e

@ -294,40 +294,35 @@
const selectedDocs = Array.from(checkboxes).map(cb => cb.value);
const loader = document.getElementById('loader');
const answerArea = document.getElementById('answerArea');
answerArea.innerHTML = '';
loader.style.display = 'block';
fetch('/api/rag', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: question,
tags: selectedDocs // 使用selectedDocs作为tags参数
tags: selectedDocs
})
})
.then(response => response.json())
.then(data => {
console.log('Raw response:', data);
return data;
}) // 改回使用json()
.then(data => {
loader.style.display = 'none';
if (data && data.data) {
document.getElementById('answerArea').innerHTML = marked.parse(data.data);
MathJax.typeset();
localStorage.setItem('lastMarkdownContent', data.data);
} else {
alert('服务器返回了无效数据');
}
})
.catch(error => {
loader.style.display = 'none';
console.error('Error:', error);
alert('请求失败: ' + error.message);
});
}
.then(response => response.json())
.then(data => {
if (data.reply) {
answerArea.innerHTML = marked.parse(data.reply);
MathJax.typeset();
localStorage.setItem('lastMarkdownContent', data.reply);
}
loader.style.display = 'none';
})
.catch(error => {
console.error('Error:', error);
loader.style.display = 'none';
});
}ai.html
function clearAll() {
document.getElementById('questionInput').value = '';

@ -0,0 +1,71 @@
<!DOCTYPE html>
<html>
<head>
<title>SSE测试</title>
<link rel="icon" href="data:,">
</head>
<body>
<button onclick="callRagAPI()">调用RAG接口</button>
<div id="responseArea"></div>
<script>
async function callRagAPI() {
const responseArea = document.getElementById('responseArea');
responseArea.innerHTML = '';
const data = {
query: "小学数学中有哪些模型?",
tags: ["MATH_1"]
};
try {
const response = await fetch('http://127.0.0.1:8000/api/rag', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify(data)
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
while (true) {
const {done, value} = await reader.read();
if (done) break;
buffer += decoder.decode(value, {stream: true});
// 处理可能的多行数据
const lines = buffer.split('\n');
buffer = lines.pop();
for (const line of lines) {
if (line.includes('data:')) {
// 处理可能的多重data:前缀
const jsonStr = line.replace(/^data:\s*/, '').replace(/^data:\s*/, '').trim();
if (jsonStr) {
try {
const data = JSON.parse(jsonStr);
if (data.reply) {
// 将换行符转换为HTML换行标签
const formattedReply = data.reply.replace(/\n/g, '<br>');
responseArea.innerHTML += formattedReply;
}
} catch (e) {
console.log('忽略解析错误:', e);
}
}
}
}
}
} catch (error) {
console.error('请求失败:', error);
responseArea.innerHTML = '请求失败: ' + error.message;
}
}
</script>
</body>
</html>
Loading…
Cancel
Save