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

Update chat.js

Browse files
Files changed (1) hide show
  1. chat.js +22 -35
chat.js CHANGED
@@ -1,38 +1,25 @@
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
-
 
 
 
 
 
 
 
 
 
 
1
+ casync function query(data) {
2
+ const response = await fetch(
3
+ "https://api-inference.huggingface.co/models/eastwind/tinymix-8x1b-chat",
4
+ {
5
+ headers: {
6
+ 'Authorization': 'Bearer ' + process.env.HUGGINGFACE_API_KEY
7
+ },
8
+ method: "POST",
9
+ body: JSON.stringify(data),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  }
11
+ );
12
+ const result = await response.json();
13
+ return result;
 
 
 
 
 
 
14
  }
15
 
16
+ // Beispiel einer Funktion, die aufgerufen wird, wenn der Benutzer eine Nachricht sendet
17
+ async function sendMessage(message) {
18
+ try {
19
+ const response = await query({ "inputs": message });
20
+ console.log(JSON.stringify(response));
21
+ // Hier könnten Sie die Antwort in Ihrem Chat-Interface anzeigen
22
+ } catch (error) {
23
+ console.error('Error during the API request:', error);
24
+ }
25
+ }