-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.html
319 lines (283 loc) · 13 KB
/
example.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
<!-- MIT License
Copyright (c) 2024 DataStax, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
**THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.** -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LangFlow Chat with Managed Sessions</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
.container {
max-width: 1200px;
margin: auto;
padding: 5px 10px;
}
.config-container input, .config-container button {
width: auto;
margin: 5px 5px;
}
.chat-header {
font-size: 20px;
color: #333;
margin-bottom: 10px;
}
.chat-container {
flex: 1 1 auto;
max-width: 330px;
margin: 10px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
th, td {
border: 1px solid #ccc;
padding: 5px;
text-align: left;
}
.chat-container-row {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
@media (max-width: 600px) {
.chat-container-row {
flex-direction: column;
}
.chat-container {
flex: 1 1 100%;
}
.container {
padding: 10px;
}
}
</style>
</head>
<body>
<div class="container config-container">
<h2>Configuration Settings</h2>
<div>
<input type="text" id="hostUrlInput" placeholder="Enter new host URL" value="http://localhost:7860">
<button onclick="updateHostUrl()">Update Host URL</button>
</div>
<div>
<input type="text" id="labelInput" placeholder="Label for new session">
<button onclick="addSession()">Add Session</button>
</div>
<table id="sessionTable">
<tr>
<th>Label</th>
<th>Session ID</th>
<th>Actions</th>
</tr>
</table>
</div>
<div class="container">
<h2>Chat Sessions</h2>
<div class="chat-container-row">
<div class="chat-container">
<div class="chat-header">Pirate Prompt</div>
Session: <select id="pirateChatSelector" onchange="updateSession('langflowPiratePrompt', this.value)"></select>
<langflow-chat
id="langflowPiratePrompt"
window_title="Pirate Prompt"
flow_id="pirate-prompt"
host_url="http://localhost:7860"
></langflow-chat>
</div>
<div class="chat-container">
<div class="chat-header">Basic Memory Chat</div>
Session: <select id="basicMemoryChatSelector" onchange="updateSession('langflowBasicMemoryChat', this.value)"></select>
<langflow-chat
id="langflowBasicMemoryChat"
window_title="Basic Memory Chat"
flow_id="basic-memory-chatbot"
host_url="http://localhost:7860"
></langflow-chat>
</div>
<div class="chat-container">
<div class="chat-header">Astra Memory Chat</div>
Session: <select id="astraMemoryChatSelector" onchange="updateSession('langflowAstraMemoryChat', this.value)"></select>
<langflow-chat
id="langflowAstraMemoryChat"
window_title="Astra Memory Chat"
flow_id="astra-memory-chatbot"
host_url="http://localhost:7860"
></langflow-chat>
</div>
</div>
</div>
<div class="container">
<h2>Document Summary</h2>
<input type="file" id="fileInput" accept="application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document, text/plain, application/rtf, text/markdown, text/html" />
<select id="summaryEndpoint">
<option value="research-paper-digest">Research Paper Digest</option>
</select>
<button onclick="loadAndDigestFile()">Summarize Document</button>
<div id="summaryOutput"></div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/uuid.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/logspace-ai/[email protected]/dist/build/static/js/bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify/dist/purify.min.js"></script>
<script>
let currentHostUrl = localStorage.getItem('hostUrl') || document.getElementById('hostUrlInput').value; // Retrieve from localStorage or use default
function updateHostUrl() {
const newUrl = document.getElementById('hostUrlInput').value;
if (newUrl) { // Check if the URL is not empty
currentHostUrl = newUrl;
localStorage.setItem('hostUrl', newUrl); // Save the new URL in localStorage
const chatComponents = document.querySelectorAll('langflow-chat');
chatComponents.forEach(chat => {
chat.setAttribute('host_url', newUrl);
});
}
}
let sessions = JSON.parse(localStorage.getItem('sessions')) || [{'label': 'initial', 'id': uuid.v4()}];
localStorage.setItem('sessions', JSON.stringify(sessions));
function populateSessionTable() {
const table = document.getElementById('sessionTable');
table.innerHTML = '<tr><th>Label</th><th>Session ID</th><th>Actions</th></tr>'; // Reset table
sessions.forEach((session, index) => {
const row = table.insertRow(-1);
row.insertCell(0).innerText = session.label;
row.insertCell(1).innerText = session.id;
const deleteBtn = document.createElement('button');
deleteBtn.innerText = 'Delete';
deleteBtn.onclick = function() { deleteSession(index); };
row.insertCell(2).appendChild(deleteBtn);
});
populateSelectors();
}
function populateSelectors() {
const selectors = [document.getElementById('pirateChatSelector'), document.getElementById('basicMemoryChatSelector'), document.getElementById('astraMemoryChatSelector')];
selectors.forEach(selector => {
selector.innerHTML = ''; // Clear selector
sessions.forEach(session => {
const option = document.createElement('option');
option.value = session.id;
option.textContent = session.label;
selector.appendChild(option);
});
});
if (sessions.length > 0) {
updateAllChatsSession(sessions[0].id); // Set all chats to the first session ID
}
}
function addSession() {
const label = document.getElementById('labelInput').value;
if (label) {
sessions.push({'label': label, 'id': uuid.v4()});
localStorage.setItem('sessions', JSON.stringify(sessions));
populateSessionTable();
}
}
function deleteSession(index) {
sessions.splice(index, 1);
localStorage.setItem('sessions', JSON.stringify(sessions));
populateSessionTable();
}
function rebuildChatComponent(chatId, sessionId) {
const container = document.getElementById(chatId).parentNode; // Get the parent container
const oldChat = document.getElementById(chatId);
const newChat = document.createElement('langflow-chat'); // Create a new chat component
// Set attributes based on old chat attributes or new session ID
newChat.id = oldChat.id;
newChat.setAttribute('window_title', oldChat.getAttribute('window_title'));
newChat.setAttribute('flow_id', oldChat.getAttribute('flow_id'));
newChat.setAttribute('host_url', oldChat.getAttribute('host_url'));
newChat.setAttribute('session_id', sessionId); // Set the new session ID
container.replaceChild(newChat, oldChat); // Replace the old chat with the new one
}
function updateSession(chatId, sessionId) {
rebuildChatComponent(chatId, sessionId);
}
function updateAllChatsSession(sessionId) {
updateSession('langflowPiratePrompt', sessionId);
updateSession('langflowBasicMemoryChat', sessionId);
updateSession('langflowAstraMemoryChat', sessionId);
}
window.onload = function() {
document.getElementById('hostUrlInput').value = currentHostUrl;
populateSessionTable();
updateHostUrl();
};
function loadAndDigestFile() {
const file = document.getElementById('fileInput').files[0];
const endpoint = document.getElementById('summaryEndpoint').value;
// Clear previous content and show loading message immediately
const summaryOutputDiv = document.getElementById('summaryOutput');
summaryOutputDiv.innerHTML = '<div style="margin-top: 20px;">Processing...</div>';
if (file) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
const base64Content = reader.result.split(',')[1];
const filename = file.name;
sendFileToAPI(endpoint, base64Content, filename);
};
reader.onerror = function (error) {
console.error('Error reading file: ', error);
summaryOutputDiv.innerHTML = `<p>Error reading file: ${error.message}</p>`; // Display error message in the summary output
};
} else {
alert('Please select a file before clicking "Summarize Document".');
}
}
function sendFileToAPI(endpoint, base64Content, filename) {
const apiURL = `${currentHostUrl}/api/v1/run/${endpoint}?stream=false`;
fetch(apiURL, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
output_type: "chat",
input_type: "chat",
"tweaks": {
"Parameter Input": {
"base64_file": base64Content,
"filename": filename
}
}
})
})
.then(response => {
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
})
.then(data => {
// console.log("API Data:", data);
// Extract the markdown text from the deeply nested structure
var markdownText = data.outputs[0].outputs[0].results.text.text;
const renderedHTML = marked.parse(markdownText || '');
document.getElementById('summaryOutput').innerHTML = DOMPurify.sanitize(renderedHTML);
})
.catch(error => {
console.error('Error:', error);
document.getElementById('summaryOutput').innerHTML = `<p>An error occurred while processing your document: ${error}</p>`;
document.getElementById('loadingIndicator').style.display = 'none'; // Hide loading indicator on error
});
}
</script>
</body>
</html>