Spaces:
Running
Running
Update index.html
Browse files- index.html +101 -93
index.html
CHANGED
@@ -177,100 +177,108 @@
|
|
177 |
const baseURL = 'https://api.aimlapi.com/v1';
|
178 |
|
179 |
async function sendMessage() {
|
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 |
-
role: 'system',
|
218 |
-
content: 'You are an AI assistant who knows everything.'
|
219 |
-
},
|
220 |
-
{
|
221 |
-
role: 'user',
|
222 |
-
content: userInput
|
223 |
-
}
|
224 |
-
]
|
225 |
-
})
|
226 |
-
});
|
227 |
-
|
228 |
-
const data = await response.json();
|
229 |
-
const botMessage = data.choices[0].message.content;
|
230 |
-
|
231 |
-
// Remove typing animation
|
232 |
-
chatBox.removeChild(typingAnimation);
|
233 |
-
|
234 |
-
// Display bot message with typewriter animation
|
235 |
-
const botMessageElement = document.createElement('div');
|
236 |
-
botMessageElement.classList.add('message', 'bot-message');
|
237 |
-
chatBox.appendChild(botMessageElement);
|
238 |
-
|
239 |
-
// Function to simulate typewriter effect (faster)
|
240 |
-
function typeWriter(text, element, delay = 20) { // Reduced delay to 20ms
|
241 |
-
let i = 0;
|
242 |
-
function type() {
|
243 |
-
if (i < text.length) {
|
244 |
-
element.textContent += text.charAt(i);
|
245 |
-
i++;
|
246 |
-
setTimeout(type, delay);
|
247 |
-
}
|
248 |
}
|
249 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
}
|
251 |
-
|
252 |
-
// Check if the message contains code
|
253 |
-
if (botMessage.includes('```')) {
|
254 |
-
const [textBeforeCode, codeSnippet] = botMessage.split('```');
|
255 |
-
const codeElement = document.createElement('code');
|
256 |
-
codeElement.style.display = 'block'; // Ensure code block is displayed properly
|
257 |
-
|
258 |
-
// Display text before code
|
259 |
-
typeWriter(textBeforeCode, botMessageElement);
|
260 |
-
|
261 |
-
// Display code snippet after a short delay
|
262 |
-
setTimeout(() => {
|
263 |
-
codeElement.textContent = codeSnippet;
|
264 |
-
botMessageElement.appendChild(codeElement);
|
265 |
-
}, textBeforeCode.length * 20 + 500); // Adjusted delay based on faster typing
|
266 |
-
} else {
|
267 |
-
// Display plain text with typewriter effect
|
268 |
-
typeWriter(botMessage, botMessageElement);
|
269 |
-
}
|
270 |
-
|
271 |
-
// Scroll to bottom
|
272 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
273 |
}
|
274 |
-
|
275 |
-
|
276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
const baseURL = 'https://api.aimlapi.com/v1';
|
178 |
|
179 |
async function sendMessage() {
|
180 |
+
const userInput = document.getElementById('user-input').value;
|
181 |
+
if (!userInput) return;
|
182 |
+
|
183 |
+
// Display user message
|
184 |
+
const chatBox = document.getElementById('chat-box');
|
185 |
+
const userMessage = document.createElement('div');
|
186 |
+
userMessage.classList.add('message', 'user-message');
|
187 |
+
userMessage.textContent = userInput;
|
188 |
+
chatBox.appendChild(userMessage);
|
189 |
+
|
190 |
+
// Clear input
|
191 |
+
document.getElementById('user-input').value = '';
|
192 |
+
|
193 |
+
// Show typing animation
|
194 |
+
const typingAnimation = document.createElement('div');
|
195 |
+
typingAnimation.classList.add('message', 'bot-message', 'typing-animation');
|
196 |
+
typingAnimation.innerHTML = `
|
197 |
+
<span></span>
|
198 |
+
<span></span>
|
199 |
+
<span></span>
|
200 |
+
`;
|
201 |
+
chatBox.appendChild(typingAnimation);
|
202 |
+
|
203 |
+
// Scroll to bottom
|
204 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
205 |
+
|
206 |
+
// Send message to API
|
207 |
+
const response = await fetch(`${baseURL}/chat/completions`, {
|
208 |
+
method: 'POST',
|
209 |
+
headers: {
|
210 |
+
'Content-Type': 'application/json',
|
211 |
+
'Authorization': `Bearer ${apiKey}`
|
212 |
+
},
|
213 |
+
body: JSON.stringify({
|
214 |
+
model: 'gpt-4.5-preview',
|
215 |
+
messages: [
|
216 |
+
{
|
217 |
+
role: 'system',
|
218 |
+
content: 'You are an AI assistant who knows everything.'
|
219 |
},
|
220 |
+
{
|
221 |
+
role: 'user',
|
222 |
+
content: userInput
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
}
|
224 |
+
]
|
225 |
+
})
|
226 |
+
});
|
227 |
+
|
228 |
+
const data = await response.json();
|
229 |
+
const botMessage = data.choices[0].message.content;
|
230 |
+
|
231 |
+
// Remove typing animation
|
232 |
+
chatBox.removeChild(typingAnimation);
|
233 |
+
|
234 |
+
// Display bot message with typewriter animation
|
235 |
+
const botMessageElement = document.createElement('div');
|
236 |
+
botMessageElement.classList.add('message', 'bot-message');
|
237 |
+
chatBox.appendChild(botMessageElement);
|
238 |
+
|
239 |
+
// Function to simulate typewriter effect
|
240 |
+
function typeWriter(text, element, delay = 20) { // Reduced delay to 20ms
|
241 |
+
let i = 0;
|
242 |
+
function type() {
|
243 |
+
if (i < text.length) {
|
244 |
+
element.textContent += text.charAt(i);
|
245 |
+
i++;
|
246 |
+
setTimeout(type, delay);
|
247 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
}
|
249 |
+
type();
|
250 |
+
}
|
251 |
+
|
252 |
+
// Parse and display the bot message
|
253 |
+
let remainingText = botMessage;
|
254 |
+
while (remainingText.includes('```')) {
|
255 |
+
// Extract text before the code block
|
256 |
+
const [textBeforeCode, rest] = remainingText.split('```', 2);
|
257 |
+
typeWriter(textBeforeCode, botMessageElement);
|
258 |
+
|
259 |
+
// Extract the code block
|
260 |
+
const [codeSnippet, restAfterCode] = rest.split('```', 2);
|
261 |
+
const codeElement = document.createElement('code');
|
262 |
+
codeElement.classList.add('code-block'); // Optional: Add a class for styling
|
263 |
+
codeElement.style.display = 'block';
|
264 |
+
|
265 |
+
// Simulate typewriter effect for the code block
|
266 |
+
setTimeout(() => {
|
267 |
+
typeWriter(codeSnippet, codeElement);
|
268 |
+
botMessageElement.appendChild(codeElement);
|
269 |
+
}, textBeforeCode.length * 20 + 50); // Adjusted delay
|
270 |
+
|
271 |
+
// Update remaining text
|
272 |
+
remainingText = restAfterCode || '';
|
273 |
+
}
|
274 |
+
|
275 |
+
// Append any remaining text after the last code block
|
276 |
+
if (remainingText) {
|
277 |
+
setTimeout(() => {
|
278 |
+
typeWriter(remainingText, botMessageElement);
|
279 |
+
}, botMessageElement.textContent.length * 20 + 50); // Adjusted delay
|
280 |
+
}
|
281 |
+
|
282 |
+
// Scroll to bottom
|
283 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
284 |
+
}
|