-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.html
351 lines (260 loc) · 12.8 KB
/
chat.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jovlabs.com AI WIP</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.bundle.min.js"></script>
<link href="css/chat.css" rel="stylesheet">
</head>
<body>
<div class="container" style="margin-bottom: 0;">
<div class="row justify-content-center">
<div class="col">
<div class="card h-100 gradient-custom" style="border: 0px; margin-bottom: 0; background-color: transparent;">
<div class="card-body" data-mdb-perfect-scrollbar="true">
<div class="chat-messages" style="padding: 0%; margin: 0;">
<div class="d-flex justify-content-left" style="padding: 0px; margin: 0px;">
<p class="small mb-1 name" id="bot name" style="padding: 0px; margin: 0px;">$CHARNAME</p>
<p class="small mb-1 text-muted" id="timestamp first" style="padding: 0px; margin: 0px;"></p>
</div>
<div class="d-flex flex-row justify-content-left mb-4 pt-1" style="display: inline-block;">
<img src="imgs/user.png" id="bot png" alt="avatar 1"
style="width: 45px; height: 100%; border-radius: 10px; border: 2px solid rgb(255, 255, 255); margin-right: 10px;">
<div class="d-flex flex-row"
style="box-shadow: 0 2px 4px rgb(253, 253, 253); border: 1px solid rgb(129, 129, 129); margin-bottom: 0px; background-color: rgba(255, 255, 255, 0.808); border-radius: 10px; padding: 1%;">
<p class="firstmess" id="bot firstmess"
style="margin: 0px; padding: 0px; display: flex; align-items: center; justify-content: center;">$REPLACEFIRSTMEMESSAGE</p>
</div>
</div>
<div id="output"></div>
</div>
</div>
</div>
</div>
</div>
<div class="input-group">
<input type="text" id="prompt" class="form-control" placeholder="Type message" aria-label="Recipient's username" aria-describedby="button-addon2" />
<button onclick="executePythonCode()" class="btn bg-primary" type="button" id="button-addon2" style="padding-top: .55rem;">Send</button>
</div>
</div>
<script>
let myChar = {};
let char = '';
if (window.location.search.includes('char')) {
char = new URLSearchParams(window.location.search).get('char');
} else {
char = 'Noodlebrain';
}
const url = `chars/${char}.json`;
const jsonFilePath = `chars/${char}.json`;
fetch(jsonFilePath)
.then(response => response.json())
.then(data => {
myChar.charName = data.char_name;
myChar.charPersona = data.char_persona;
myChar.worldScenario = data.world_scenario;
myChar.exampleDialogue = data.example_dialogue;
myChar.personality = data.personality;
myChar.scenario = data.scenario;
myChar.firstMes = data.first_mes;
myChar.mesExample = data.mes_example;
const botFirstMess = document.getElementById('bot firstmess');
botFirstMess.innerHTML = myChar.firstMes;
const botName = document.getElementById('bot name');
botName.innerHTML = myChar.charName;
const botPng = document.getElementById('bot png');
botPng.src = `chars/${myChar.charName}.png`;
// executePythonCode();
})
.catch(error => console.error(error));
//code to send on enter
var promptInput = document.getElementById('prompt');
// Add event listener for the Enter key press
promptInput.addEventListener('keyup', function (event) {
if (event.key === 'Enter') {
executePythonCode();
}
});
var isExecutionPending = false;
function executePythonCode() {
if (isExecutionPending) {
return; // Exit the function if execution is pending
}
isExecutionPending = true;
// Execute your code here
setTimeout(function () {
isExecutionPending = false;
}, 1000); // Set the delay to 1 second (1000 milliseconds)
// Use myChar object here, now that it has been assigned a value
//console.log(myChar.charName);
var promptInput = document.getElementById('prompt');
// var prompt = promptInput.value;
//character info -> chat history -> your message + bot prefix
//var persona = "You are chatting with Bot. Bot is an AI assistant that helps answer your questions
var botname = myChar.charName;
var persona = myChar.charPersona + " \n\n";
var worldscenario = "World Scenario: " + myChar.scenario + " \n\n";
var dialog = "Dialog: " + myChar.exampleDialogue + " \n\n";
var charactercard = persona + worldscenario + dialog;
//console.log(charactercard);
var chatHistory = "";
var botElements = document.querySelectorAll("p#bot");
var userElements = document.querySelectorAll("p#user");
for (var i = 0; i < Math.max(botElements.length, userElements.length); i++) {
if (botElements[i]) {
chatHistory += botname + ": " + botElements[i].textContent.trim() + "\n";
}
if (userElements[i]) {
chatHistory += "User: " + userElements[i].textContent.trim() + "\n";
}
}
// Check if chatHistory exceeds 6000 characters 2048 * 3 = 6144 chars - 144 for fudge factor
if (chatHistory.length > 6000) {
var excessCharacters = chatHistory.length - 6000;
chatHistory = chatHistory.slice(excessCharacters * -1);
console.log('trimming history');
}
var tokens = chatHistory.length;
console.log(tokens);
//console.log(chatHistory);
var userInput = promptInput.value;
//character info -> chat history -> your message + bot prefix
var prompt = charactercard + "\n" + chatHistory + "You: " + userInput + "\n" + botname + ": ";
// Clear the input field
promptInput.value = '';
// Create the chat message elements for the user's
var botInfoContainer = document.createElement("div");
botInfoContainer.className = "d-flex justify-content-end";
botInfoContainer.style.padding = "0";
botInfoContainer.style.margin = "0";
// Create the first <p> element for the bot name
var botNameElement = document.createElement("p");
botNameElement.className = "small mb-1 name";
botNameElement.id = "bot name";
botNameElement.style.padding = "0";
botNameElement.style.margin = "0";
botNameElement.textContent = "User";
// Create the second <p> element for the timestamp
var timestampElement = document.createElement("p");
timestampElement.className = "small mb-1 text-muted";
timestampElement.id = "timestamp";
timestampElement.style.padding = "0";
timestampElement.style.margin = "0";
timestampElement.style.marginRight = "0";
// Append the two <p> elements to the bot info container
botInfoContainer.appendChild(botNameElement);
botInfoContainer.appendChild(timestampElement);
// Append the bot info container to the chat body
var chatBody = document.getElementsByClassName("card-body")[0];
chatBody.appendChild(botInfoContainer);
var userInputContainer = document.createElement("div");
userInputContainer.className = "d-flex flex-row justify-content-end mb-4 pt-1";
userInputContainer.style.display = "inline-block";
var userInputContent = document.createElement("div");
userInputContent.className = "d-flex flex-row";
userInputContent.style.border = "1px solid rgb(129, 129, 129)";
userInputContent.style.boxShadow = "0 2px 4px rgba(255, 255, 255, 1)";
userInputContent.style.marginBottom = "0";
userInputContent.style.backgroundColor = "rgba(255, 255, 255, 0.808)";
userInputContent.style.borderRadius = "10px";
userInputContent.style.padding = "1%";
userInputContainer.appendChild(userInputContent);
var userInputText = document.createElement("p");
userInputText.className = "firstmess";
userInputText.id = "user";
userInputText.textContent = userInput;
userInputText.style.margin = "0";
userInputText.style.padding = "0";
userInputText.style.display = "flex";
userInputText.style.alignItems = "center";
userInputText.style.justifyContent = "center";
var userAvatar = document.createElement("img");
userAvatar.src = "imgs/user.png";
userAvatar.alt = "avatar 1";
userAvatar.style.width = "45px";
userAvatar.style.height = "100%";
userAvatar.style.borderRadius = "10px";
userAvatar.style.border = "2px solid white";
userAvatar.style.marginLeft = "10px";
userInputContent.appendChild(userInputText);
userInputContainer.appendChild(userInputContent);
userInputContainer.appendChild(userAvatar);
// Append the user's input message elements to the chat body
var chatBody = document.getElementsByClassName("card-body")[0];
chatBody.appendChild(userInputContainer);
// Scroll to the bottom of the chat body
chatBody.scrollTop = chatBody.scrollHeight;
// Make an AJAX request to the Python backend
var xhr = new XMLHttpRequest();
xhr.open("POST", "execute.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = xhr.responseText;
// Remove the original prompt from the response
response = response.replace(prompt, '');
// Create the response message elements
var responseContainer = document.createElement("div");
responseContainer.className = "d-flex justify-content-left";
responseContainer.style.padding = "0px";
responseContainer.style.margin = "0px";
var nameElement = document.createElement("p");
nameElement.className = "small mb-1 name";
nameElement.id = "bot name";
nameElement.style.padding = "0px";
nameElement.style.margin = "0px";
nameElement.textContent = botname;
var timestampElement = document.createElement("p");
timestampElement.className = "small mb-1 text-muted";
timestampElement.id = "timestamp first";
timestampElement.style.padding = "0px";
timestampElement.style.margin = "0px";
responseContainer.appendChild(nameElement);
responseContainer.appendChild(timestampElement);
var avatarElement = document.createElement("img");
avatarElement.src = "chars/" + botname + ".png";
avatarElement.id = "bot png";
avatarElement.alt = "avatar 1";
avatarElement.style.width = "45px";
avatarElement.style.height = "100%";
avatarElement.style.borderRadius = "10px";
avatarElement.style.border = "2px solid white";
avatarElement.style.marginRight = "10px";
var responseContentContainer = document.createElement("div");
responseContentContainer.className = "d-flex flex-row justify-content-left mb-4 pt-1";
responseContentContainer.style.display = "inline-block";
var responseContent = document.createElement("div");
responseContent.className = "d-flex flex-row";
responseContent.style.border = "1px solid rgb(129, 129, 129)";
responseContent.style.boxShadow = "0 2px 4px rgba(255, 255, 255, 1)";
responseContent.style.marginBottom = "0px";
responseContent.style.backgroundColor = "rgba(255, 255, 255, 0.808)";
responseContent.style.borderRadius = "10px";
responseContent.style.padding = "1%";
var responseText = document.createElement("p");
responseText.className = "";
responseText.id = "bot";
responseText.style.margin = "0px";
responseText.style.padding = "0px";
responseText.style.display = "flex";
responseText.style.alignItems = "center";
responseText.style.justifyContent = "center";
responseText.innerHTML = response;
responseContent.appendChild(responseText);
responseContentContainer.appendChild(avatarElement);
responseContentContainer.appendChild(responseContent);
// Append the response message elements to the chat body
chatBody.appendChild(responseContainer);
chatBody.appendChild(responseContentContainer);
// Scroll to the bottom of the chat body
chatBody.scrollTop = chatBody.scrollHeight;
}
};
console.log(prompt);
xhr.send("prompt=" + encodeURIComponent(prompt));
}
</script>
</body>
</html>