-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (64 loc) · 2.73 KB
/
script.js
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
document.getElementById('sendButton').addEventListener('click', () => {
const userInput = document.getElementById('userInput').value;
if (userInput) {
addMessage('User', userInput);
sendRequest(userInput);
document.getElementById('userInput').value = ''; // Clear the input field
}
});
function sendRequest(userInput) {
// Replace the URL with the actual endpoint of the REST API
const apiUrl = 'http://localhost:8081/query';
fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({ query: userInput }),
})
.then(response => response.json())
.then(data => {
displayResponse(data);
})
.catch(error => {
console.error('Error:', error);
});
}
function addMessage(sender, text) {
const messagesDiv = document.getElementById('messages');
const message = document.createElement('div');
message.classList.add('message');
message.classList.add(sender.toLowerCase());
message.innerHTML = `<div class="name">${sender}</div><div class="text">${text}</div>`;
messagesDiv.appendChild(message);
messagesDiv.scrollTop = messagesDiv.scrollHeight; // Scroll to the bottom
}
function displayResponse(data) {
addMessage('AI', data.aiResponse.response);
const sourcesTableBody = document.getElementById('sourcesTable').getElementsByTagName('tbody')[0];
sourcesTableBody.innerHTML = ''; // Clear previous sources
if (data.sources && data.sources.length > 0) {
document.getElementById('sourcesWindow').style.display = 'block';
document.getElementById('chatWindow').style.height = '50%';
data.sources.forEach(source => {
const row = document.createElement('tr');
const scoreCell = document.createElement('td');
scoreCell.textContent = source.individualScore;
row.appendChild(scoreCell);
const textSegmentCell = document.createElement('td');
textSegmentCell.textContent = source.textSegment;
row.appendChild(textSegmentCell);
const fileNameCell = document.createElement('td');
fileNameCell.textContent = source.file_name;
row.appendChild(fileNameCell);
const absoluteDirectoryPathCell = document.createElement('td');
absoluteDirectoryPathCell.textContent = source.absoluteDirectoryPath;
row.appendChild(absoluteDirectoryPathCell);
sourcesTableBody.appendChild(row);
});
} else {
document.getElementById('sourcesWindow').style.display = 'none';
document.getElementById('chatWindow').style.height = '100%';
}
}