-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.html
More file actions
284 lines (241 loc) · 10.7 KB
/
demo.html
File metadata and controls
284 lines (241 loc) · 10.7 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<!DOCTYPE html>
<html>
<head>
<title>Demo chat</title>
<style>
#message-window {
width: 1000px;
height: 600px;
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 10px;
overflow-y: scroll;
}
#message-input {
width: 800px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="auth">
<input id="register-user-id" type="text" placeholder="User ID for registration">
<input id="register-password" type="password" placeholder="Password for registration">
<button onclick="register(event)">Register</button>
<input id="login-user-id" type="text" placeholder="User ID for login">
<input id="login-password" type="password" placeholder="Password for login">
<button onclick="login(event)">Login</button>
</div>
<div id="message-window"></div>
<input id="message-input" type="text" placeholder="Type your message here...">
<button onclick="submitForm(event)">Send</button>
<button onclick="newConversation(event)" id="new-conversation-button">New Conversation</button>
<div id="conversation-list"></div>
<script>
function register(event) {
event.preventDefault();
var userId = document.getElementById('register-user-id').value;
var password = document.getElementById('register-password').value;
fetch('http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ user_id: userId, password })
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
alert('Registration successful');
console.log(data)
})
.catch((error) => {
console.error('Error:', error);
alert('Registration failed');
});
}
function login(event) {
event.preventDefault();
var userId = document.getElementById('login-user-id').value;
var password = document.getElementById('login-password').value;
fetch('http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `username=${encodeURIComponent(userId)}&password=${encodeURIComponent(password)}&grant_type=password`
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
alert('Login successful');
localStorage.setItem('token', data.access_token);
// reload the page to show the conversation list
location.reload();
})
.catch((error) => {
console.error('Error:', error);
alert('Login failed');
});
}
document.getElementById('message-input').addEventListener('keypress', function (event) {
var input = document.getElementById('message-input').value;
if (event.key === 'Enter') {
event.preventDefault();
if (input == '') {
return;
}
submitForm(event);
}
});
// Create a chat history object
// This object will be used to keep track of the conversation for multi-turn conversations
let chatHistory = {
"content": []
};
let currentConversationId = null;
function submitForm(event) {
event.preventDefault();
var message = document.getElementById('message-input').value;
// The user's message is added as a new object with the role 'user' and the content of the message
chatHistory.content.push({
"role": "user",
"parts": [message]
});
let messageElement = document.createElement('p');
messageElement.textContent = message;
document.getElementById('message-window').appendChild(messageElement);
let data = JSON.stringify(
{
"content": chatHistory.content
}
);
fetch('http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + localStorage.getItem('token')
},
body: data
})
.then(response => {
// The response from the API is a ReadableStream
// This allows for the data to be read in chunks as it is being received
const reader = response.body.getReader();
let responseElement = document.getElementById('response');
let chunks = '';
let tempElement = document.createElement('p');
document.getElementById('message-window').appendChild(tempElement);
// // Start reading the response from the API
// The response is read in chunks to allow for dynamic display of the model's response
return reader.read().then(function processText({ done, value }) {
// check to see if the response has been fully received
if (done) {
// The model's response is added as a new object with the role 'model'
// and the content of the response
chatHistory.content.push({
"role": "model",
"parts": [chunks]
});
let responseElement = document.createElement('p');
responseElement.textContent = chunks;
document.getElementById('message-window').replaceChild(responseElement, tempElement);
saveChat(chatHistory);
return;
}
let chunk = new TextDecoder('utf-8').decode(value);
chunks += chunk;
tempElement.textContent = chunks;
// Continue reading the next chunk of the response
return reader.read().then(processText);
});
})
.catch((error) => {
console.error('Error:', error);
});
document.getElementById('message-input').value = '';
}
function getAllConversationTitles() {
fetch('http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/user/me/conversations', {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('token')
}
})
.then(response => response.json())
.then(data => {
let conversationListElement = document.getElementById('conversation-list');
conversationListElement.innerHTML = '';
data.forEach(conversation => {
let conversationElement = document.createElement('p');
conversationElement.textContent = conversation.title;
conversationElement.addEventListener('click', function () {
currentConversationId = conversation.conversation_id;
getConversation(conversation.conversation_id);
});
conversationListElement.appendChild(conversationElement);
});
})
.catch((error) => {
console.error('Error:', error);
});
}
function getConversation(conversationId) {
fetch(`http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/user/me/conversations/${conversationId}`, {
method: 'GET',
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('token')
}
})
.then(response => response.json())
.then(data => {
chatHistory.content = data;
let messageWindowElement = document.getElementById('message-window');
messageWindowElement.innerHTML = '';
data.forEach(message => {
let messageElement = document.createElement('p');
messageElement.textContent = message.parts.join(' ');
messageWindowElement.appendChild(messageElement);
});
})
.catch((error) => {
console.error('Error:', error);
});
}
function saveChat(chatHistory) {
fetch(`http://ec2-18-224-51-247.us-east-2.compute.amazonaws.com:8000/user/me/conversations/${currentConversationId}`, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + localStorage.getItem('token'),
'Content-Type': 'application/json'
},
body: JSON.stringify(chatHistory)
})
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch((error) => {
console.error('Error:', error);
});
}
function newConversation(event) {
document.getElementById('message-window').innerHTML = '';
currentConversationId = 0;
chatHistory.content = [];
saveChat(chatHistory);
getAllConversationTitles();
}
// Call getAllConversationTitles when the page loads
window.onload = getAllConversationTitles;
</script>
</body>
</html>