Spaces:
Running
Running
Update chat.js
#1
by
TheMaisk
- opened
chat.js
CHANGED
@@ -1,60 +1,48 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
const sendMessageButton = document.getElementById(
|
4 |
-
const
|
5 |
-
const
|
6 |
-
const clearConversationsButton = document.getElementById("clear-conversations");
|
7 |
|
8 |
-
function
|
9 |
const userMessage = messageInput.value;
|
10 |
-
if (
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
}
|
15 |
-
|
16 |
-
}
|
17 |
|
18 |
-
function
|
19 |
-
const messageElement = document.createElement(
|
20 |
-
messageElement.
|
|
|
21 |
conversationHistory.appendChild(messageElement);
|
22 |
}
|
23 |
|
24 |
-
async function
|
25 |
try {
|
26 |
-
const response = await fetch(
|
27 |
method: 'POST',
|
28 |
headers: {
|
29 |
-
'Content-Type': 'application/json'
|
|
|
30 |
},
|
31 |
-
body: JSON.stringify({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
});
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
}
|
37 |
-
|
38 |
-
const responseData = await response.json();
|
39 |
-
return responseData.reply;
|
40 |
} catch (error) {
|
41 |
-
console.error('Error
|
42 |
-
return "Sorry, there was an error.";
|
43 |
}
|
44 |
}
|
45 |
-
|
46 |
-
sendMessageButton.addEventListener("click", sendMessage);
|
47 |
-
messageInput.addEventListener("keypress", function(event) {
|
48 |
-
if (event.key === "Enter") {
|
49 |
-
sendMessage();
|
50 |
-
}
|
51 |
-
});
|
52 |
-
|
53 |
-
newConversationButton.addEventListener("click", function() {
|
54 |
-
conversationHistory.innerHTML = "";
|
55 |
-
});
|
56 |
-
|
57 |
-
clearConversationsButton.addEventListener("click", function() {
|
58 |
-
conversationHistory.innerHTML = "";
|
59 |
-
});
|
60 |
});
|
|
|
1 |
+
// script.js
|
2 |
+
document.addEventListener('DOMContentLoaded', function() {
|
3 |
+
const sendMessageButton = document.getElementById('send-message');
|
4 |
+
const messageInput = document.getElementById('message-input');
|
5 |
+
const conversationHistory = document.getElementById('conversation-history');
|
|
|
6 |
|
7 |
+
sendMessageButton.addEventListener('click', function() {
|
8 |
const userMessage = messageInput.value;
|
9 |
+
if (userMessage) {
|
10 |
+
addMessageToChat('user', userMessage);
|
11 |
+
sendMessageToApi(userMessage);
|
12 |
+
messageInput.value = ''; // Clear the input field
|
13 |
+
}
|
14 |
+
});
|
|
|
15 |
|
16 |
+
function addMessageToChat(role, message) {
|
17 |
+
const messageElement = document.createElement('div');
|
18 |
+
messageElement.classList.add('message', role);
|
19 |
+
messageElement.textContent = message;
|
20 |
conversationHistory.appendChild(messageElement);
|
21 |
}
|
22 |
|
23 |
+
async function sendMessageToApi(message) {
|
24 |
try {
|
25 |
+
const response = await fetch(`https://api.fireworks.ai/inference/v1/chat/completions`, {
|
26 |
method: 'POST',
|
27 |
headers: {
|
28 |
+
'Content-Type': 'application/json',
|
29 |
+
'Authorization': 'Bearer YOUR_API_KEY_HERE' // Replace with your real API key
|
30 |
},
|
31 |
+
body: JSON.stringify({
|
32 |
+
messages: [{ role: 'user', content: message }],
|
33 |
+
model: 'accounts/fireworks/models/mixtral-8x7b-instruct',
|
34 |
+
stream: true,
|
35 |
+
n: 1,
|
36 |
+
max_tokens: 1000,
|
37 |
+
temperature: 0.9,
|
38 |
+
stop: []
|
39 |
+
})
|
40 |
});
|
41 |
+
const data = await response.json();
|
42 |
+
const botMessage = data.choices[0].message.content; // Adjust this according to the API response structure
|
43 |
+
addMessageToChat('bot', botMessage);
|
|
|
|
|
|
|
|
|
44 |
} catch (error) {
|
45 |
+
console.error('Error sending message to API:', error);
|
|
|
46 |
}
|
47 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
});
|