js / chat.js
TheMaisk's picture
Update chat.js
1710d5f verified
raw
history blame
1.99 kB
// script.js
document.addEventListener('DOMContentLoaded', function() {
const sendMessageButton = document.getElementById('send-message');
const messageInput = document.getElementById('message-input');
const conversationHistory = document.getElementById('conversation-history');
sendMessageButton.addEventListener('click', function() {
const userMessage = messageInput.value;
if (userMessage) {
addMessageToChat('user', userMessage);
sendMessageToApi(userMessage);
messageInput.value = ''; // Clear the input field
}
});
function addMessageToChat(role, message) {
const messageElement = document.createElement('div');
messageElement.classList.add('message', role);
messageElement.textContent = message;
conversationHistory.appendChild(messageElement);
}
async function sendMessageToApi(message) {
try {
const response = await fetch(`https://api.fireworks.ai/inference/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY_HERE' // Replace with your real API key
},
body: JSON.stringify({
messages: [{ role: 'user', content: message }],
model: 'accounts/fireworks/models/mixtral-8x7b-instruct',
stream: true,
n: 1,
max_tokens: 1000,
temperature: 0.9,
stop: []
})
});
const data = await response.json();
const botMessage = data.choices[0].message.content; // Adjust this according to the API response structure
addMessageToChat('bot', botMessage);
} catch (error) {
console.error('Error sending message to API:', error);
}
}
});