AilexGPT commited on
Commit
52d5007
1 Parent(s): bfbdf77

Update chat.js

Browse files
Files changed (1) hide show
  1. chat.js +54 -33
chat.js CHANGED
@@ -1,39 +1,60 @@
1
- // chat.js
2
-
3
- const express = require('express');
4
- const bodyParser = require('body-parser');
5
- const LangChain = require('langchain'); // Stellen Sie sicher, dass Sie die LangChain-Bibliothek installiert haben
6
-
7
- const app = express();
8
- app.use(bodyParser.json());
9
-
10
- // Erstellen des LangChain-Modells
11
- const llm = LangChain.CTransformers({
12
- model: 'TheBloke/CodeLlama-7B-GGUF',
13
- model_type: 'llama',
14
- max_new_tokens: 1096,
15
- temperature: 0.2,
16
- repetition_penalty: 1.13,
17
- gpu_layers: 2
18
- });
19
 
20
- // API-Endpunkt für Chatbot-Anfragen
21
- app.post('/chatbot', async (req, res) => {
22
- const userQuery = req.body.query;
23
- if (!userQuery) {
24
- return res.status(400).send({ error: 'Query is required' });
 
 
 
25
  }
26
 
27
- try {
28
- const response = await llm.run({query: userQuery});
29
- res.send({ reply: response });
30
- } catch (error) {
31
- res.status(500).send({ error: 'Error processing request' });
32
  }
33
- });
34
 
35
- // Starten des Servers
36
- const PORT = 3000;
37
- app.listen(PORT, () => {
38
- console.log(`Server running on port ${PORT}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  });
 
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
  });