parent
4258f94fef
commit
c300fab45e
@ -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…
Reference in new issue