1.html
7.5 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<link href="css/markdown.css" rel="stylesheet" type="text/css"/>
<head>
<meta charset="UTF-8">
<title>战损版ChatGPT-SSE实现流式输出</title>
<link rel="icon" type="image/png" sizes="32x32" href="image/favicon-32x32.png">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="js/markdown.min.js"></script>
<script src="js/eventsource.min.js"></script>
<script>
function setText(text, uuid_str) {
let content = document.getElementById(uuid_str)
content.innerHTML = marked(text);
}
function uuid() {
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";
var uuid = s.join("");
return uuid;
}
window.onload = function () {
let disconnectBtn = document.getElementById("disconnectSSE");
let messageElement = document.getElementById("message");
let chat = document.getElementById("chat");
let sse;
let uid = window.localStorage.getItem("uid");
if (uid == null || uid == '' || uid == 'null') {
uid = uuid();
}
let text = '';
let uuid_str;
// 设置本地存储
window.localStorage.setItem("uid", uid);
// 回车事件
messageElement.onkeydown = function () {
if (window.event.keyCode === 13) {
if (!messageElement.value) {
return;
}
uuid_str = uuid();
//创建sse
const eventSource = new EventSourcePolyfill('/api/createSse', {
headers: {
'uid': uid
}
});
eventSource.onopen = (event) => {
console.log("开始输出后端返回值");
sse = event.target;
};
eventSource.onmessage = (event) => {
if (event.lastEventId == "[TOKENS]") {
text = text + event.data;
setText(text, uuid_str)
text = ''
return;
}
if (event.data == "[DONE]") {
if (sse) {
sse.close();
}
return;
}
let json_data = JSON.parse(event.data)
if (json_data.content == null || json_data.content == 'null') {
return;
}
text = text + json_data.content;
setText(text, uuid_str)
};
eventSource.onerror = (event) => {
console.log("onerror", event);
alert("服务异常请重试并联系开发者!")
if (event.readyState === EventSource.CLOSED) {
console.log('connection is closed');
} else {
console.log("Error occured", event);
}
event.target.close();
};
eventSource.addEventListener("customEventName", (event) => {
console.log("Message id is " + event.lastEventId);
});
eventSource.addEventListener("customEventName", (event) => {
console.log("Message id is " + event.lastEventId);
});
$.ajax({
type: 'post',
url: '/api/chat',
data: JSON.stringify({
'msg': messageElement.value
}),
contentType: "application/json;charset=UTF-8",
dataType: "json",
headers: {
"uid": uid,
},
beforeSend: function (request) {
},
success: function (result) {
//新增问题框
chat.innerHTML += '<tr><td style="height: 30px;">' + messageElement.value + '<br/><br/> tokens:' + result.question_tokens + '</td></tr>';
messageElement.value = null
//新增答案框
chat.innerHTML += '<tr><td><article id="' + uuid_str + '" class="markdown-body"></article></td></tr>';
},
complete: function () {
},
error: function () {
console.info("发送问题失败!");
}
})
}
};
disconnectBtn.onclick = function () {
if (sse) {
sse.close();
}
};
};
</script>
</head>
<body>
<div class="float-card">
<div class="float-card-item" id="disconnectSSE">
<a rel="noopener noreferrer">停止输出</a>
</div>
</div>
<div class="input-card">
<div class="input-card-item">
<input id="message" placeholder="输入你的问题,回车结束......" type="text">
</div>
</div>
<div class="container" >
<table border="1">
<tbody id="chat">
</tbody>
</table>
</div>
</body>
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
input {
height: 50px;
width: 500px;
font-size: 20px;
background: no-repeat;
color: #d0838e;
}
.container {
width: 980px;
border: 1px solid black;
display: flex;
flex-direction: column;
margin-left: 150px;
margin-top: 40px;
}
.input-card {
position: fixed;
display: inline-block;
right: 37%;
top: 80%;
}
.input-card-item {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-bottom: 16px;
}
.float-card {
position: fixed;
display: inline-block;
right: 120px;
top: 100px;
}
.float-card-item {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 60px;
height: 60px;
border-radius: 50%;
background-color: #ccccd6;
margin-bottom: 16px;
}
.float-card-item:last-child {
margin-bottom: 0px;
background-color: #d0838e;
}
.float-card-item a {
text-decoration: none;
color: #594649;
font-size: 13px;
}
</style>
</html>