AilexGPT commited on
Commit
3aa2e72
1 Parent(s): ab499b2

Update chat.js

Browse files
Files changed (1) hide show
  1. chat.js +26 -43
chat.js CHANGED
@@ -1,55 +1,38 @@
1
- const userInput = document.getElementById('message-input');
2
  const sendButton = document.getElementById('send-message');
 
3
  const chatBox = document.getElementById('conversation-history');
4
- const status = document.getElementById('status');
5
-
6
- // Funktion, um eine Abfrage an die Hugging Face API zu senden
7
- async function queryHuggingFace(data) {
8
- const response = await fetch(
9
- "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-v0.1",
10
- {
11
- headers: { Authorization: "Bearer IhrHuggingFaceAPIToken" },
12
- method: "POST",
13
- body: JSON.stringify(data),
14
- }
15
- );
16
- const result = await response.json();
17
- return result;
18
- }
19
-
20
- // Event-Listener für den Senden-Button
21
- sendButton.addEventListener('click', () => {
22
- const message = userInput.value.trim();
23
- if (!message) return;
24
 
 
 
 
25
  displayMessage('Du: ' + message, 'user');
26
  userInput.value = '';
27
 
28
- status.textContent = 'Der Bot antwortet...';
29
- queryHuggingFace({"inputs": message})
30
- .then(response => {
31
- displayMessage('Bot: ' + response.generated_text, 'bot');
32
- status.textContent = '';
33
- })
34
- .catch(error => {
35
- displayMessage('Fehler beim Generieren der Antwort: ' + error, 'error');
36
- status.textContent = '';
37
- });
 
 
 
 
 
 
 
38
  });
39
 
40
- // Nachricht im Chat anzeigen
41
  function displayMessage(message, sender) {
42
- const messageElement = document.createElement('div');
43
- messageElement.className = 'message ' + sender;
44
- messageElement.textContent = message;
45
- chatBox.appendChild(messageElement);
46
- chatBox.scrollTop = chatBox.scrollHeight; // Scroll to the bottom
47
  }
48
 
49
- // Enter-Taste als Alternative zum Senden
50
- userInput.addEventListener('keypress', function(e) {
51
- if (e.key === 'Enter') {
52
- sendButton.click();
53
- }
54
- });
55
 
 
 
1
  const sendButton = document.getElementById('send-message');
2
+ const userInput = document.getElementById('message-input');
3
  const chatBox = document.getElementById('conversation-history');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ sendButton.addEventListener('click', async () => {
6
+ const message = userInput.value.trim();
7
+ if (message) {
8
  displayMessage('Du: ' + message, 'user');
9
  userInput.value = '';
10
 
11
+ try {
12
+ const response = await fetch('http://localhost:7860/api/chat/', {
13
+ method: 'POST',
14
+ headers: {
15
+ 'Content-Type': 'application/json',
16
+ },
17
+ body: JSON.stringify({
18
+ "inputs": message,
19
+ }),
20
+ });
21
+ const data = await response.json();
22
+
23
+ displayMessage('Bot: ' + data, 'bot');
24
+ } catch (error) {
25
+ console.error('Fehler beim Senden der Nachricht:', error);
26
+ }
27
+ }
28
  });
29
 
 
30
  function displayMessage(message, sender) {
31
+ const messageElement = document.createElement('div');
32
+ messageElement.className = 'message ' + sender;
33
+ messageElement.textContent = message;
34
+ chatBox.appendChild(messageElement);
35
+ chatBox.scrollTop = chatBox.scrollHeight; // Scroll to the bottom
36
  }
37
 
 
 
 
 
 
 
38