Files changed (1) hide show
  1. chat.js +33 -45
chat.js CHANGED
@@ -1,60 +1,48 @@
1
- document.addEventListener("DOMContentLoaded", function() {
2
- const messageInput = document.getElementById("message-input");
3
- const sendMessageButton = document.getElementById("send-message");
4
- const conversationHistory = document.getElementById("conversation-history");
5
- const newConversationButton = document.getElementById("new-conversation");
6
- const clearConversationsButton = document.getElementById("clear-conversations");
7
 
8
- function sendMessage() {
9
  const userMessage = messageInput.value;
10
- if (!userMessage) return;
11
- addToConversationHistory("You", userMessage);
12
- getBotResponse(userMessage).then(botMessage => {
13
- addToConversationHistory("Bot", botMessage);
14
- });
15
- messageInput.value = "";
16
- }
17
 
18
- function addToConversationHistory(sender, message) {
19
- const messageElement = document.createElement("p");
20
- messageElement.textContent = `${sender}: ${message}`;
 
21
  conversationHistory.appendChild(messageElement);
22
  }
23
 
24
- async function getBotResponse(message) {
25
  try {
26
- const response = await fetch('/chatbot', {
27
  method: 'POST',
28
  headers: {
29
- 'Content-Type': 'application/json'
 
30
  },
31
- body: JSON.stringify({ query: message })
 
 
 
 
 
 
 
 
32
  });
33
-
34
- if (!response.ok) {
35
- throw new Error('Network response was not ok');
36
- }
37
-
38
- const responseData = await response.json();
39
- return responseData.reply;
40
  } catch (error) {
41
- console.error('Error fetching bot response:', 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
  });