forked from deepflowio/stella-agent-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.html
71 lines (62 loc) · 1.99 KB
/
stream.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html>
<head>
<title>POST Request with JSON Parameter</title>
</head>
<body>
<div id="stream-container"></div>
<script>
const streamContainer = document.getElementById("stream-container");
// 构建要发送的 JSON 数据
const jsonData = {
system_content: "你是一个网络专家",
user_content: "网络安全如何设计",
};
fetch("http://127.0.0.1:30831/v1/ai/stream/azure?engine=web-gpt4-turbo", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(jsonData),
})
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
// 返回一个 ReadableStream 对象
return response.body;
})
.then((stream) => {
const reader = stream.getReader();
const textDecoder = new TextDecoder("utf-8"); // 指定字符编码为UTF-8
function readChunk() {
reader
.read()
.then(({done, value}) => {
if (done) {
// 流结束
console.log("Stream ended");
return;
}
const text = textDecoder.decode(value);
// 处理流数据,例如将其附加到页面上
const para = document.createElement("p");
para.textContent = text;
// dashscope 不需要追加,直接覆盖!!!
streamContainer.appendChild(para);
// 继续读取下一个块
readChunk();
})
.catch((error) => {
console.error("Error reading stream:", error);
});
}
// 开始读取流
readChunk();
})
.catch((error) => {
console.error("Error:", error.message);
});
</script>
</body>
</html>