-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
213 lines (182 loc) · 5.11 KB
/
Copy pathindex.html
File metadata and controls
213 lines (182 loc) · 5.11 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Chatbot</title>
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
max-width: 600px;
margin: auto;
background-color: #f9f9f9;
}
h2 {
text-align: center;
}
#chatbox {
white-space: pre-wrap;
border: 1px solid #ccc;
padding: 10px;
height: 400px;
overflow-y: auto;
background: #fff;
margin-bottom: 10px;
}
#inputContainer {
display: flex;
gap: 8px;
margin-bottom: 10px;
}
#webToggleBtn {
background-color: #e0e0e0;
color: #333;
}
#webToggleBtn.active {
background-color: #007bff;
color: white;
}
input[type="text"] {
flex: 1;
padding: 10px;
font-size: 16px;
border: 1px solid #999;
border-radius: 4px;
}
button {
padding: 10px 16px;
font-size: 14px;
border: none;
border-radius: 4px;
cursor: pointer;
}
#sendBtn {
background-color: #28a745;
color: white;
}
#sendBtn:hover {
background-color: #218838;
}
#clearBtn {
background-color: #dc3545;
color: white;
}
#clearBtn:hover {
background-color: #b52a37;
}
.loading {
font-style: italic;
color: #999;
}
</style>
</head>
<body>
<h2>Chatbot</h2>
<div id="chatbox">🤖 Ask me something...</div>
<div id="inputContainer">
<button id="webToggleBtn">🌐 Web</button>
<input type="text" id="queryInput" placeholder="Type your question here" />
<button id="sendBtn">Send</button>
<button id="clearBtn">Clear Chat</button>
</div>
<script>
const input = document.getElementById("queryInput");
const chatbox = document.getElementById("chatbox");
const sendBtn = document.getElementById("sendBtn");
const webToggleBtn = document.getElementById("webToggleBtn");
const clearBtn = document.getElementById("clearBtn");
let webSearchEnabled = false;
webToggleBtn.addEventListener("click", () => {
webSearchEnabled = !webSearchEnabled;
webToggleBtn.classList.toggle("active");
});
clearBtn.addEventListener("click", () => {
chatbox.innerHTML = "🤖 Ask me something...";
input.value = "";
input.focus();
});
input.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
e.preventDefault();
sendBtn.click();
}
});
sendBtn.addEventListener("click", async () => {
const question = input.value.trim();
if (!question) return;
appendMessage(`\n\n🧑💼 You: ${question}`);
input.value = "";
scrollToBottom();
if (webSearchEnabled) {
appendMessage(`\n🌐 Web Search enabled. Searching...`);
scrollToBottom();
await performWebSearch(question);
} else {
appendMessage(`\n🤖 Bot: `);
const responseEl = document.createElement("span");
responseEl.classList.add("loading");
responseEl.textContent = "⏳ Thinking...";
chatbox.appendChild(responseEl);
scrollToBottom();
await askLLM(question, responseEl);
}
});
function appendMessage(text) {
chatbox.innerHTML += text;
}
async function askLLM(question, responseEl) {
try {
const start = Date.now();
const res = await fetch("http://localhost:8000/ask-stream", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ question }),
});
if (!res.ok) {
responseEl.textContent = "⚠️ Server Error.";
return;
}
const reader = res.body.getReader();
const decoder = new TextDecoder("utf-8");
let fullText = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = decoder.decode(value, { stream: true });
fullText += chunk;
responseEl.textContent = fullText.trim();
scrollToBottom();
}
const end = ((Date.now() - start) / 1000).toFixed(2);
appendMessage(`\n⏱️ Response time: ${end} sec`);
} catch (err) {
responseEl.textContent = "⚠️ Failed to connect to backend.";
}
}
async function performWebSearch(query) {
try {
const res = await fetch("http://localhost:8000/search", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query }),
});
const data = await res.json();
if (data.results && data.results.length) {
const links = data.results
.map((r) => `\n${r.body}\n${r.href}`)
.join("\n\n");
appendMessage(`\n\n🌐 Web Results:\n${links}`);
} else {
appendMessage("\n❌ No results found.");
}
} catch (err) {
appendMessage("\n⚠️ Web search error.");
}
scrollToBottom();
}
function scrollToBottom() {
chatbox.scrollTop = chatbox.scrollHeight;
}
</script>
</body>
</html>